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

Read cookies using Response Object in code behind

Author: Zunnair
Download Source Code : 883_Read

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



protected void Page_Load(object sender, EventArgs e)



        Label1.Text = "Cookie Value:";

        Label2.Text = "";

        Button1.Text = "Get Cookie Value";

        Button2.Text = "Write Cookie";



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


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);




  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)


    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 using C#.

Disclaimer - Privacy
© 2002-2017