How To Use Cookies In ASP.NET Using VB.NET

In this article we demonstrate on what is Cookies and how you can create and read cookies.
  • 4620
 

A cookie is a small bit of text that accompanies requests and pages as they go between the web server and browser. the cookie contain information the web application can read whenever the user visits the site. Cookies provides a means in web applications to store user specific information. In example when a user visits your site u can use cookies to store user preferences or other information. When the user visit your website another time, the application can retrieve the information it stored earlier. Creating cookies with asp.net is simple and straight forward.
The System.Web namespace offers a class called HttpCookie to create cookies. The 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. In this ASP.NET application you can read the cookies using the HttpRequest object which is available as the Request property of the page class.

We added one Textbox two Buttons and a Label to the web page. The Textbox 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 2011-10-4.

 How to create and Read cookies

  • simply just open a new Web site.
  • Drag the Textbox, two buttons and a Label on web page. The page will display like below.

    cookies1..gif
     
  • Write the below code.

        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.Cookies("MyCookie").Expires = DateTime.Parse("2011-10-4")

        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.Cookie
                ("MyCookie")("Time") & "</font>"
            End If
        End Sub
     

  • The front end Default.aspx page looks like below.

    <%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
        CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <
    asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <h2>
            <asp:TextBox ID="TextBox1" runat="server" Height="31px" Width="123px"></asp:TextBox>
        </h2>
        <p>
    <asp:Button ID="Button1" runat="server" Text="Add" Width="109px" />
    &nbsp;&nbsp;
    <asp:Button ID="Button2" runat="server" Text="View" Width="116px" />
        </p>
        <p>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </p>
    </asp:Content>
Output

Cookies2..gif

Cookies3.gif
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.