Monday, December 31, 2007

Maintaining Control Values Between Pages

Here is a quick example of how you can maintain the values of controls when switching back and forth between different web pages by making use of Session variables. You can use ASP.NET Session variables to pass the values from one web page to another, but if you need to return to the previous page via a button for example the web page will loose the previous values it had. You can maintain those values just as if you had clicked your browser's back button by using Session variables.

Below is a short example that demonstrates this.

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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:Label ID="FNLabel" runat="server" Text="First Name"></asp:Label>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
<asp:Label ID="LNLabel" runat="server" Text="Last Name"></asp:Label><br />
<asp:TextBox ID="FNTextBox" runat="server"></asp:TextBox>
<asp:TextBox ID="LNTextBox" runat="server"></asp:TextBox>&nbsp;<br />
<br />
<asp:DropDownList ID="IncomeDropDownList" runat="server" AutoPostBack="True">
<asp:ListItem>10K -30K</asp:ListItem>
<asp:ListItem>31K - 50K</asp:ListItem>
<asp:ListItem>51K - 70K</asp:ListItem>
<asp:ListItem>71K - 90K</asp:ListItem>
<asp:ListItem>91K+</asp:ListItem>
</asp:DropDownList><br />
<br />
<asp:RadioButtonList ID="ResidenceRadioButtonList" runat="server" AutoPostBack="True">
<asp:ListItem>Apartment</asp:ListItem>
<asp:ListItem>Mobile Home</asp:ListItem>
<asp:ListItem>Townhouse</asp:ListItem>
<asp:ListItem>Single Family House</asp:ListItem>
</asp:RadioButtonList><br />
<asp:Calendar ID="AcctStartCalendar" runat="server" BackColor="White" BorderColor="#999999" CellPadding="4" DayNameFormat="Shortest" Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" Height="180px" Width="200px">
<SelectedDayStyle BackColor="Green" Font-Bold="True" ForeColor="White" />
<TodayDayStyle BackColor="#CCCCCC" ForeColor="Black" />
<SelectorStyle BackColor="#CCCCCC" />
<WeekendDayStyle BackColor="#FFFFCC" />
<OtherMonthDayStyle ForeColor="#808080" />
<NextPrevStyle VerticalAlign="Bottom" />
<DayHeaderStyle BackColor="#CCCCCC" Font-Bold="True" Font-Size="7pt" />
<TitleStyle BackColor="#999999" BorderColor="Black" Font-Bold="True" />
</asp:Calendar>
<br />
<asp:Button ID="SubmitButton"
runat="server" OnClick="Button1_Click" Text="Submit" /></div>
<br />
</form>
</body>
</html>
Default.aspx.cs
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
LoadExistingValues();
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["value1"] = FNTextBox.Text;
Session["value2"] = LNTextBox.Text;
Session["value3"] = IncomeDropDownList.SelectedValue;
Session["value4"] = ResidenceRadioButtonList.SelectedValue;
Session["value5"] = AcctStartCalendar.SelectedDate;

Response.Redirect("~/Page2.aspx");
}

private void LoadExistingValues()
{
if (!Page.IsPostBack)
{
if (Session["value1"] != null)
{
FNTextBox.Text = Session["value1"].ToString();
LNTextBox.Text = Session["value2"].ToString();
IncomeDropDownList.SelectedValue = Session["value3"].ToString();
ResidenceRadioButtonList.SelectedValue = Session["value4"].ToString();

AcctStartCalendar.SelectedDate = Convert.ToDateTime((Session["value5"]));
}
}
}
}
Page2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Page2.aspx.cs" Inherits="Page2" %>

<!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:Label ID="FNLabel" runat="server"></asp:Label>,
<asp:Label ID="LNLabel" runat="server"></asp:Label><br />
<br />
<asp:Label ID="IncomeLabel" runat="server"></asp:Label>
<br />
<asp:Label ID="ResidenceLabel" runat="server"></asp:Label><br />
<br />
<asp:Calendar ID="AcctSelectionCalendar" runat="server"></asp:Calendar>
<br />
<br />
&nbsp;
<asp:Button ID="PreButton" runat="server" OnClick="Button1_Click" Text="Return to previous page" /></div>
</form>
</body>
</html>
Page2.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Page2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
FNLabel.Text = (Session["value1"]).ToString();
LNLabel.Text = (Session["value2"]).ToString();
IncomeLabel.Text = (Session["value3"]).ToString();
ResidenceLabel.Text = (Session["value4"]).ToString();
AcctSelectionCalendar.SelectedDate = Convert.ToDateTime((Session["value5"]));

}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default.aspx", true);
}
}
This little example should show you how easy it is to maintain the values of controls when navigating back and forth between pages. I have not experimented extensively with other controls, so further tweaking may be required.



No comments: