i was recently surfing the msdn for the session state management tools, i came accross viewstate, which can be used the retain the page or controls value accross the page postback, so i created a simple application, which contains a asp:textbox and asp:button now i made the EnableViewState="false" for textbox and run the page, entered some values and clicked on the button, the page postbacked but the value was retained, i thought that would be because the pages viewstate property is enabled, so i changed the EnableViewState="false" in the page directives, and run the page, still the textbox value was retained in the textbox accross the postback, can anyone tell me with small example how the does viewstate work in my scenario
ViewState can probably not be explained with a small example ;-)
I'd recommend to read this article: Truly Understanding ViewState
The TextBox is rendered as input control, so the value is post back and set again to the TextBox.
The viewstate have a meaning on the TextBox for the other attributes that you can set it pro grammatically, or in case that you make it hidden and you like to keep the content of it.
Related
I have read some articles on viewstate, but didn't find exactly what I wanted.
I have a simple aspx page on which I have a textbox and a button control.
If I disable the viewstate for the whole page at design time -> run the application -> then across postbacks after clicking the button,
what difference would I see on my page ?
I just want to see the difference between two cases i.e. viewstate enabled and viewstate disabled.
Can you please explain me with example ?
I got a page with multiple dropdownlist. Each of them are fill with a lot of stock.I need the selected value of each of them on postback.
My problem is, at the end of the post back it take so much time to refresh(show) the page.
and I saw that the viewstate is fill with a lot of thing.
I try to disable viewstate for my dropdownlist, but that erase my value on postback.
I already have a conpressor for viewstate.
can you help me.
You can use Page Caching - based on OutputCache property of page
Link Sample : http://msdn.microsoft.com/en-us/library/hdxfb6cy%28v=vs.100%29.aspx
Link Configuration : http://msdn.microsoft.com/en-us/library/ms178606%28v=vs.100%29.aspx
Page Caching or disable viewstate and add selected values to session and get that values at pageload again.
I've been going through this excellent article http://msdn.microsoft.com/en-us/library/ms972976.aspx that says ViewState is not responsible for form fields to retain their values between postbacks. So form fields values are never stored in ViewState?
EDITED: What I mean form fields are ASP.NET controls like TextBox, Dropdownlist etc.
EDITED: If an user enters a value in an ASP.NET textbox and submit the form, the new page still has the textbox with that value I was thinking this is because of ViewState but the article says it's not so!
As you say, form values are NOT stored in the viewstate. The reason that (for example) the text of a TextBox control is retained between two postbacks is because it implements the IPostBackDataHandler-contract and automatically maps the keys in the Request.Form-collection to the appropriate properties of the control. These two mechanisms are often confused.
See http://www.mikesdotnetting.com/Article/65/ViewState-form-fields-labels-and-Javascript for a good explanation.
Text fields don't carry their value in ViewState because their value is explicitly sent through the HTTP POST (See Request.Form) and restored to the control before Page_Load.
DropDownLists do use ViewState to store their contents.
only Asp.Net Control will stored in the ViewState. No html fields.
So
<asp:TextBox id="tb1" runat="server" />
will work and
<input type="text" id="tb1" />
will not work.
So form fields values are never stored in ViewState?
As stated by dknaack: No
But you may also want to have a look at ControlState since ViewState can be disabled.
The point of viewstate is to track the "change" in your webcontrols. It's your responsibility as a developer to ensure that the "initial" state of your controls is recreated each page load. Then the asp .net mechanism determines the change that occurred during a postback scenario to decide which events should be fired on the server side.
For an overview of how the ASP .Net postback mechanism works and where Viewstate fits in have a look at this question I originally asked on SO;
How does Postback Work?
Put a textbox, a checkbox and a button on a website.
Set the "EnableViewState" property of textbox and checkbox to false.
Write something into textbox and check the checkbox.
Click the button.
Why is the textbox still written and the checkbox checked after response?
Some things aren't totally dependent on ViewState. In the controls you listed, those values are available in the POST sent to the server, so they're gotten out of there and the controls restore their state that way.
Other things, like the text in a <asp:Label> for instance aren't sent back in any way, and they'll lose their data without ViewState. The same is true for other properties, like the styling of the textbox, etc...only it's value will be restored, because that's all that's sent back and as a result, all it's coded to grab and restore. If you were to say make it red, that would be lost on postback.
As a general rule, what a control can restore strictly from posted data will be restored on postback, everything else is lost.
Because HTML Controls are Stateless control. Therefore Microsoft provide a feature of ViewState that help when a user sends the data into server or after post back the value remain same. Therefore you have to set the property "EnableViewState" to True. By default, all the ASP.NET controls have their EnableViewState set to True
I have an asp:Gridview bound to an asp:ObjectDataSource. I have disabled the ViewState on the GridView, and have not set the DataKeyNames property. I have about 10 BoundFields and a few TemplateFields. These TemplateFields are not bound to server controls but to an anchor tag or to an img tag.
However, at runtime, when I switch on page tracing I see that the ControlState of the Gridview varies between 7 and 12K for displaying just 14 rows of data. (View source on the rendered page also gives a same long string in the __VIEWSTATE hidden field). I do not understand why this happens as I have enableViewState="false" on the gridview and, as said above, I am not using DataKeyNames. So, where is this Gridview ControlState coming from and is there a way to get rid of it?
Thanks in advance,
Tim
i think its normal because:
control state cannot be disabled, Control state is designed for storing a control's essential data (such as a pager control's page number) that must be available on postback to enable the control to function even when view state has been disabled
note: By default, the ASP.NET page framework stores control state in the page in the same hidden element in which it stores view state. Even if view state is disabled microsoft said
that mean you actually saw the data of ControlState in _ViewState field which is ok
because as microsoft said the ControlState of the control stored in viewstate even if you disable ViewState
If you're disabling viewstate, not using any postback controls within the gridview, and not doing paging/sorting, then you're probably better off using a repeater. Repeaters don't have to be placed inside a tag. So, the control state won't be an issue.
If you're using .net 3.5 you could also investigate using the ListView, which to me seems like a repeater / gridview hybrid.
This article says that control state is actually stored in the __VIEWSTATE in a HybridDictionary.
Also, these articles say that even though you can set EnableViewState=false, you can never turn off ControlState (which is the point - so clients can't break your application):
Control State vs. View State Example
ASP.NET State Management Overview
ASP.NET State Management Recommendations