Powered By Blogger

Wednesday, December 30, 2009

cookies in asp.net example c#


Cookies in asp.net example c#

cookie.aspx


<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btn_cookie" runat="server" OnClick="btn_cookie_Click" Text="Create Cookie" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:Button ID="btn_viewcookie" runat="server" OnClick="btn_viewcookie_Click" Text="View Cookie" />

cookie.aspx.cs


HttpCookie cook;

protected void btn_cookie_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
cook = new HttpCookie("username");
cook.Value = TextBox1.Text.Trim();
cook.Expires = DateTime.Now.AddMinutes(1);
Response.Cookies.Add(cook);
}
else
Response.Write("Enter Value");
}
protected void btn_viewcookie_Click(object sender, EventArgs e)
{
cook = Request.Cookies["username"];

if (cook != null)
{
Label1.Text = cook.Value.ToString();
}
else
{
Label1.Text = "Cookie Not Available or Expired.";
}
}





No comments:

Post a Comment