Which controls have ViewState maintained?

I am trying to wrap my head around the control infrastructure to understand which ones ASP.NET maintains view state for.

There are these regular HTML controls ex:
<input type="radio" checked="checked"/> -> I understand these do not have viewstate

Then there are HTML controls with runat=”server”
<input type="radio" checked="checked" runat="server"/> -> Does the viewstate get maintained between postbacks?

Then there are ASP.NET controls
<asp:TextBox id="txtMyText" runat="server"/> -> I understand these do have viewstate

We have a few custom controls that inherit HtmlTextBox
<myPrefix:myTextBox id="txtMyText" runat="server"/> -> Is this the same as type 2 above?

Is it safe to assume that any control with runat="server" tag will have viewstate maintained?


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There are 3 types of controls, the standard HTML elements like , HTML server controls which have the runat=server tag added, and full web controls. Only the web controls have viewstate maintained.

Method 2

When we were having problems with viewstate I started using the Viewstate helper software from Binary Fortress http://www.binaryfortress.com/aspnet-viewstate-helper/

It gives you a real insight into what’s going on – as well as helping with viewstate related performance issues you can decode the viewstate with one click and see what is actually in there – so you get to understand what controls are using viewstate and which aren’t, and exactly what they are storing in there.

Also, something nobody else has mentioned is ControlState. This came along with ASP.NET 2 and the theory is that the important stuff that is necesssary for a control to function goes in the control state, and the data etc in the viewstate, so you can switch off the viewstate and bind the data to your control on every postback and the control still basically works using controlstate. I say “theory” because in practice the implementation seems patchy. When you look into the dropdownlist code using reflector for example this isn’t properly implemented. This may have changed with later releases of the framework, I’m not sure. Lots of info on controlstate out there if you search for it, I just thought I’d mention it.

Method 3

afaik no, that HTML controls are not designed to maintain anything in the viewstate, if you care about it, take the webcontrols.

Method 4

Anything you put on your page’s view and add the runat=”server” will have the viewstate maintained.

As for dynamically added controls, it depends on when and how you add the control to the control tree. Check out the accepted answer to this question, but also check out my question here.

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x