Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Network - Reading and Writing Cookies in ASP.NET and VB
Reading and Writing Cookies in ASP.NET and VB


ASP.NET Network Tutorial

This tutorial will show how we can both write to and read from a simple cookie. VB version.

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Using Cookies in web pages is very useful for temporarily storing small amounts of data, for the website to use. These Cookies are small text files that are stored on the user's computer, which the web site can read for information; a web site can also write new cookies.

An example of using cookies efficiently would be for a web site to tell if a user has already logged in. The login information can be stored in a cookie on the user's computer and read at any time by the web site to see if the user is currently logged in. This enables the web site to display information based upon the user's current status - logged in or logged out.

Cookies can be very powerful, yet are very small text files and, fortunately, ASP.NET makes it easy for us to both read and write cookies.

In this example, we will have two ASPX pages: One to write a cookie, and one to read this same cookie.
The first ASPX page will look something like this:

<form id="form1" runat="server">
Cookie Name <asp:textbox id="NameField" runat="server"/><br />
Cookie Value <asp:textbox id="ValueField" runat="server"/><br />
<asp:button ID="Button1" text="WriteCookie" onclick="WriteClicked" runat="server" /><br />
<asp:HyperLink ID="lnkRead" NavigateUrl="Read.aspx" runat="server" Visible="false">Read the cookies <br />
</form>

The code-behind for this page will look something like this:

Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub

Protected Sub WriteClicked(ByVal Sender As Object, ByVal e As EventArgs) Handles Button1.Click
lnkRead.Visible = True
lnkRead.NavigateUrl = "Read.aspx?cookie=" & NameField.Text.ToString()

'Create a new cookie, passing the name into the constructor
Dim cookie As New HttpCookie(NameField.Text)

'Set the cookies value
cookie.Value = ValueField.Text

'Set the cookie to expire in 1 minute
Dim dtNow As DateTime = DateTime.Now
Dim tsMinute As New TimeSpan(0, 0, 1, 0)
cookie.Expires = dtNow.Add(tsMinute)

'Add the cookie
Response.Cookies.Add(cookie)

lblRead.Text = "Cookie written."
End Sub
End Class

We also create a page to read the cookie, which will be as follows:

<form id="form1" runat="server">
<asp:Label ID="lblCookie" runat="server"></asp:Label>
</form>

The code-behind for this page will look something like this:

Partial Class Read
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Request.QueryString("cookie") IsNot Nothing Then
ReadCookie()
End If
End Sub

Protected Sub ReadCookie() 'Get the cookie name the user entered
Dim strCookieName As String = Request.QueryString("cookie").ToString()

'Grab the cookie
Dim cookie As HttpCookie = Request.Cookies(strCookieName)

'Check to make sure the cookie exists
If cookie Is Nothing Then
lblCookie.Text = "Cookie not found. <br><hr>"
Else 'Write the cookie value
Dim strCookieValue As String = cookie.Value.ToString()
lblCookie.Text = "The " & strCookieName & " cookie contains: <b>" & strCookieValue & "</b><br><hr>"
End If
End Sub
End Class

Download the Full Working Version of this Project written with Visual Studio.NET VB 2005 Here!

Looking for the C#.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks