ViewState question

C

Carly

Hi,

I am now not sure I understand what ViewState does.
Having EnableViewState=true or false on a WEB form and/or different
server controls does not seem to make any difference.
I am just playing around with VS 2005 and no matter the value of
EnableViewState all the controls maintain their value.
Can anybody explain or point me to a clear article about this subject.

Much appreciated,

Carly
 
K

Karl Seguin

Textboxes will automatically maintain their viewstate across a postback
reguardless of what you set the EnableViewState property.

I suspect that what you are seeing are controls having their values reset in
postback. If you set EnableViewState to false, but reset a control's value,
of course you'll see it's value.

The simplest example is a label, such as:

<asp:label id="x" runat="server">Some Text</asp:label>

Even with EnableViewState to false, "Some Text" will appear within the
table.

Taking it to the next level, if you are binding a repeater on page load and
on postback, the values will appear in the repeater even with
EnableViewState set to false.
That's why you often see people do stuff like:

if (!Page.IsPostBack)
{
users.DataSource = GetUsers();
users.DataBind();
}

When the page IS posting back, the users control will be recreated from the
viewstate. If you did the above code and set EnableViewState to false, you
wouldn't see any data inside the users repeater.

Karl
 
B

bruce barker

viewstate is stored in a hidden field form the form. if you trun
viewstate on, all controls store their properties in viewstate.

contrary to popular belief, viewstate is not used for the values, as
this is posted back by the browser as form data. if you want to fire the
onchange event, the original value is stored in viewstate, and the
postback value is compared to the viewstate value to see if it was
modified. another common use of viewstate is by dropdowns to to remember
the list, or grids/ repeaters to recreate their lists.

controls storing their properities in viewstate makes coding easy, the
viewstate can be huge, and has to be sent to the browser, and the
browser has to send it back.

on any serious site, you should turn viewstate off, and set control
properties during oninit. (if you restore the render value, the onchange
will still fire).


-- bruce (sqlwork.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top