ASP.NET Tutorial/Cookie/Read — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 14:57, 26 мая 2010
List all cookies contained in the Request.Cookies collection
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
void Page_Load() { ArrayList colCookies = new ArrayList(); for (int i = 0; i < Request.Cookies.Count; i++) colCookies.Add(Request.Cookies[i]); grdCookies.DataSource = colCookies; grdCookies.DataBind(); }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Get All Cookies</title>
</head> <body>
<form id="form1" runat="server">
<asp:GridView id="grdCookies" Runat="server"/>
</form>
</body> </html></source>
Read cookie, if not there, create one (C#)
<source lang="csharp">
File: Default.ascx <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="CookieExample" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<title>Untitled Page</title>
</head> <body>
<form id="form1" runat="server">
<asp:Label ID="lblWelcome" runat="server" EnableViewState="False" ></asp:Label>
Name: <asp:TextBox ID="txtName" runat="server" Width="178px"></asp:TextBox> <asp:Button ID="cmdStore" runat="server" OnClick="cmdStore_Click" Text="Create Cookie" />
</form>
</body> </html> File: Default.ascx.cs using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class CookieExample : System.Web.UI.Page {
protected void Page_Load(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies["Preferences"]; if (cookie == null) { lblWelcome.Text = "Unknown Customer"; } else { lblWelcome.Text = "Cookie Found.
"; lblWelcome.Text += "Welcome, " + cookie["Name"]; } } protected void cmdStore_Click(object sender, EventArgs e) { HttpCookie cookie = Request.Cookies["Preferences"]; if (cookie == null) { cookie = new HttpCookie("Preferences"); } cookie["Name"] = txtName.Text; cookie.Expires = DateTime.Now.AddYears(1); Response.Cookies.Add(cookie); lblWelcome.Text = "Cookie Created.
"; lblWelcome.Text += "New Customer: " + cookie["Name"]; }
}</source>
Reading Cookies
<source lang="csharp">
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">
void Page_Load() { if (Request.Cookies["message"] != null) lblCookieValue.Text = Request.Cookies["message"].Value; }
</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">
<title>Get Cookie</title>
</head> <body>
<form id="form1" runat="server">
The value of the message cookie is: <asp:Label id="lblCookieValue" Runat="server" />
</form>
</body> </html></source>