1 / 14

Cookies

Cookies. 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). Cookie’s Properties.

marged
Download Presentation

Cookies

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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. <script runat="server"> 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 </script> <html> <body> <form runat="server" > <p>&nbsp;</p> <p>Please enter customer ID:<asp:textbox id="cid" runat="server" /></p><br> <p>Please enter your name:<asp:textbox id="cname" runat="server" /><br> <p>This ID and name will be used to set your cookie.</p> <p><asp:button id="button1" text="Submit" runat="server" onClick="SetCookie" /></p> </form> </body> </html>

  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 ("<hr>Welcome:" & objDataReader("CustName") & "<hr>") Demo:ASPNET/CookieGreeting.aspx

  9. Demo: CookieGreetingShowOrder.aspx sub page_Load() If not Page.IsPostBack then dim cid as string cid=request.cookies("CID").value objConn.open() strSQL="select * from webcustomer where CustID= '" & cid & "'" dim objComm as new OledbCommand(strSQL,objConn) objDataReader=objComm.executeReader() if not objDataReader.Read()then response.write("We don't have your record" & "</br>") else response.write ("<hr>Welcome:" & objDataReader("CustName") & "<hr>") end if session("cid")=cid session("cname")=objDataReader("CustName") objConn.close() end if end sub

  10. sub showOrders(sender as object, e as EventArgs) dim cid, cname as string cid=session("cid") cname=session("cname") msg1.text=cname & " orders: " objConn.open() strSQL="select * from webOrder where CustID= '" & cid & "'" dim objComm as new OledbCommand(strSQL,objConn) objDataReader=objComm.executeReader() dgOrder.Datasource=objDataReader dgOrder.DataBind() objConn.close() end sub

  11. Session • System.Web.HttpSessionState • Properties: • SessionID • IsCookieLess • TimeOut • Count • Methods • Abandon

  12. Working with the Session • To place a value into the collection simply assign it a key and then assign the value: • Session (“Age”)=25 • To read values from the collection: • myAge = Session(“Age”) • Session ID: • Session.SessionID • Demo: sessionID.aspx • To remove a session • Session.abandon

  13. Increase Counter by One. What is wrong? <script runat="server"> dim counter as integer sub Page_Load() counter=0 end sub sub IncreaseCounter(sender as object, e as EventArgs) counter=counter+1 response.write("The counter's value is:" + cstr(counter)) end sub Demo: StateTestCounter.ASPX

  14. Save Counter in SessionDemo: StateTestCounter2.ASPX <script runat="server"> dim counter as integer sub Page_Load() if not page.isPostBack then counter=0 session("MyCounter")=counter else counter=session("MyCounter") end if end sub sub IncreaseCounter(sender as object, e as EventArgs) counter=counter+1 response.write("The counter's value is:" + cstr(counter)) session("MyCounter")=counter End sub

More Related