how use cookies in asp.net?

Sagarp
Posted by Sagarp under ASP.NET category on | Views : 6706
%@ Page language="c#" Codebehind="coole.aspx.cs" AutoEventWireup="false" Inherits="day8.coole" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>coole</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout" background="Metal Weave.bmp">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 264px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox>
<asp:AdRotator id="AdRotator1" style="Z-INDEX: 111; LEFT: 8px; POSITION: absolute; TOP: 8px" runat="server"
Width="81px" Height="24px" AdvertisementFile="X.xml"></asp:AdRotator>
<asp:Label id="Label5" style="Z-INDEX: 110; LEFT: 552px; POSITION: absolute; TOP: 128px" runat="server"
ForeColor="DarkOliveGreen" Font-Bold="True" BackColor="Yellow">COLOR</asp:Label>
<asp:DropDownList id="DropDownList2" style="Z-INDEX: 109; LEFT: 480px; POSITION: absolute; TOP: 176px"
runat="server" Width="208px" AutoPostBack="True">
<asp:ListItem Value="Yellow">Yellow</asp:ListItem>
<asp:ListItem Value="Green">Green</asp:ListItem>
<asp:ListItem Value="Pink">Pink</asp:ListItem>
<asp:ListItem Value="Red">Red</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:Label id="Label4" style="Z-INDEX: 108; LEFT: 544px; POSITION: absolute; TOP: 48px" runat="server"
BackColor="Yellow" Font-Bold="True" ForeColor="DarkOliveGreen">FONT</asp:Label>
<asp:DropDownList id="DropDownList1" style="Z-INDEX: 107; LEFT: 472px; POSITION: absolute; TOP: 80px"
runat="server" Width="216px" AutoPostBack="True"></asp:DropDownList>
<asp:Label id="Label3" style="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 128px" runat="server"
ForeColor="DarkGreen" Font-Bold="True" BackColor="Yellow">POST</asp:Label>
<asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 168px; POSITION: absolute; TOP: 80px" runat="server"
ForeColor="DarkOliveGreen" Font-Bold="True" BackColor="Yellow">NAME</asp:Label>
<asp:Button id="Button1" style="Z-INDEX: 104; LEFT: 264px; POSITION: absolute; TOP: 184px" runat="server"
Width="168px" Font-Bold="True" Text="COOKIES"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 232px; POSITION: absolute; TOP: 24px" runat="server"
Width="192px" ForeColor="Yellow" Font-Bold="True" BackColor="#404000">COOKIES APPLICATION</asp:Label>
<asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 264px; POSITION: absolute; TOP: 128px"
runat="server"></asp:TextBox>
</form>
</body>
</HTML>

coole.aspx.cs
private void Button1_Click(object sender, System.EventArgs e)
{
HttpCookie sp=new HttpCookie("cookies");

sp.Values.Add("name",this.TextBox1.Text);
sp.Values.Add("post",this.TextBox2.Text);
sp.Values.Add("font",this.DropDownList1.SelectedItem.Value);
sp.Values.Add("color",this.DropDownList2.SelectedItem.Value);
//sp.Expires=DateTime.Now.AddSeconds(5);
Response.Cookies.Add(sp);
Response.Redirect("WebForm4.aspx");
}

WebForm4.aspx.cs

private void Button4_Click(object sender, System.EventArgs e)
{
this.Label3.Font.Name=Request.Cookies["cookies"].Values["font"].ToString();
this.Label4.Font.Name=Request.Cookies["cookies"].Values["font"].ToString();

}

private void Button5_Click(object sender, System.EventArgs e)
{

this.Label3.ForeColor=Color.FromName(Request.Cookies["cookies"].Values["color"].ToString());
this.Label4.ForeColor=Color.FromName(Request.Cookies["cookies"].Values["color"].ToString());
}
}

Comments or Responses

Login to post response