9

Why does ASP.NET render the hidden __VIEWSTATE input element within <div></div>?

Agnel Kurian
  • 57,975
  • 43
  • 146
  • 217
  • Thanks in advance for the down votes. If time permits, please let me know why. – Agnel Kurian Jul 22 '12 at 10:06
  • You are asking people to speculate: this does not fit with the [SO] approach of asking questions which have objective answers. See the FAQ. – Richard Jul 22 '12 at 10:10
  • That is BS. I am not asking for speculation. I am looking for an answer. As a web developer I care about every single element on my page. A `
    ` takes space. Given enough time, some one who knows the answer will turn up here and let us know the reasons behind this decision. There are lots of folks on SO who can get us the inside information if we ask the right questions. Why don't you just let this be for a while?
    – Agnel Kurian Jul 22 '12 at 10:14
  • 2
    A `
    ` does not inherently take space unless it contains something else that does or you give it specific dimensions in your CSS. I guess the main reason why people are voting to close your question is because although you're not asking for speculation, you're *inevitably* going to get speculative answers, because people can't read. (You can always downvote those answers if they don't provide references or aren't themselves authoritative, of course, but...)
    – BoltClock Jul 22 '12 at 10:18
  • There are instances on SO when the authors and team members of an application/platform ("the ones that placed them there") have stepped in to answer questions such as these. In my experience on SO, this has happened twice. And this is why SO rocks... or used to. Wouldn't you be interested in knowing what the ASP.NET developers have to say on this? – Agnel Kurian Jul 22 '12 at 10:26
  • Aren't any of you interested in knowing what an expert or an ASP.NET insider (I'm sure there are some of them on SO) would say in answer to this question? There are surely people out there who can explain why a hidden input field needs to be surrounded by `
    `. If you aren't one of them then this is your chance to learn. Sit back, grab a beer, relax and let SO do it's magic like it used to. Cheers.
    – Agnel Kurian Jul 22 '12 at 10:36

1 Answers1

5

They did it to "be more aligned with Web standards" - here is a quote from MSDN Magazine article Enforce Web Standards For Better Accessibility:

There were some well-known deficiencies in ASP.NET 1.1. The out-of-the-box controls produced code that would not pass validation. That was largely attributed to the way that ViewState was handled in ASP.NET 1.1-using a hidden input tag that was not contained within a block display, like this:

<input type="hidden" name="__VIEWSTATE" value="dDwtMTU1NzQzNDgy..." />

This, combined with some other syntactical issues, gave ASP.NET 1.1 an unfortunate reputation for non-compliance.
ASP.NET 2.0 addressed many of the Web standards issues. For example, if you look at the source of ASP.NET 2.0-generated pages, you see that ViewState is now wrapped in a div tag, making it compliant:

<div>
      <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="dDwtMTU1NzQzNDgy..." />
</div>

Microsoft has made it known publicly that one of the goals of ASP.NET 2.0 is to be more aligned with Web standards and, in fact, ASP.NET 2.0 lets you create compliant Web sites and controls. A great resource for building compliant Web sites is Stephen Walther's MSDN® article "Building ASP.NET 2.0 Web Sites Using Web Standards". This 78-page article goes into great detail on building sites according to Web standards.

Whether this is truly more aligned with Web standards is open for debate personally I'm not into web standards enough to make my own opinion, I just answer what you asked.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • OK. Actually I still don't understand why an `` standing by itself would not "pass validation". But thanks a ton for the input. – Agnel Kurian Jul 22 '12 at 10:54
  • 1
    I'm with you on this - maybe it's worth a separate question, asking from pure web standards and/or CSS point of view. :) BTW somehow you got influx of upvotes, cheers for that! (only the first was from me). – Shadow The GPT Wizard Jul 22 '12 at 11:42
  • Check the bio at the end of the quoted article: the author is *not* part of the ASP.NET team. – Richard Jul 22 '12 at 14:06
  • @Richard that's true but still, it's published as part of the official MSDN Magazine which means it passed Microsoft confirmation - official enough in my opinion. Anyhow, I've edited my answer to reflect this and prevent future confusion. – Shadow The GPT Wizard Jul 22 '12 at 14:12
  • @Agnel Kurian: As far as I know `` elements can't sit on their own without a block container as its parent (this is why placing inputs directly into forms without nesting them within divs or paragraphs would fail validation). – BoltClock Jul 22 '12 at 16:38
  • @BoltClock, sounds speculative – Agnel Kurian Jul 22 '12 at 17:09