Hidden Div elements and ASP.Net 4.0

This is a very short post for a new feature that ships with VS 2010 and ASP.Net 4.0.

It is about the hidden fields in ASP.Net 4.0. We do know that ASP.Net is using hidden fields as a state control mechanism. It is used to preserve viewstate and control state.

They are usually included in a div element, <div></div>.

So if you create a simple asp.net application with VS 2008 and .Net 3.5, run it, and view the source code from the browser,

you would see something like that

<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTQ0NTA1MjAxMmRkjIozh8I5q9vNjFXLeK/1IBsUwBM=" />
</div>

Sometimes if you style your div elements in your external css file, those rules that apply for all div elementsin your code, will apply for that <div> that surrounds the hidden field elements that deal with the viewstate.

Sometimes you can end up having a result in your browser window that is different from what you expected. The guilty part for that outcome can be the styling of those div tags that surround viewstate. 

We have a small but very welcome change in ASP.Net 4.0

If you create a simple asp.net application with VS 2010 and view the source code after you run it, you will see something like this

<div class="aspNetHidden">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJNzM3NjE2MDI3ZGRgQNiFxlXngQrLz2ewgBmti+Ee0T+BGCrLcGwY5QJnmQ==" />
</div>

As we can clearly see, ASP.NET 4 renders the div element for hidden fields with a CSS class.

In that way we can differentiate the hidden fields div from others.

Hope it helps!!!

 

1 Comment

  • Hi, that's a nice feature but at my work we still getting the same problem, viewstate's id:
    ID "__VIEWSTATE" uses XML ID syntax

    this is pretty bad, no w3c compliant.

Comments have been disabled for this content.