[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?
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
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.
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.
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. ;)
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! :eek: