locked
Using Session Variables Between Pages RRS feed

  • Question

  • User564995064 posted

    I have an application in which my form is 2 pages.  When the 'continue' button is hit, it sets the session variables for the page 1.  On page 2, I have a 'previous' button.  When this button is hit, I want to fill in the textboxes on page 1 with the session variables.  No matter what I try, it is clearing the textboxes.  What am I doing wrong?  Here is my code:

            protected void Page_Load(object sender, EventArgs e)
            {
    
                if (!IsPostBack)
                {
                    txtSSNO.Text = Session["ssno"].ToString();
                    txtFirst_name.Text = Session["firstname"].ToString();
                }
               
            }
    
            protected void btnContinue_Click(object sender, EventArgs e)
            {
                Session["ssno"] = txtSSNO.Text;
                Session["firstname"] = txtFirst_name.Text;
    
                Response.Redirect("Page2.aspx");
            }
    

    Thank you in advance!

    Thursday, March 3, 2016 7:27 PM

Answers

  • User281315223 posted

    On the second page, I have a 'previous' button.  If a user clicks the 'previous' button on page 2, I want to display the information they have already entered on page 1 in the correct textboxes.  However, this information will not be displayed on page 2.  Does this make sense?

    Sure. If that's the case then it sounds like everything just needs to be handled in your Page1.aspx.cs file :

    public partial class Page1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    TextBoxA.Text = Convert.ToString(Session["A"]);
                    TextBoxB.Text = Convert.ToString(Session["B"]);
                    TextBoxC.Text = Convert.ToString(Session["C"]);
                    TextBoxD.Text = Convert.ToString(Session["D"]);
                }
            }
    
            protected void NavigateToPage2_Click(object sender, EventArgs e)
            {
                Session["A"] = TextBoxA.Text;
                Session["B"] = TextBoxB.Text;
                Session["C"] = TextBoxC.Text;
                Session["D"] = TextBoxD.Text;
                Response.Redirect("Page2.aspx");
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2016 2:53 PM

All replies

  • User281315223 posted

    The code should be working, you can see the following example below which is working just as expected :

    Page1.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page1.aspx.cs" Inherits="Example.Page1" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Page 1</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:TextBox ID="ExampleTextBox" runat="server"></asp:TextBox>
            <asp:Button ID="NavigateToPage2" runat="server" Text="Navigate to Page 2" OnClick="NavigateToPage2_Click" />
        </form>
    </body>
    </html>

    Page1.aspx.cs

    using System;
    
    namespace Example
    {
        public partial class Page1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    ExampleTextBox.Text = Convert.ToString(Session["Text"]);
                }
            }
    
            protected void NavigateToPage2_Click(object sender, EventArgs e)
            {
                Session["Text"] = ExampleTextBox.Text;
                Response.Redirect("Page2.aspx");
            }
        }
    }

    Page2.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Page2.aspx.cs" Inherits="Example.Page2" %>
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Page 2</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:TextBox ID="FromSessionTextBox" runat="server"></asp:TextBox>
            <asp:Button ID="NavigateToPage1" runat="server" Text="Navigate to Page 1" OnClick="NavigateToPage1_Click" />
        </form>
    </body>
    </html>

    Page2.aspx.cs

    using System;
    
    namespace Example
    {
        public partial class Page2 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    FromSessionTextBox.Text = Convert.ToString(Session["Text"]);
                }
            }
    
            protected void NavigateToPage1_Click(object sender, EventArgs e)
            {
                Response.Redirect("Page1.aspx");
            }
        }
    }

    It appears to work as you would expect. You might consider placing breakpoints in all of the affected methods to ensure they are being called properly and use the debugger to check your values during those times : 

    Thursday, March 3, 2016 8:04 PM
  • User564995064 posted

    My code does work.  However, when it returns back to page 1, it clears out the textbox.  Just for clarification, I have a 2 page form.  The first page has demographic information (ssn, name, address, city, state, zip).  The second page will have different information.  On the second page, I have a 'previous' button.  If a user clicks the 'previous' button on page 2, I want to display the information they have already entered on page 1 in the correct textboxes.  However, this information will not be displayed on page 2.  Does this make sense?

    Thursday, March 3, 2016 8:38 PM
  • User-2057865890 posted

    Hi rhondadunn,

    You could refer to the following sample.

    Page1.aspx

    <form id="form1" runat="server">
            <h3>Page1</h3>
            <div>
    
                <div>Name:
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox></div>
                <div>Address:
                    <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox></div>
    
                <div>City:
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox></div>
            </div>
            <asp:Button ID="btnGoToPage2" runat="server" Text="Go To Page2" OnClick="btnGoToPage2_Click" />
        </form>

    code behind

    public partial class Page1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    if (Session["Demo"] != null)
                    {
                        var demo = (DemoClass)Session["Demo"];
                        txtAddress.Text = demo.Address;
                        txtCity.Text = demo.City;
                        txtName.Text = demo.Name;
                    }
                }
            }
    
            protected void btnGoToPage2_Click(object sender, EventArgs e)
            {
                var demo = new DemoClass();
                demo.Address = txtAddress.Text;
                demo.City = txtCity.Text;
                demo.Name = txtName.Text;
                Session["Demo"] = demo;
                Response.Redirect("Page2.aspx");
            }
        }

    Page2.aspx

    <form id="form1" runat="server">
            <h3>Page2</h3>
            <div>
    
                <div>
                    <asp:Button ID="btnPrevious" runat="server" Text="Previous" OnClick="btnPrevious_Click" />
                </div>
                <div>
                    <asp:Button ID="Button2" runat="server" Text="Go To Page1" OnClick="Button2_Click" />
                </div>
                <div>
                    Name:
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </div>
                <div>
                    Address:
                    <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                </div>
    
                <div>
                    City:
                    <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
                </div>
            </div>
        </form>

    code behind

    protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnPrevious_Click(object sender, EventArgs e)
            {
                if (Session["Demo"] != null)
                {
                    var demo = (DemoClass)Session["Demo"];
                    txtAddress.Text = demo.Address;
                    txtCity.Text = demo.City;
                    txtName.Text = demo.Name;
                }
    
            }
    
            protected void Button2_Click(object sender, EventArgs e)
            {
                Response.Redirect("Page1.aspx");
            }

    Class

    public class DemoClass
        {
            public string Address { get; set; }
            public string Name { get; set; }
            public string City { get; set; }
        }

    Best Regards,

    Chris Zhao

    Friday, March 4, 2016 9:40 AM
  • User281315223 posted

    On the second page, I have a 'previous' button.  If a user clicks the 'previous' button on page 2, I want to display the information they have already entered on page 1 in the correct textboxes.  However, this information will not be displayed on page 2.  Does this make sense?

    Sure. If that's the case then it sounds like everything just needs to be handled in your Page1.aspx.cs file :

    public partial class Page1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    TextBoxA.Text = Convert.ToString(Session["A"]);
                    TextBoxB.Text = Convert.ToString(Session["B"]);
                    TextBoxC.Text = Convert.ToString(Session["C"]);
                    TextBoxD.Text = Convert.ToString(Session["D"]);
                }
            }
    
            protected void NavigateToPage2_Click(object sender, EventArgs e)
            {
                Session["A"] = TextBoxA.Text;
                Session["B"] = TextBoxB.Text;
                Session["C"] = TextBoxC.Text;
                Session["D"] = TextBoxD.Text;
                Response.Redirect("Page2.aspx");
            }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, March 4, 2016 2:53 PM