There are a few different ways using "State Bags". Two of them are the
Session collection and the ViewState collection.
The ViewState collection is valid while the current page is up.Meaning, if
the page is refreshed, the ViewState values remain, but as soon as you leave
the page, your ViewState values are gone.
The Session collection exists for the lifetime of the user's session on your
web site, so it's a good way to pass information from one page to another.
The collections are dictionaries that hold objects, so you can do stuff
like:
ViewState["UserName"] = userNameTextBox.Text;
and then elsewhere,
string userName = ViewState["UserName"].ToString()
You can store other data types of course, but you'll need to convert
them from objects to whatever the destination type is.
Session works the same way. Both are members of the Page (well
technically, ViewState is a member of Control, but Page derives, eventually,
from Control).
The primary use of ViewState is to store control state information, but
using it for your own values is perfectly legitimate.
Pete
"Das" <Da*@discussions.microsoft.com> wrote in message
news:91**********************************@microsof t.com...
Hi all,
I'm using Asp.net. Within a form I have declared declared a public
variable. When the form is loaded I set the property for the variable. but
it doesn't maintain its state on postback. I there any way to maintain the
state for variable?
Thanks,
Das