Cookies in asp.net
Just like QueryStrings, Cookies can also be used to send data from one webform to another. In general, web sites use cookies to store user preferences or other information that is client-specific. Cookies store small amounts of information on the client’s machine.
Cookies can be broadly classified into 2 types
1. Persistent cookies - Remain on the client computer, even after the browser is closed. You can configure how long the cookies remain using the expires property of the HttpCookie object.
2. Non-Persistent cookies - If you don't set the Expires property, then the cookie is called as a Non-Persistent cookie. Non-Persistent cookies only remain in memory until the browser is closed.
On WebForm1.aspx, user enters Name and Email. Let's write these values on to the client's computer using cookies. Finally read the values from the cookie and display them in WebForm2.aspx.
WebForm1.aspx HTML source:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSendData" runat="server"
Text="Go to WebForm2" onclick="btnSendData_Click" />
</td>
</tr>
</table>
</div>
WebForm1.aspx.cs code:
protected void btnSendData_Click(object sender, EventArgs e)
{
// Create the cookie object
HttpCookie cookie = new HttpCookie("UserDetails");
cookie["Name"] = txtName.Text;
cookie["Email"] = txtEmail.Text;
// Cookie will be persisted for 30 days
cookie.Expires = DateTime.Now.AddDays(30);
// Add the cookie to the client machine
Response.Cookies.Add(cookie);
Response.Redirect("WebForm2.aspx");
}
WebForm2.aspx HTML Source:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
</table>
</div>
WebForm2.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["UserDetails"];
if (cookie != null)
{
lblName.Text = cookie["Name"];
lblEmail.Text = cookie["Email"];
}
}
Cookies can be broadly classified into 2 types
1. Persistent cookies - Remain on the client computer, even after the browser is closed. You can configure how long the cookies remain using the expires property of the HttpCookie object.
2. Non-Persistent cookies - If you don't set the Expires property, then the cookie is called as a Non-Persistent cookie. Non-Persistent cookies only remain in memory until the browser is closed.
On WebForm1.aspx, user enters Name and Email. Let's write these values on to the client's computer using cookies. Finally read the values from the cookie and display them in WebForm2.aspx.
WebForm1.aspx HTML source:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>
This is WebForm1</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:TextBox ID="txtName" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:TextBox ID="txtEmail" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSendData" runat="server"
Text="Go to WebForm2" onclick="btnSendData_Click" />
</td>
</tr>
</table>
</div>
WebForm1.aspx.cs code:
protected void btnSendData_Click(object sender, EventArgs e)
{
// Create the cookie object
HttpCookie cookie = new HttpCookie("UserDetails");
cookie["Name"] = txtName.Text;
cookie["Email"] = txtEmail.Text;
// Cookie will be persisted for 30 days
cookie.Expires = DateTime.Now.AddDays(30);
// Add the cookie to the client machine
Response.Cookies.Add(cookie);
Response.Redirect("WebForm2.aspx");
}
WebForm2.aspx HTML Source:
<div style="font-family: Arial">
<table>
<tr>
<td colspan="2">
<h1>This is WebForm2</h1>
</td>
</tr>
<tr>
<td>
<b>Name</b>
</td>
<td>
:<asp:Label ID="lblName" runat="server">
</asp:Label>
</td>
</tr>
<tr>
<td>
<b>Email</b>
</td>
<td>
:<asp:Label ID="lblEmail" runat="server">
</asp:Label>
</td>
</tr>
</table>
</div>
WebForm2.aspx.cs Code:
protected void Page_Load(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies["UserDetails"];
if (cookie != null)
{
lblName.Text = cookie["Name"];
lblEmail.Text = cookie["Email"];
}
}