blog.darkstar.work - a simple url encoder/decoder

 a simple url encoder/decoder
 http://blog.darkstar.work

Labels

Wirtschaft (152) Pressefreiheit (131) Österreich (123) IT (98) code (62) Staatsschulden (37) EZB (27) Pensionssystem (16)

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