为什么在每个.aspx页面中生成表单元素?

发布于 2024-10-26 18:52:03 字数 292 浏览 0 评论 0原文

我是asp.net新手,我必须使用asp.net来luanch我们的应用程序,并且我使用visial studio,我刚刚发现每次我创建一个新页面时,都会出现类似:

<body>
  <form runat="server">
   the content of my page
  </form>
</body>

我只是不知道为什么表单元素存在于每个页面中,其中一些根本不需要表单,因为我不尝试使用表单向服务器端提交某些内容。

为什么?

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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情徒 2024-11-02 18:52:03

ASP.Net 要求所有服务器控件都嵌套在表单控件中。这是因为 ASP.net 将其状态信息存储在名为 ViewState 的隐藏控件中。页面上的任何服务器控件都将使用 ViewState 来保留其状态。另外,您应该意识到页面上可能只有一个表单。

如果您需要有两个单独的“表单”,通常会将每个表单放置在一个 Panel 控件中,并在其元素上设置其 DefaultButton 属性和 ValidationGroup 属性,以便将其与其他“形式”隔离开来。

有关详细信息,请参阅 W3Schools 的 ASP.NET Web 表单

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.

很糊涂小朋友 2024-11-02 18:52:03

最初的 ASP.Net(Webforms)框架对您的工作方式做出了一些假设。其中一些现在可能很烦人,具体取决于您喜欢的工作方式。

  • 大多数.Net 控件都需要表单。
  • ViewState 需要一个表单,并且一个表单。
  • 回发模型需要一个表单,并且一个表单。

如果没有表单,大多数 ASP.NET 功能将无法工作。因此,默认情况下没有表单会导致很多问题,尤其是对于初学者来说。几乎每个页面的第一步都是拖动“表单容器控件”或任何名称,人们总是会弄错。根据他们的计划,始终存在的单一形式是有意义的。

本讨论假设Webforms,即“默认”ASP.NET Web 框架。在 ASP.Net MVC 中,没有自动表单元素。许多像这样的假设都被我们许多人现在更喜欢的其他假设所取代。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文