Passing session variable from page to page

83,675

You say that you've set the PostBackUrl to your second page. If you're going to do it that way, you need to use Page.PreviousPage to get access to your textbox. But this is the easiest way:

Firstly, leave the PostBackUrl alone. Setting the PostBackUrl to your second page means that you're telling the SECOND PAGE to handle your button click, not the first page. Hence, your session variable never gets set, and is null when you try to pull it.

This should work for ya.

And yes, you can also do this with a QueryString, but if its something that you don't want the user to see/edit, then a Session variable is better.

 protected void submit_Click(object sender, EventArgs e)
 {
      string name = txtFirstName.Text.Trim();
      Session["name"] = name;
      Response.Redirect("PageTwo.aspx");
 }

Then in the second page (You don't REALLY need the ToString()):

 protected void Page_Load(object sender, EventArgs e)
 {
      if (Session["name"] != null)
      {
           lblName.Text = Session["name"].ToString();
      }
 }

EDIT -- Make sure that your button click actually gets fired. Someone can correct me wrong on this, as I do most of my work in VB.NET, not C#. But if you don't specify the OnClick value, your function won't get called.

 <asp:Button ID="Button1" runat="server" Text="Click Me!" OnClick="submit_Click" />
Share:
83,675
Troy Bryant
Author by

Troy Bryant

I'm just looking to get better and help other because so many have helped me thus far.

Updated on April 21, 2020

Comments

  • Troy Bryant
    Troy Bryant about 4 years

    I'm wondering what is my issue on passing a variable from page to page using asp.net session.

    I've stripped the code down to just one text box to see whats going on. I'm just trying to take the value of a text box and display it on a confirmation page. When the button is clicked it transfers me to the second page but there label is blank. Yes my post back url is pointing to the second page.

    Here is the button click:

    protected void submit_Click(object sender, EventArgs e)
    {
        string name = txtFirstName.Text.Trim();
        Session["name"] = name;
    }
    

    Here is the page load of the second page:

    protected void Page_Load(object sender, EventArgs e)
    {
        lblName.Text = (string)(Session["name"]);
    }
    

    Unless I've been looking at this to long and missed something. I've already read "How to: Read Values from Session State" from MSDN.

    • IrishChieftain
      IrishChieftain over 10 years
      Did you set "enableSessionState" to true in the Page directive?
    • Lost_Cause
      Lost_Cause over 10 years
      What if you try to call .ToString() instead of casting the Session["name"] ?
    • Troy Bryant
      Troy Bryant over 10 years
      I tried the .ToString and was getting Object reference not set to an instance of an object error so moved on from that method.
    • CaptainStealthy
      CaptainStealthy over 10 years
      The reason you got the object reference is because your Session variable was never actually set, and hence was null. See my answer. :)
    • Servy
      Servy over 10 years
      Seems like a place where you should just be passing a query string, rather than using session. You should generally strive to be stateless, whenever possible, when using a web application.
    • Troy Bryant
      Troy Bryant over 10 years
      enableSessionState I did that also and still just a blank page.
    • CaptainStealthy
      CaptainStealthy over 10 years
      Servy, you have a point. But if you want the user to not view/edit the data after submission, then a QueryString won't work. Troy, try the code that I posted and see if that works.
    • Servy
      Servy over 10 years
      @Stealth22 You can post the data, rather than using a get parameter, or you can do a server side transfer to the other page rather than a client side transfer. Either way, the data isn't logically stateful, so using persistent state for it isn't really proper.
    • CaptainStealthy
      CaptainStealthy over 10 years
      Yeah, you've got a point there. Can't say that I disagree.
  • Xenolightning
    Xenolightning over 10 years
    +1... The PostBackUrl will cause the submit_Click event not to fire. Also instead of null checking the Session you can write: lblName.Text = Session["name"] as string, which is a little tidier (IMO).
  • CaptainStealthy
    CaptainStealthy over 10 years
    I stand corrected, then. I had assumed that it would crash regardless if the value was null. But now that I think of it, I believe you're right. I normally do this just to be extra paranoid, but that would probably worked.
  • Troy Bryant
    Troy Bryant over 10 years
    This worked after removing my post back url and actually setting setting / checking the session got what I was looking for. Thank you for the help.
  • CaptainStealthy
    CaptainStealthy over 10 years
    No worries! Glad I could be of help!
  • Xenolightning
    Xenolightning over 10 years
    As an added extra here: The PostBackUrl will cause the form to be posted to the page specified. So you could achieve the same result using PostBackUrl="Page2.aspx" and in Page2.Page_Load, inspect Request.Form for the value of the TextBox.
  • CaptainStealthy
    CaptainStealthy over 10 years
    @Xenolightning, come to think of it, that's probably the better way of doing it, as per Servy's comment above.
  • João Mendes
    João Mendes almost 10 years
    This is basically the same as an answer already on the page. It really adds nothing to the discussion.