Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does asp.net wrap the page in a form?

I'm a PHP developer who has to work on ASP.net projects and I'm wondering why every page is wrapped in a form. This just doesn't make sense to me.

Also What's with all the hidden input fields especially the "View State" one.

like image 351
Emily Laguna Avatar asked Jun 14 '10 14:06

Emily Laguna


2 Answers

ASP.Net tries to make it so that the programmers can pretend that the web is a stateful platform, and that it behaves like a desktop application. The ViewState is basically a serialized block of the state of the page when it was generated. When the page gets posted back the server side model gets initialized to the values in ViewState, and then the new values from the posted form are applied.

Part of becoming a decent ASP.Net programmer is learning when to use ViewState and not, because the default is to use it everywhere which causes a lot of bloat in the downloaded page.

like image 62
Chris Pitman Avatar answered Oct 18 '22 20:10

Chris Pitman


Every ASP.NET page is wrapped in a <form> element because the entire framework revolves around POST commands.

ASP.NET provides 'web controls' which are object-oriented abstractions of HTML elements (and in some cases, groups of elements) - in your server-side code you can attach commands to various events on web controls (for example, Button.OnClick, TextBox.OnChanged) - the framework wires these up using a combination of hidden fields and generated javascript. The generated javascript typically sets a hidden field few values to indicate (for example) which control triggered the post and the command arguments (if applicable), then submits the form.

ViewState is a technique used by the framework to serialize client state. It's an alternative to using session heavily, trading larger HTML payloads for a lower memory footprint on the server.

like image 5
Jeff Sternal Avatar answered Oct 18 '22 20:10

Jeff Sternal