TextBox can retain the value entered in it even if the viewstate is disabled, as LoadPostBackData event magically loads the data into TextBox at the PagePostBack. Is there a specific reason TextBox has ViewState or the ViewState has been just inherited from WebControl class?
ViewState includes much, much more than just the text.
To clarify: if you do Textbox1.Visible = false; then the control will not render any html output. With ViewState enabled its full runtime state will still be passed on to the next postback, including the Text property, the Visible property and many of its other properties.
In other words, with ViewState enabled a Web Control never loses any of its state, even when the control itself is not rendered in the html output.
With ViewState disabled, the Text property (and all others) will lose their value as soon as you set Visible to False; or even if you set set Visible to False for its surrounding/parent control.
Some properties of a control are required to maintain their values between postbacks so them can work properly. Disabling ViewState doesn't disable this behavior.
Text is the only property of the TextBox which preserves the data between postbacks, even ViewState is disabled, but on the other side, the ToolTip, as example, uses pages ViewState to preserve the data. If ViewState is disabled, ToolTip won't preserve the data between postbacks.
The TextBox value is not maintained via the ViewState. It is maintained through form data.
Related
I have read that the viewstate in asp.net stores the values of control properties across postbacks.
Lets say I have a page that has a textbox
<asp:TextBox ID="fldFileId" runat="server"></asp:TextBox>
and then on the client side via javascript, I get a reference to the element , and then set the border style thusly
refToTextBox.style["border-style"] = "dashed";
Upon postback, the border style has dissapeared and the textbox reverts to its original look. But glancing at the properties for an asp:TextBox in VS2010 there is a 'BorderStyle' property for it.
Is there a reason why this attribute does not get saved in the view state?
Setting a value client-side does not update ViewState. You have to set the style server-side for ViewState to store it. Alternatively, you could:
Re-run your JavaScript after postback.
Store the styling in a cookie and use JavaScript to restore the style.
Find a JavaScript library to modify ViewState on the client-side (not recommended).
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.
I've set ViewStateMode="Disabled" on a textbox, however when I perform any postback action, when the page loads again it's text value is still retained.
Have I misunderstood this property?
Assigning it string.empty on postback seems abit over the top.
Have a look at this article. It explains very well why the text is being persisted even if ViewState is disabled. So you don't come around assigning to it String.Empty on postback.
I have a ASP.NET 4.0 webforms site where I have the MasterPage so it is set to ViewStateMode="Disabled" along with the content placeholders being set similarly.
When I'd view my page I'd still see a ViewState field rendered, I then tried adding the ViewStateMode="Disabled" to the page level also but that didn't change anything.
I'm not aware of latest changes on ViewState for the framework 4 but you have to take into account that the ViewState field rendered to the client has 2 components: ViewState itself and ControlState.
The ControlState is ALWAYS sent to the client on the viewstate field no matter if you have enabled ViewState or not.
So you can expect to drastically reduce the size of the viewstate field sent to the client but not completely remove it.
Control state contains the minimal things that a control needs to persist across postbacks in order to work as expected.
Control State
In addition to view state, ASP.NET supports control state. The page uses control state to persist control information that must be retained between postbacks, even if view state is disabled for the page or for a control. Like view state, control state is stored in one or more hidden fields.
http://msdn.microsoft.com/en-us/library/bb386448.aspx
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