Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29782 |  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: 9879 | 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

In this article I will be explaining step by step building an application using MS-Charting tool(a free add-on for Visual Studio)

This is my first article on asp.net and I will try to explain the basics of asp.net page life cycle and easy understanding of its usage. Page life cycle depends on whether it is requested for the first time or it is after postback(page request of itself) and finalize to the web server. When a web page is requested to web server, it goes through a series of sequence of steps/events (like initialization, instantiating controls, restoring and maintaining state, running event handler code, and rendering) before it returned back to the web browser.

Here i have tried to put some explanation about Query String with some examples.

In this article we shall learn ASP.NET Dynamic data. With .NET 3.5, a new cool feature is added named as Dynamic Data which is a RAD development paradigm for building functional websites.

This is a pretty simple tutorial dedicated to LINQ newbie’s who want to learn how to do CRUD operations using LINQ entities. I am sure many experienced LINQ players would criticize me for such a mild article. One of the biggest catch which I found is the in-memory update service by LINQ, which I think for any newbie for LINQ is a must to understand.

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 found 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/28/2012 11:55:09 AM