Hi Thomas,
Not much. It's certainly a way that works.
To maintain state you have a few other options as well:
1. Session State
Works acros the session but feels like a bit of overkill for this scenario
2. Cookies
Maintains state between browser sessions but feels like a bit of overkill as well for this scenario
3. Hidden form fields
You can add an <asp:Hidden /> control and write the value to it. Works, but feels slightly clumsy.
4. ViewState
The best option, IMO, is ViewState. You can directly write to and read from ViewState like this:
Code:
ViewState["SomeThing"] = "SomeValue";
string whatEver = (string) ViewState["SomeThing"];
However, it's much cleaner to wrap this in a ViewState property:
Code:
public string SomeValue
{
get
{
object someValue = ViewState["SomeValue"];
if (someValue != null)
return (string)someValue;
else
return String.Empty;
}
set
{
ViewState["SomeValue"] = value;
}
}
This way, you can access the property like any other, but it's value will be persisted in ViewState. Example:
Code:
if (this.SomeValue == "WhatEver")
{
}
You can drop the "this" reference; I just added it here to make it clear it's referring to a property of the Page.
Does this help? Let me know if you want a
VB example instead.
Cheers,
Imar