开发者

why the form element generated in each .aspx page?

I am new in asp.net,I have to luanch our application use asp.net,and I use the visial studio,I just found that each time I create a new page,there will be something like:

<body>
  <form runat="server">
   the content of my开发者_JS百科 page
  </form>
</body>

I just do not know why the form element exist in EVERY page,some of them does not need a form at all,since I do not try to submit something to the server side use the form.

WHy?


ASP.Net requires that all server controls be nested within a form control. This is because ASP.net stores it's state information in a hidden control called ViewState. Any server controls you have on your page will use ViewState to persist their state. Also, you should be aware that you may only have one form on the page.

If you need to have two separate "forms," you would generally place each one in a Panel control and set it's DefaultButton property and the ValidationGroup property on it's elements in order to isolate it from the other "forms."

Please see ASP.NET Web Forms from W3Schools for more information.


The original ASP.Net (Webforms) framework made some assumptions about how you would work. Some of these can be annoying now, depending how you like to work.

  • Most .Net controls require a form.
  • ViewState requires a form, and only one form.
  • the Postback model requires a form, and only one form.

Most asp.net functionality would not work if there wasn't a form. So not having a form by default would cause a lot of problems, especially for beginners. The first step on almost every page would be to drag over your "form container control" or whatever it would be called, and people would get it wrong all the time. Given their plan, a one form that was always there made sense.

This discussion assumes Webforms, the "default" ASP.NET web framework. In ASP.Net MVC, there is no automatic form element. A lot of assumptions like this one are traded for other assumptions, which many of us now prefer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜