Tuesday, 19 July 2011

Working with cookies in ASP.NET 2.0 and C#


Websites typically use session cookies to ensure that users are recognized when they move from page to page within one site and that any information you have entered is remembered. For example, if an e-commerce site which uses session cookies then items placed in a shopping cart would appear by the time you reach the checkout. We will introduce you how to work with the cookies in ASP.NET 2.0 and C# in this tutorial.

The browser is responsible for managing cookies on a user system. Cookies are sent to the browser via the HttpResponse object that exposes a collection called Cookies. You can access the HttpResponse object as the Response property of your Page class. Any cookies that you want to send to the browser must be added to this collection. When creating a cookie, you specify a Name and Value.

When a browser makes a request to the server, it sends the cookies for that server along with the request. In this ASP.NET applications, we can read the cookies using the HttpRequest object, which is available as the Request property of Page class.

We added one text box, two buttons and 1 label to the web page. The text box is used for inputting the information of cookie. By clicking Add button, the sample application will create a new cookie. By clicking View button, you will see the cookie created. The expiration date of cookie will be set to 2006-10-1.

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.FromString("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>";
}

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

The front end Default.aspx page looks something like this:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>

<body>
<form id="form1" runat="server">
<div>
<fieldset>
<legend>rdfdf</legend>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Add" Width="70px" />
<asp:Button ID="Button2" runat="server" Text="View" Width="84px" />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="" Width="138px"></asp:Label>
</fieldset>
</div>
</form>
</body>
</html>

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.

The flow for the code behind page is as follows

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.FromString("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>";
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

No comments:

Post a Comment