Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Postback, ViewState and SessionState

Status
Not open for further replies.

FatalExceptionError

Technical User
Apr 10, 2001
100
US
I am confused as to the operations of postback, viewstate and session state. Is a link or FAQ available that explains in depth about these things. As it is my page is acting funny capturing data from controls and i believe the problem is me not properly understanding the functions of these things. As it is all postback seems to do is reload the webpage.

The statement below is true
The statement above is false
There are 10 kinds of people, those who know binary and those who don't
I am a geek and i approve this message
 
Here is the very, very basic definition:

"PostBack" simply means that the form has been submitted (POSTed) back to itself. In other words, the page is loading AGAIN.

"ViewState" is the state of the Page and all of its controls. It is the value of all the form objects. ASP.NET maintains this by wrapping it all up into hidden form variables. That's how it knows that the user entered "Bubba" into "TextBox1".

ViewState is stored in something called the "State Bag", which is a dictionary (key-value) structure. You can view anything in the state bage by using the "ViewState" keyword:

Code:
if (ViewState["myTextBox"] != null)

"Session State" is like ViewState, only it is scoped not to a single page, but to all of the requests and communication occuring between a USER/Browser, and the complete ASP.NET application, which may comprise several forms.

It is maintained by the server, not by hidden form variables. You can interact with the Session by using the "SessionState" class, which has a "Contents" colleciont.

The typical use for Session state is to write your own authentication, in which you want to maintain a flag or variable across all pages for a particular user.

That should get you going. If not, then let us know. Or, if you have particular questions, of course post those.

For a reference, I use "Programming ASP.NET" by Jesse Libery & Dan Hurwitz, published by O'Reilly.



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
So when the state is captured and the page is posted its saves the way the form looked and then reloads. I get this. I had a function in page_load to populate a listbox from a dataset. Everytime it posted back the data was repopulated into the list.

Now my problem is my selected index and values in those indexes in the list box are not being passed back when the page is reposted.

The statement below is true
The statement above is false
There are 10 kinds of people, those who know binary and those who don't
I am a geek and i approve this message
 
In your Page_Load method, the first and only the first time you run, you populate your controls with their initial values.

Subsequently, the ViewState mechanism will carry those values back and forth from the client to the server. If, that is, your Page_Load doesn't stomp all over it be reloading the initial values.

That's what the "IsPostBack" property of the "Page" object is for. In your Page_Load, you can do something IF the Page is or isn't being posted back.

So I would suggest that the code to bind your dataset to your listbox be contained in a conditional:

Code:
if (! Page.IsPostBack)
{
  // bind dataset to listbox
}



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
So reloading a listbox with data from a dataset via a function in the page_load was not a good idea.

The statement below is true
The statement above is false
There are 10 kinds of people, those who know binary and those who don't
I am a geek and i approve this message
 
Thanks for the help really set some things str8 for me.

The statement below is true
The statement above is false
There are 10 kinds of people, those who know binary and those who don't
I am a geek and i approve this message
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top