
Navigator : Home > Tutorials > Internet Browsers Tutorials > ...
Working with cookie in ASP.NET 2.0 and VB.NET
Websites typically use session cookies to ensure that users are recognized when they move from page to page within one site and that any information you have entered is remembered. For example, if an e-commerce site which uses session cookies then items placed in a shopping cart would appear by the time you reach the checkout. We will introduce you how to work with the cookies in ASP.NET 2.0 and VB.NET in this tutorial.
The browser is responsible for managing cookies on a user system. Cookies are sent to the browser via the HttpResponse object that exposes a collection called Cookies. You can access the HttpResponse object as the Response property of your Page class. Any cookies that you want to send to the browser must be added to this collection. When creating a cookie, you specify a Name and Value.
When a browser makes a request to the server, it sends the cookies for that server along with the request. In this ASP.NET applications, we can read the cookies using the HttpRequest object, which is available as the Request property of Page class.
We added one text box, two buttons and 1 label to the web page. The text box is used for inputting the information of cookie. By clicking Add button, the sample application will create a new cookie. By clicking View button, you will see the cookie created. The expiration date of cookie will be set to 2006-10-1.
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Cookies("MyCookie")("Data") = TextBox1.Text Response.Cookies("MyCookie")("Time") = DateTime.Now.ToString("G") Response.Cookies("MyCookie").Expires=DateTime.Now.AddMonths(1) Label1.Text = "Cookie created!<p>" & "Your cookie contains:<font color=red>" & Request.Cookies("MyCookie")("Data") & "<br>" & Request.Cookies("MyCookie")("Time") & "</font>" Response.Cookie("MyCookie").Expires=DateTime.FromString("2006-10-1")</p> End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
If (Request.Cookies("MyCookie") Is Nothing) Then
Label1.Text = "There is no cookie:" Else
Label1.Text = "Your cookie contains:" & "<font color=red>" & Request.Cookies("MyCookie")("Data") & "<br>" & Request.Cookies("MyCookie")("Time") & "</font>" End If End Sub |
The front end Default.aspx page looks something like this:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server"> <title></title> </head> <body>
<form id="form1" runat="server"> <div>
<fieldset> <legend>rdfdf</legend> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" Text="Add" Width="70px" /> <asp:Button ID="Button2" runat="server" Text="View" Width="84px" /> <br /> <br /> <asp:Label ID="Label1" runat="server" Text="" Width="138px"></asp:Label> </fieldset> </div> </form> </body> </html> |
The flow for the code behind page is as follows
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
Partial Class _Default
Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Response.Cookies("MyCookie")("Data") = TextBox1.Text Response.Cookies("MyCookie")("Time") = DateTime.Now.ToString("G") Response.Cookies("MyCookie").Expires=DateTime.Now.AddMonths(1) Label1.Text = "Cookie created!<p>" & "Your cookie contains:<font color=red>" & Request.Cookies("MyCookie")("Data") & "<br>" & Request.Cookies("MyCookie")("Time") & "</font>" Response.Cookie("MyCookie").Expires=DateTime.FromString("2006-10-1") End Sub Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
If (Request.Cookies("MyCookie") Is Nothing) Then
Label1.Text = "There is no cookie:" Else
Label1.Text = "Your cookie contains:" & "<font color=red>" & Request.Cookies("MyCookie")("Data") & "<br>" & Request.Cookies("MyCookie")("Time") & "</font>" End If End Sub End Class |
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!