Introducing DotNetFunda.com on mobile http://m.dotnetfunda.com ! Be with DotNetFunda.com on the go !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 29881 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > How to read-write multi-valued cookies in ASP.NET?

How to read-write multi-valued cookies in ASP.NET?

1 vote(s)
Rating: 5 out of 5
Article posted by SheoNarayan on 6/20/2011 | Views: 4208 | Category: ASP.NET | Level: Advance | Points: 250 red flag


In this article we are going to learn how to read and write multi-valued cookies in ASP.NET. Multi-valued cookies is just like a normal cookies however it gives us flexibility to store multiple data in a single cookies using subkeys.

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="btnCookieSet" runat="server" Text="Set Cookie" OnClick="SetCookie" />

<asp:Button ID="btnCookieGet" runat="server" Text="Get Cookie" OnClick="GetCookie" />

<asp:Button ID="btnRemoveCookie" runat="server" Text="Remove Cookie" OnClick="RemoveCookie" />

In the above code snippet we have three buttons,

  1.  btnCookieSet executes SetCookie server side method
  2.  btnCookieGet executes GetCookie server side method
  3.  btnRemoveCookie executes RemoveCookie server side method

CODE BEHIND

protected void SetCookie(object sender, EventArgs e)

{

Response.Cookies["MyCookie"]["My1"] = "My cookie value 1";

Response.Cookies["MyCookie"]["My2"] = "My cookie value 2";

Response.Cookies["MyCookie"].Expires = DateTime.Now.AddSeconds(10);


// the above code can be replaced with //HttpCookie cookie = new HttpCookie("MyCookie"); //cookie.Values["My1"] = "My cookie value 1"; //cookie.Values["My2"] = "My cookie value 2"; //cookie.Expires = DateTime.Now.AddSeconds(10); //Response.Cookies.Add(cookie);


}

protected void GetCookie(object sender, EventArgs e)

{

if (Request.Cookies["MyCookie"] != null)

{

string cookieValue = Request.Cookies["MyCookie"]["My1"].ToString();

Response.Write(cookieValue);

cookieValue = Request.Cookies["MyCookie"]["My2"].ToString();

Response.Write(cookieValue);


// Above code can be replaced with //HttpCookie cookie = Request.Cookies["MyCookie"]; //Response.Write(cookie.Values["My1"]); //Response.Write(cookie.Values["My2"]);

}

}

protected void RemoveCookie(object sender, EventArgs e)

{

Response.Cookies["MyCookie"].Expires = DateTime.Now.AddDays(-1);

}

Get video tutorials of hundreds of ASP.NET Tips and Tricks.

SetCookie method

This method creates a cookie called “MyCookie” with subkey as “My1” and “My2”. As “My1” and “My2” is the subkeys of “MyCookie” so setting the expiry property for this sets the epiration of both “My1” and “My2”. 

The same can be done using the HttpCookie object as well, it has been shown in the above code snippet.

GetCookie method

In this method, first we are checking for “MyCookie”, if it is not null then fetching both cookie subkeys and writing on the page using Response.Write method.

RemoveCookie method

To remove the cookie, we have used the Expires property, setting Expires property for the main key applies for both subkyes.

OUTPUT

In case you have missed earlier articles series on cookies, click here. In the next article, we shall learn how to limit the scope of the cookies to a particular folder.

Thanks for reading.

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: 21 Jun 2011 12:21:00 AM | Points: 25

Hi Sir,
It great to read all the essentials of cookies that we implement in real life scenarios......
Your articles are invaluable......

Looking forward for more articles related to this matter....
Thanks and Regards
Akiii

Posted by: SheoNarayan | Posted on: 21 Jun 2011 12:22:56 AM | Points: 25

Thanks Akiii,

More will be coming , keep reading and referring friends.

Thanks
Regards

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

This article describe how to create an image from text in asp.net with C#.

Getting the data from the row that is being currently edited within a GridView.

To perform CRUD operation (create, read, update and delete) operation with GridView, we can follow this approach.

ASP.NET 4.0 supports different types of web application development and associated templates. In this article, we will talk about the ASP.Net Dynamic Data and how we can create a fully functional web application without writing a single line of code.

In this article we will take up a simple example and try to implement DI using Unity Application Blocks thus resulting in loosely coupled architecture.

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:18 AM