Gridview ControlState very large even when viewstate disabled and not using DataKeyNames - asp.net

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

Related

ViewState Management in ASP.Net

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.

Question reg. ASP.NET ViewState

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?

ASP.Net Web Form + Ajax Data lost on post back. Possible cause ? Ajax or changing visible property

I have two Repeater controls, each hosted in a user control. Both user controls are contained in the same aspx page. Only one User Control is visible at any one time. The repeaters are comprised of checkboxes, and text boxes for user input.
The aspx page is configured with an Ajax ScriptManager; and contains several Ajax UpdatePanels. These UpdatePanels result in partial page post backs when text is changed in a textbox control in controls on the aspx page (this is not the behaviour for the Repeaters in the User Controls). Through use of several AsyncPostBackTriggers, various controls contained in the other UpdatePanels on the page have their content refreshed in response to the partial page post backs they are configured be notified about.
Depending on a radio button group selection, I set the visible property to true or false – as appropriate for the User control containing a repeater control. The Repeater control is then populated with data using databinding. All of this works.
However, when the Submit button is clicked, the Repeater control contains no data.
Given that I am not dynamically adding the Controls containing the Repeater controls (but using Visible true / false). I would have thought that the State of the fields and the data in the visible control would be preserved during the post back.
The User Controls are contained within the UpdatePanel that contains the Submit Button.
I have explicitly Enabled View state without any effect.
Am I correct in assuming that I should not have to do any explicit handling of data changes the user makes (via client side script and manipulation of an Data Structure Representing the Repeater Data); and the View State should maintain the data I need to access on the server when submitted?
I do not believe that it is the User Control visible state changes that are causing the issue because when the page is initially loaded on of the User controls is populated with dummy rows (so it displays).
I am suspicious that because the visible state of the controls is changed during partial page post back, that the Page View State ends up with no knowledge of the User Control and therefore cannot track its data (or changes).
I have investigated a lot of similar sounding posts but so far do not feel that I have come across a solid explanation that can help me understand and fix the issue.
Hopefully someone can help.
I must be the only person who was unaware of the finer points of DataBind() method; be it called on a specific control being bound to data; or on the page itself. I hope the below helps others who find themselves in a position where data binding appears to be inexplicably lost'.
My problem was that my page was pretty complex. It has several User Controls on it. The problem was that in these User Controls, I was calling Page.DataBind() in the Page_Load event handler to 're-bind' the page data on PostBacks. This resulted in the Repeater control located on another User Control, to lose its data binding.
Removing the Page.DataBind method call from each of the User Controls on the Page_Load event handler method resolved the problem. It had nothing to do with Update panels / refreshes etc.

ViewStateMode Disabled but still getting ViewState element

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

Why do some asp.net controls keep its state although its EnableViewState property is false?

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

Resources