This is a discussion on How to use cookie in asp.net using C#?.Is there any sample for cookies? within the ASP and ASP.NET Programming forums, part of the Web Development category; How to use cookie in asp.net using C#?.Is there any sample for cookies?...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| In order to handle a cookie in your system you should aware of 2 things: 1 - How to create them 2 - How to delete them So, first in order to create a cookie you have 2 variable must be defined as they are the cookie name and the duration of the cookie before it expired • In order to create a cookie it looks like this one ////SET THE COOKIE//// Response.Cookies["test"].Value = UserName.Text ; Response.Cookies["test"].Expires = DateTime.Now.AddYears(30); Where test is the cookie name and the username.text is the textbox that will be used to define the cookies information. * In order to delete a cookie use this Response.Cookies["test"].Expires = DateTime.Now.AddYears(-30); * In order to check whether the cookie is found or not and retrieve the cookies information use this ////check to see if cookie presented//// if (Request.Cookies["test"] == null) TextBox2.Text = "No cookie found"; else TextBox2 .Text = Request.Cookies["test"].Value; ////END check to see if cookie presented//// using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Response.Cookies["ashok"].Value = TextBox1.Text; Response.Cookies["ashok"].Expires = DateTime.Now.AddSeconds(6); } } protected void Button1_Click(object sender, EventArgs e) { if (Request.Cookies["ashok"] != null) Label1.Text = Request.Cookies["ashok"].Value; else Label1.Text = "Cookie Not Found"; } }
__________________ The OXYGEN Delivers edgy, intelligent Technology to all... |
| |||
| using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { Response.Cookies["MyCookie"]["Data"] = TextBox1.Text; Response.Cookies["MyCookie"]["Time"] = DateTime.Now.ToString("G"); Response.Cookies["MyCookie"].Expires=DateTime.Now.AddMonths(1); Label1.Text = "Cookie created!<p>" + "Your cookie contains:<font color=red>" + Request.Cookies["MyCookie"]["Data"] + "<br>" + Request.Cookies["MyCookie"]["Time"] + "</font>"; Response.Cookie("MyCookie").Expires=DateTime.FromS tring("2006-10-1"); } protected void Button2_Click(object sender, EventArgs e) { if (Request.Cookies["MyCookie"] == null) Label1.Text = "There is no cookie:"; else Label1.Text = "Your cookie contains:" + "<font color=red>" + Request.Cookies["MyCookie"]["Data"] + "<br>" + Request.Cookies["MyCookie"]["Time"] + "</font>"; } } |
| |||
| Hi All.. I am using shoping cart detail saved in cookie, the user selected product id stored in cookie, i can get all the value from cookie, but i can get productId for current and previous values only, i can't get all productid... pls gimme solution... Thanks ![]()
__________________ H2O Without us, no one can survive.. |
| |||
| Quote:
Here's a tutorial that shows you how to use cookies in ASP .NET. I'm not going to explain the role of cookies in web applications or cover any other theoretical aspect of cookies. There are many (similar) ways to handle cookies in ASP .NET. I'm only going to show you one of the ways, my way. Oh, and we're going to use C#, although the code can be adapted to Visual Basic .NET easily. How to create a cookie. Here's a new cookie named cakes. Code: HttpCookie myCookie = new HttpCookie("cakes"); Code: myCookie.Values.Add("aaa", "favour");
myCookie.Values.Add("bbb", "secret"); Code: Response.Cookies.Add(myCookie); Here's how to get the keys and values stored in a cookie: Code: Response.Write(myCookie.Value.ToString()); However, most of the time you'll want to get the value stored at a specific key. If we want to find the value stored at our bbb key, we use this: Code: Response.Write(myCookie["bbb"].ToString());
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/asp-asp-net-programming/2996-how-use-cookie-asp-net-using-c-there-any-sample-cookies.html | |||
| Posted By | For | Type | Date |
| cookie | This thread | Refback | 08-17-2007 04:16 PM |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sample for load *.OBJ file using Android Object3D class | leoraja8 | Mobile Software Development | 0 | 01-09-2008 01:52 AM |
| Is there any sample code for sending a text file from the phone to the PC via... | mobilegeek | Mobile Software Development | 4 | 09-11-2007 04:12 AM |
| Write sample code for pagination using java script | itbarota | HTML, CSS and Javascript Coding Techniques | 1 | 09-11-2007 12:00 AM |
| Any simple step or sample code for transparency mobile..? | mobilegeek | Mobile Software Development | 1 | 07-24-2007 06:43 AM |
| sample script in Rational Robot | vigneshgets | Software Testing | 0 | 05-22-2007 08:25 AM |