I'm trying to find a way to maintain state information across the
pages of a website. My site contains of many different pages, and I
want to be able to pass a fixed set of parameters from page to page,
without using the Session object.
Ideally I would like to be able to use <input type=hidden> elements to
store the variables in, so users can press the Back button and still
get the correct state information for each page when cached HTML is
loaded. When a submit happens, the IsPostBack block then changes the
state parameters based on whatever the users have done on the page,
before Server.Transferring the modified form to the next page. To do
this, obviously, the <input type=hidden> elements run server-side.
So far so good. But I also would like to completely separate out the
logic that maintains state so that I don't have to change every page
if I want to, say, add a new state parameter.
What I have so far is a user control to which I programmatically add
my HtmlInputHidden elements. This user control (.ascx) sits inside a
PlaceHolder. I add an attribute called "runat" to each
HtmlInputHidden element with a value of "server". I define all this in
Page_Init. When the page renders as HTML, it shows as a bunch of
correctly defined <input type=hidden runat=server> elements.
However what I can't work out is how I then change the value of the
<input type=hidden> elements before the Server.Transfer. I can't seem
to get a reference to the individual elements, except through the
Request.Form object, and this is read-only. *Do* my <input
type=hidden> elements actually run server-side? I have also tried
using LiteralControls in my user control, to no avail. Obviously
Request.Form.Set is not an option.
Code below. The line hdVar1.Value = "second"; blows up with a
System.NullReferenceException. This example uses the LiteralControl.
Hope it makes sense.
Thanks, Eva
[PageState.ascx]
using...
public class PageState : System.Web.UI.UserControl
{
private void Page_Init(object sender, System.EventArgs e)
{
String var1 = "first";
if (Request.Form.Get("hdVar1") != null)
var1 = Request.Form.Get("hdVar1");
String txt = "<input type='hidden' runat='server' id='hdVar1'
name='hdVar1' value='" + var1 + "'>";
System.Web.UI.LiteralControl lc = new
System.Web.UI.LiteralControl(txt);
this.Controls.Add(lc);
}
(OnInit code etc - snip)
}
[PageStateTest1.aspx]
using...
public class PageStateTest1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder2;
protected System.Web.UI.UserControl pageStateControl;
protected System.Web.UI.HtmlControls.HtmlInputHidden hdVar1;
private void Page_Init(object sender, System.EventArgs e)
{
pageStateControl = (UserControl)LoadControl("PageState.ascx");
PlaceHolder2.Controls.Add(pageStateControl);
}
private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack)
{
hdVar1.Value = "second";
Server.Transfer("PageStateTest2.aspx",true);
}
}
(OnInit code etc - snip)
}