Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 10224 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to create cookies in ASP.NET?

How to create cookies in ASP.NET?

2 vote(s)
Rating: 5 out of 5
Article posted by Sheonarayan on 6/14/2011 | Views: 26290 | Category: ASP.NET | Level: Beginner | Points: 250 red flag


To create a cookie using ASP.NET, we can follow below approach. In this article, we are going to learn two different ways of creating cookies in ASP.NET.


Introduction

A Cookie is a small amount of text that attached to the requet and response in between browser and the server. This small amount of text can be read by the application whenever user browse the it. Please note that cookie is not a good medium to store the confidential data as it is stored into the user browser.

ASPX page

 

<asp:Button ID="btnCreate" runat="server" Text="Create Cookie - One way" OnClick="CreateCookie1" />

<asp:Button ID="Button1" runat="server" Text="Create Cookie - Other way" OnClick="CreateCookie2" />

In the above code snippet, we have two buttons that calls CreateCookie1 and CreateCookie2 server side method respectively. Here we are trying to show two different ways to create cookie using ASP.NET. You can use anyone of them.

Code behind

 

protected void CreateCookie1(object sender, EventArgs e)

{

string cookieValue = "Cookie Value 1 ";

HttpCookie cookie = new HttpCookie("CookieKey1", cookieValue);

Response.Cookies.Add(cookie);

}

protected void CreateCookie2(object sender, EventArgs e)

{

string cookieValue = "Cookie Value 2 ";

Response.Cookies["CookieKey2"].Value = cookieValue;

}

Get hundreds of ASP.NET How to Tips and Tricks.

CreateCookie1 method

In this method, we are declaring a string variable with value that we will store into the cookie. Then we are instantiating the HttpCookie object by passing the cookie name and value to store. At last we are calling the Response.Cookies.Add method by passing the HttpCookie object to create the cookie into the user browser.

CreateCookie2 method

In this method, we are declaring a string variable with value to store into the cookie and using the Response.Cookies collection to set the cookie value by passing the cookie name to create. In this case, we do not need to explicitly call the Add method of the Response.Cookies.

Output

After running the above url, press F12 (in IE 9) to open the developer tools and then click on Cache > View Cookie Information menu to display the above output.

To learn how to read cookies in ASP.NET, click here.

Thanks. If you like this article, share it to your friends by tweeting or sharing it via facebook, linkedin or other websites.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Sheo Narayan

Experience:8 year(s)
Home page:http://www.snarayan.com
Member since:Tuesday, July 08, 2008
Level:HonoraryPlatinum
Status: [Microsoft_MVP] [Administrator]
Biography:Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001.

Connect me on Facebook | Twitter | LinkedIn | Blog

 Responses
Posted by: Akiii | Posted on: 15 Jun 2011 11:59:47 AM | Points: 25

good article sir....

Thanks and Regards
Akiii

Posted by: SheoNarayan | Posted on: 16 Jun 2011 10:52:34 AM | Points: 25

Thanks Akiii,

Keep reading ....



>> Write Response - Respond to this post and get points
Related Posts

Adding dynamic field in to the gridview.

This article helps in understanding how the readonly property behaves when viewstate is enabled or disabled for textbox.

In this article, I will explain how to uplaod files using ASP.NET ,VB.NET.It is a simplest way of uploading the files.

Many a times we are in the need of reading the MS Excel file and populate them into the GridView or validating the data of the excel sheet. In this article we are going to see how to populate the MS Excel data into the GridView and loop through them.

In this article, we shall learn how to select GridView rows and persist the selection during GridView pagination.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/20/2013 8:47:20 AM