Download presentation
Presentation is loading. Please wait.
1
Cookies
2
Data in Cookies Which web site set the cookie Expiration date –DateTime data type –TimeSpan data type One or more pieces of data Keys Define a new cookie: –Dim CookieCID as new HttpCookie(“cid”) Add to: Response.Cookies –Response.cookies.add(cookieCID)
3
Cookie’s Properties System.Web/HttpCookie –Name –Value –Expires To write a cookie: –Response.Cookies.Add(cookieObj)
4
Creating Cookies dim cookieCID as New HttpCookie("cid") dim cookieCNAME as new HttpCookie("cname") dim dt as dateTime=dateTime.now() dim ts as new TimeSpan(30,0,0,0) cookieCID.value=cid.text cookieCname.value=cname.text cookieCID.expires=dt.add(ts) cookieCname.expires=dt.add(ts) response.cookies.add(cookieCID) response.cookies.add(cookieCNAME) Note: The name(or key)of cookieCID is “cid” Demo: ASPNET/CookieForm.aspx
5
sub setCookie(Sender As Object, E As EventArgs) dim cookieCID as New HttpCookie("cid") dim cookieCNAME as new HttpCookie("cname") dim dt as dateTime=dateTime.now() dim ts as new TimeSpan(30,0,0,0) cookieCID.value=cid.text cookieCname.value=cname.text cookieCID.expires=dt.add(ts) cookieCname.expires=dt.add(ts) response.cookies.add(cookieCID) response.cookies.add(cookieCNAME) end sub Please enter customer ID: Please enter your name: This ID and name will be used to set your cookie.
6
Reading Cookies Dim custid as string Dim custName as string custid=request.cookies("cid").value custname=request.cookies("cname").value
7
Demo: CookieRead.ASPX sub page_load() cid.text=request.cookies("cid").value cname.text=request.cookies("cname").value cookieCount.text=cstr(request.cookies.count) Dim ckey As String For Each ckey In Request.Cookies.Keys Response.Write(ckey) Next end sub Note: SessionID
8
Using Cookie with DataReader dim strConn as string ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\sales2k.mdb" dim objConn as new OledbConnection(strConn) dim strSQL as string dim objDataReader as oledbDataReader dim cid as string cid=request.cookies("CID").value strSQL="select * from webcustomer where CustID= '" & cid & "'" dim objComm as new OledbCommand(strSQL,objConn) objDataReader=objComm.executeReader() objDataReader.Read() session("cname")=objDataReader("CustName") response.write (" Welcome:" & objDataReader("CustName") & " ") Demo:ASPNET/CookieGreeting.aspx
9
Sending Email From an ASP.Net Page
10
Send a confirmation message. Send a document to a user as attachment. Forgot password?
11
Simple Mail Transport Protocol (SMTP) Email messages are text files. InetPub\MailRoot –PickUp directory: SMTP monitors this directory and sends any messages found in this directory. –Drop: Incoming messages received by SMTP are written to this directory. –BadMail: If an email cannot be delivered or returned to the sender it is moved to this directory.
12
ASP.Net Email Classes SmtpMail class –System.Web.Mail Methods: –Send(From, To, Subject, messageText) SmtpMail.Send(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text) –Send(System.Web.Mail.MailMessage) Demo: –SendMail.aspx –Import system.web.mail
13
Using MailMessage Class to Set Email Properties Email properties: –Attachments –Bcc –Body –BodyFormat Text or Html –Cc –From –Headers –Subject –To
14
Demo: ForgotPassword.ASPX Dim emailAddress As String emailAddress = TextBox1.Text objConn.Open() strSQL = "select * from users where email= '" & emailAddress & "'" Dim objComm As New OleDbCommand(strSQL, objConn) objDataReader = objComm.ExecuteReader() If Not objDataReader.Read() Then Response.Write("We don't have your record" & " ") Else Dim objMsg As New MailMessage() objMsg.From = "webmaster@dchaolaptop" objMsg.To = emailAddress objMsg.Subject = "Your password" objMsg.Body = "Your password is: " & objDataReader("password") SmtpMail.Send(objMsg) Response.Write(" Your password is sent to your email account ") End If
15
UpLoading Files
16
File Field Control HTML File Field Control: –Composite control with a textbox and a Browse button. Convert a HTML File Field control to a server- side control: –Visual Studio: Add a File Field control from the HTML tab. Point to and right click the control and choose Run As a Server Control (This will add the Runat=“Server” attribute to the File Field control).
17
Accessing the Uploaded File FileFieldControlName.PostedFile –This is a System.Web.HttpPostedFile class Properties: –FileName: This name contains the path of the posted file.’ –Contentlength –ContentType Method: –SaveAs – this method save the posted file on server. Demo: Upload.aspx
18
Save Uploaded File Sub btnUpload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim FileName As String Dim strFilePath As String = "c:\inetpub\wwwroot\images\" FileName = upLoadFilePath.PostedFile.FileName.Substring(upLoadFil ePath.PostedFile.FileName.LastIndexOf("\") + 1) strFilePath = strFilePath & FileName upLoadFilePath.PostedFile.SaveAs(strFilePath) response.write("File: " & fileName & " is saved on server") End Sub
19
Insurance Claim Example Uploading claim pictures for insurance cases. Each case may have many pictures. Database: –CaseTable: CaseID, CaseDate, Agent –CasePics: CaseID, PicPathName Each picture is named: CaseID + PictureName and saved in folder: Images Create a web page with a dropdown list of CaseID, a File Field control to choose file, and a upload button. The uploaded picture will be saved in the Images folder and a record will be entered in CasePics file. Demo: Insurance/UploadPic.aspx
20
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim CaseID As String Dim FileName As String Dim strFilePath As String = "c:\inetpub\wwwroot\images\" FileName = upLoadFilePath.PostedFile.FileName.Substring(upLoadFilePath.Poste dFile.FileName.LastIndexOf("\") + 1) CaseID = ListBox1.SelectedItem.Text FileName = CaseID & FileName strFilePath = strFilePath & FileName upLoadFilePath.PostedFile.SaveAs(strFilePath) Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = c:\InsurancePic.mdb" Dim objConn As New OleDbConnection(strConn) Dim strSQL As String = "Insert Into CasePics Values ('" & CaseID & "','" & strFilePath & "')" Dim objComm As New OleDbCommand(strSQL, objConn) objConn.Open() objComm.ExecuteNonQuery() End Sub
21
Calendar Properties: –SelectionMode –SelectedDate –SelectedDates Event: –SelectionChanged
22
AdRotator AdRotator displays images in an advertisement file which is an XML file with properties of images to display.
23
Advertisement File Example images/cake.gif default.aspx Great Cake 1 TestAd images/earth2.gif default.aspx Beautiful Earth 1 TestAd
24
Advertisement Properties ImageURL NavigateURL: The page you go to when you click an image. AlternateText: Text displayed for browsers do not support image. Keyword: Categorize advertisements, can be used with AdRotator’s KeyWordFilter property. Impressions: The relative frequency a particular advertisement should be shown.
25
Validation Controls CompareValidator: (Collating sequence comparison) –Properties: ControlToValidate ErrorMessage Operator: Greater, LessThan, … ValueToCompare RangeValidator: –MaximumValue, MinimumValue RequiredFieldValidator
26
CustomValidator: Checks the user’s data entry using validation logic from a customer method you write processed on the server or the client. Use the ClientValidationFunction property to call a client-site validation script. For server-site validation, this control raises an OnServerValidate event where you can write an event procedure to validate data. The OnServerValidate event is triggered by: Page.IsValid statement
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.