Nov 10, 2011

How to use cookies


Create a page named page1.aspx, and then drag a button and a TextBox onto the page. Double click the button and then add the following code:
protected void Button1_Click(object sender, EventArgs e)
{
    HttpCookie cookie = new HttpCookie("UserName");
    cookie.Value = TextBox1.Text;
    cookie.Expires = DateTime.Now.AddDays(1);
    Response.Cookies.Add(cookie);
    Response.Redirect("Page2.aspx");
}
page1.aspx, 
<div>
     <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
On page2.aspx, double click the form and put the following code in it:
protected void Page_Load(object sender, EventArgs e)
{
    if (Request.Cookies["UserName"] != null)
        Response.Write(Request.Cookies["UserName"].Value);
}

Related posts:

0 comments:

Post a Comment

Your comments:

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More