Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Read cookies using Response Object in code behind asp.net.

Author: Zunnair
Download Source Code : 883_Read Cookies.zip

In this simple article you will learn to Read cookies.

 Create a web application in c#

Drag two buttons and two labels on the form

Then write code on form load event

 

C#

protected void Page_Load(object sender, EventArgs e)

    {

       

        Label1.Text = "Cookie Value:";

        Label2.Text = "";

        Button1.Text = "Get Cookie Value";

        Button2.Text = "Write Cookie";

    }

VB

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

 

        Label1.Text = "Cookie Value:"

        Label2.Text = ""

        Button1.Text = "Get Cookie Value"

        Button2.Text = "Write Cookie"

    End Sub

After that write some code on button click even

C#

protected void Button1_Click(object sender, EventArgs e)

    {

        System.Web.HttpCookie cookie = new HttpCookie("CookieTest");

        cookie.Value = TextBox1.Text.Trim();

        cookie.Expires = DateTime.Now.AddDays(1);

        Response.Cookies.Add(cookie);

    }

VB

  Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)

        If Request.Cookies("CookieTest") IsNot Nothing Then

            Label2.Text = Request.Cookies("CookieTest").Value.ToString()

        End If

    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim cookie As System.Web.HttpCookie = New HttpCookie("CookieTest")

        'cookie.Name = Pricecookie

        cookie.Value = "this is cookie...DevAsp"

        cookie.Expires = DateTime.Now.AddDays(1)

        Response.Cookies.Add(cookie)

    End Sub

 

This is simple code to read cookies using Response object.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Change Page Meta tags dynamically in asp.net using C#.

Disclaimer - Privacy
© 2002-2017 DevASP.net