Online Sample: http://area23.at/default/Stateful.aspx |
Code:
<form id="form1" runat="server"><asp:Literal ID="Literal1" runat="server"></asp:Literal>
<br />
CookieValue: <asp:TextBox ID="TextBox1" runat="server" MaxLength="4" Text="0"></asp:TextBox>
<br />
SessionValue: <asp:TextBox ID="TextBox2" runat="server" MaxLength="4" Text="0"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="PostBack Button" />
<br />
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/" >Redirect Link</asp:HyperLink>
</form>
protected void Page_Load(object sender, EventArgs e) {
int counter = 0;
this.Literal1.Text = (this.Page.IsPostBack) ?
"Page is postback!" : "This is new request without postback";
if (this.Page.Session != null) {
if (this.Page.Request.Cookies[this.Page.Session.SessionID] != null) {
this.TextBox1.Text =
this.Page.Request.Cookies[this.Page.Session.SessionID].Value;
}
if (this.Page.Session["SequenceEventCounter"] != null) {
this.TextBox2.Text =
this.Page.Session["SequenceEventCounter"]).ToString();
counter = (int)this.Page.Session["SequenceEventCounter"];
}
this.Page.Session["SequenceEventCounter"] = (int)(++counter);
}
HttpCookie cookie = new HttpCookie(
HttpContext.Current.Session.SessionID, counter.ToString());
HttpContext.Current.Response.AppendCookie(cookie);
}
Keine Kommentare:
Kommentar veröffentlichen