By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,143 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,143 IT Pros & Developers. It's quick & easy.

Maintaining variable state on web form

P: n/a
Das
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
Nov 16 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.