ASP.NET(vb.net) & Cookies Variables - Cookies : A cookie is a small bit of text that accompanies requests and pages as they go between the Web server and browser. The cookie contains information the Web application can read whenever the user visits the site.
ShotDev Focus:
- ASP.NET(vb.net) & Cookies Variables [Create Cookies,Read Cookies,Delete Cookies]
Example
Create Cookies
AspNetCookieObject1.aspx
<%@ Page Language="VB" %> <script runat="server"> Sub Page_Load(sender As Object, e As EventArgs) '*** Properties on the Cookies ***' Response.Cookies("myCookie1")("SiteName") = "www.ShotDev.Com" Response.Cookies("myCookie1")("Creator") = "Mr.Weerachai Nukitram" Response.Cookies("myCookie1").Expires = DateTime.Now.AddDays(7) '*** Instance of the HttpCookies ***' Dim newCookie As HttpCookie = New HttpCookie("myCookie2") newCookie("Version") = "2009" newCookie("ProjectReader") = "Mr.Win" newCookie("NumTeam") = "1" newCookie("StartProject") = "Jan 2008" newCookie.Expires = DateTime.Now.AddDays(7) Response.Cookies.Add(newCookie) Me.lblText.Text = "Cookie Created" Me.hplLink.Text = "Click here to check" Me.hplLink.NavigateUrl = "AspNetCookieObject2.aspx" End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form runat="server"> <asp:Label id="lblText" runat="server"></asp:Label><br /><br /> <asp:HyperLink id="hplLink" runat="server"></asp:HyperLink><br /> </form> </body> </html>
Read Cookies
AspNetCookieObject2.aspx
<%@ Page Language="VB" %> <script runat="server"> Sub Page_Load(sender As Object, e As EventArgs) '*** Read Cookie ***' If Not IsNothing(Request.Cookies("myCookie1")) Then Response.write("SiteName = "& Request.Cookies("myCookie1")("SiteName") & "<br>") Response.write("Creator = "& Request.Cookies("myCookie1")("Creator") & "<br>") End IF Response.write("<hr>") '*** Read Cookie ***' If Not IsNothing(Request.Cookies("myCookie2")) Then Response.write("Version = "& Request.Cookies("myCookie2")("Version") & "<br>") Response.write("ProjectReader = "& Request.Cookies("myCookie2")("ProjectReader") & "<br>") Response.write("NumTeam = "& Request.Cookies("myCookie2")("NumTeam") & "<br>") Response.write("StartProject = "& Request.Cookies("myCookie2")("StartProject") & "<br>") End IF Me.hplLink.Text = "Click here to delete" Me.hplLink.NavigateUrl = "AspNetCookieObject3.aspx" End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form runat="server"> <asp:HyperLink id="hplLink" runat="server"></asp:HyperLink><br /> </form> </body> </html>
Delete Cookie
AspNetCookieObject3.aspx
<%@ Page Language="VB" %> <script runat="server"> Sub Page_Load(sender As Object, e As EventArgs) '*** Delete Cookie ***' If Not IsNothing(Request.Cookies("myCookie1")) Then Dim delCookie1 As HttpCookie delCookie1 = New HttpCookie("myCookie1") delCookie1.Expires = DateTime.Now.AddDays(-1D) Response.Cookies.Add(delCookie1) End IF '*** Delete Cookie ***' If Not IsNothing(Request.Cookies("myCookie2")) Then Dim delCookie2 As HttpCookie delCookie2 = New HttpCookie("myCookie2") delCookie2.Expires = DateTime.Now.AddDays(-1D) Response.Cookies.Add(delCookie2) End If Me.lblText.Text = "Now Cookie Deleted" Me.hplLink1.Text = "Click here to check" Me.hplLink1.NavigateUrl = "AspNetCookieObject2.aspx" Me.hplLink2.Text = "Click here to create" Me.hplLink2.NavigateUrl = "AspNetCookieObject1.aspx" End Sub </script> <html> <head> <title>ShotDev.Com Tutorial</title> </head> <body> <form runat="server"> <asp:Label id="lblText" runat="server"></asp:Label><br /><br /> <asp:HyperLink id="hplLink1" runat="server"></asp:HyperLink><br /> <asp:HyperLink id="hplLink2" runat="server"></asp:HyperLink><br /> </form> </body> </html>
Screenshot