locked
Session Variable not persisting when button pressed in aspx RRS feed

  • Question

  • ASPX:

    <asp:Button class="testBtn" id="testBtn" Text="Test Variable" runat="server" OnClick="test_Click"/>

    C#:

    protected void Page_Load(object sender, EventArgs e)
        {
            //Session Variables START
            Session["Name"] = null;
            //Session Variables END
    
            if (!IsPostBack)
            {   
             Session["Name"] = "tester";
            }
            else
            {
            //
            }
        }
    
         protected void test_Click(object sender, EventArgs e)
        {
            string test1 = (string)Session["Name"];
            
    Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + test1 + "');", true);
        }
    
    Any help would be greatly appreciated. For whatever reason I cannot get the variable to stick.

    Friday, January 8, 2016 1:54 PM

Answers

  • Figured out that if I use Session variables I do not need to worry about !IsPostBack and containing them in there was the problem.

    Resolution: Take it out of the !IsPostBack statement

    • Marked as answer by DevMethods Friday, January 8, 2016 2:01 PM
    Friday, January 8, 2016 2:01 PM

All replies

  • Any help would be greatly appreciated

    http://forums.asp.net/

    Friday, January 8, 2016 2:01 PM
  • Figured out that if I use Session variables I do not need to worry about !IsPostBack and containing them in there was the problem.

    Resolution: Take it out of the !IsPostBack statement

    • Marked as answer by DevMethods Friday, January 8, 2016 2:01 PM
    Friday, January 8, 2016 2:01 PM
  • Figured out that if I use Session variables I do not need to worry about !IsPostBack and containing them in there was the problem.

    Resolution: Take it out of the !IsPostBack statement

    http://forums.asp.net/


    • Proposed as answer by DA924 Friday, January 8, 2016 2:06 PM
    Friday, January 8, 2016 2:06 PM