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).
Related
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.
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
Can you set a asp.net web user control property using jQuery
I don't think it's possible, since the controls' properties are evaluated before the PreRender phase. Every property setting made by jQuery happens after the page is rendered so it's useless.
please tell me how to maintain state of placeholder. i have a placeholder in which i add many image controls dynamically but when my page get refresh all controls from placeholder gets removed from it. the enableViewstate of placeholder is set to true.. please tell me how to maintain its state..
ViewState registration happens just after the Init events in the Page lifecycle.
If you are adding your dynamic images after Init, then they are not registered as part of ViewState. Your issue will be solved if you add them to your Placeholder control during Page_Init.
I think you need to add the controls in the Page_PreInt() event
You must add dynamic controls on every page load. Controls are not stored in ViewState, only control STATES and data for controls which do not post their data.
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