blog.area23.at - a simple url encoder/decoder

 a simple url encoder/decoder
 http://blog.area23.at

Labels

Wirtschaft (121) Österreich (93) Pressefreiheit (67) IT (47) Staatsschulden (31) code (25) EZB (19) Pensionssystem (14) France (1) music (1)

2013-05-17

ASP.NET C# - Stateful session by cookie

Online Sample: http://area23.at/default/Stateful.aspx
Attention, Request Timeouts and ApplicationError Events are not handled in this litte sample!

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