Code:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
HttpCookie testCokkie = new HttpCookie("ExampleCookie");
testCokkie.Expires = DateTime.Now.AddDays(5);
Response.Cookies.Add(testCokkie);
}
}
protected void Button1_Click(object sender, System.EventArgs e)
{
HttpCookie ExampleCookie = Request.Cookies["ExampleCookie"];
ExampleCookie["Name"] = TextBox1.Text.ToString();
ExampleCookie["City"] = TextBox2.Text.ToString();
Response.Cookies.Add(ExampleCookie);
}
protected void Button2_Click(object sender, System.EventArgs e)
{
HttpCookie exampleCookie = Request.Cookies["ExampleCookie"];
if (exampleCookie != null)
{
string name = exampleCookie["Name"];
string city = exampleCookie["City"];
label1.Text = "Cookie found and read<br />";
label1.Text += "Name: " + name + "<br />City: " + city;
}
}
</script>
<div>
<center>
<asp:Label ID="lbltitle" runat="server" Text="asp.net cookie example:" Font-Bold="true"
Font-Size="XX-Large"></asp:Label>
<br />
<asp:Label ID="label1" runat="server" Font-Size="Large" ForeColor="SeaGreen" Font-Italic="true">
</asp:Label>
<br />
<br />
<table>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Name">
</asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="City">
</asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Set cookie value" OnClick="Button1_Click"
Font-Bold="true" ForeColor="Crimson" />
<asp:Button ID="Button2" runat="server" Text="Read cookie" OnClick="Button2_Click"
Font-Bold="true" ForeColor="DarkGreen" />
</td>
</tr>
</table>
</center>
</div>
</asp:Content>
Output:
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
{
HttpCookie testCokkie = new HttpCookie("ExampleCookie");
testCokkie.Expires = DateTime.Now.AddDays(5);
Response.Cookies.Add(testCokkie);
}
}
protected void Button1_Click(object sender, System.EventArgs e)
{
HttpCookie ExampleCookie = Request.Cookies["ExampleCookie"];
ExampleCookie["Name"] = TextBox1.Text.ToString();
ExampleCookie["City"] = TextBox2.Text.ToString();
Response.Cookies.Add(ExampleCookie);
}
protected void Button2_Click(object sender, System.EventArgs e)
{
HttpCookie exampleCookie = Request.Cookies["ExampleCookie"];
if (exampleCookie != null)
{
string name = exampleCookie["Name"];
string city = exampleCookie["City"];
label1.Text = "Cookie found and read<br />";
label1.Text += "Name: " + name + "<br />City: " + city;
}
}
</script>
<div>
<center>
<asp:Label ID="lbltitle" runat="server" Text="asp.net cookie example:" Font-Bold="true"
Font-Size="XX-Large"></asp:Label>
<br />
<asp:Label ID="label1" runat="server" Font-Size="Large" ForeColor="SeaGreen" Font-Italic="true">
</asp:Label>
<br />
<br />
<table>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Name">
</asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label3" runat="server" Text="City">
</asp:Label>
</td>
<td>
<asp:TextBox ID="TextBox2" runat="server">
</asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="Button1" runat="server" Text="Set cookie value" OnClick="Button1_Click"
Font-Bold="true" ForeColor="Crimson" />
<asp:Button ID="Button2" runat="server" Text="Read cookie" OnClick="Button2_Click"
Font-Bold="true" ForeColor="DarkGreen" />
</td>
</tr>
</table>
</center>
</div>
</asp:Content>
Output:
No comments:
Post a Comment