140 likes | 321 Views
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.
E N D
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 • System.Web/HttpCookie • Name • Value • Expires • To write a cookie: • Response.Cookies.Add(cookieObj)
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
<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> </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>
Reading Cookies Dim custid as string Dim custName as string custid=request.cookies("cid").value custname=request.cookies("cname").value
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
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
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
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
Session • System.Web.HttpSessionState • Properties: • SessionID • IsCookieLess • TimeOut • Count • Methods • Abandon
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
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
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