Friday, 10 February 2012

HOW TO READ COOKIES VALUES IN DOT NET


 
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

protected void Page_Load(object sender, System.EventArgs e) {
if (!this.IsPostBack)
{
HttpCookie infoCookie = new HttpCookie("Info");
infoCookie["Country"] = "USA";
infoCookie["City"] = "NewYork";
infoCookie["Name"] = "Jenny";
infoCookie.Expires = DateTime.Now.AddDays(7);
Response.Cookies.Add(infoCookie);
}
}
protected void Button1_Click(object sender, System.EventArgs e) {

HttpCookie cookie = Request.Cookies["Info"];
if (cookie != null)

{
string country = cookie["Country"];
string city = cookie["City"];
string name = cookie["Name"];
Label1.Text = "Cookie[Info] Found and read<br/>";
Label1.Text += "Name: " + name;
Label1.Text += "<br />Country: " + country;
Label1.Text += "<br />City: " + city;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">

<head id="Head1" runat="server">
<title>asp.net cookie example: how to read a cookie</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Teal">asp.net Cookie example: Read a cookie</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Large"
ForeColor="DarkGreen">
</asp:Label>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Text="Read Cookie"
OnClick="Button1_Click"
Font-Bold="true"
ForeColor="HotPink"
/>
</div>
</form>
</body>
</html>


No comments:

Post a Comment