Results 1 to 6 of 6

Thread: [RESOLVED] [2005] Preserving form data while navigating between webpages

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    79

    Resolved [RESOLVED] [2005] Preserving form data while navigating between webpages

    I've spent half-the-morning working on this and I still can't find an answer anywhere. Please note this is a simplified example, the real project has tabs and customised sidemenus as well.

    I have 2 webpages - test.aspx (with a textbox and 'Next' button) and target.aspx (with a 'Back' button).

    TEST.ASPX -
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Next" OnClick="Navigate_Next" />
        </div>
        </form>
    </body>
    </html>
    TEST.ASPX.CS -
    Code:
    using System;
    
    public partial class test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            TextBox1.Text = Session["surname"].ToString();
        }
    
        protected void Navigate_Next(object sender, EventArgs e)
        {
            Session["surname"] = TextBox1.Text;
            Server.Transfer("target.aspx");
        }
    }

    TARGET.ASPX -
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="target.aspx.cs" Inherits="target" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <h1>Target Page</h1>
            <br />
            <asp:Button ID="Button1" runat="server" Text="Back" OnClick="Navigate_Next" />
        </div>
        </form>
    </body>
    </html>

    TARGET.ASPX.CS -
    Code:
    using System;
    
    public partial class target : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void Navigate_Next(object sender, EventArgs e)
        {
            //Master.Authnavclass = "selectedTab";
            Server.Transfer("test.aspx");
        }
    }
    When the user navigates from test.aspx to target.aspx and back, I want them to still see the value they typed into the test.aspx textbox. I've tried using Session variables to resolve this issue, but that isn't working because the test.aspx's Page_Load event is invoked before the Navigate_Next event. So the textbox value is getting deleted before it can be saved.

    My question is - does anyone know a way to preserve a form's values when they're navigating back and forth between separate webpages?
    "Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
    - Brian Hendrix

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] Preserving form data while navigating between webpages

    You need to use javascript for this... It's pretty simple, just set the button's onclick="history.back()" if you want to go back or "history.forward()" if you want to go forward. Check here for more details:
    http://www.pageresource.com/jscript/jhist.htm

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Preserving form data while navigating between webpages

    I suggest you do a search and read up on cross page postbacks (in asp.net of course) for this. You may be able to post from test to target and target to test and not lose the values.

    If it does still get erased, then either use javascript to throw the user back one page, or have a method in test.aspx which reads from a session variable containing all those values you need.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    79

    Re: [2005] Preserving form data while navigating between webpages

    Quote Originally Posted by mendhak
    I suggest you do a search and read up on cross page postbacks (in asp.net of course) for this. You may be able to post from test to target and target to test and not lose the values.

    If it does still get erased, then either use javascript to throw the user back one page, or have a method in test.aspx which reads from a session variable containing all those values you need.
    Mendhak, thanks for the suggestions. I've already tried using session variables. The problem is that when the test.aspx 'Next' button is pressed, the Page_Load function is invoked before the Navigate_Next() method is called. Because Page_Load is used to load the save session variables, the new session variable values are never saved.

    Stanav, your suggestion works perfectly for the problem.
    Last edited by Spacehamster; Sep 16th, 2007 at 06:22 PM.
    "Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
    - Brian Hendrix

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [RESOLVED] [2005] Preserving form data while navigating between webpages

    Aah, you missed out on but one simple statement.

    Code:
    If Not Page.IsPostBack Then
    'Populate the session variables
    End If
    And the next time, the session variables would not be overwritten.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2006
    Posts
    79

    Re: [RESOLVED] [2005] Preserving form data while navigating between webpages

    Quote Originally Posted by mendhak
    Aah, you missed out on but one simple statement.

    Code:
    If Not Page.IsPostBack Then
    'Populate the session variables
    End If
    And the next time, the session variables would not be overwritten.
    DOH!
    "Intelligent people may or may not appear Smart. Smart people may be Intelligent, but don’t bet on it. Intelligent and Smart people do Stupid things on occasion. Stupid people are not Intelligent. Everyone is ignorant. Who needs a drink?"
    - Brian Hendrix

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width