我们可以在网页中使用多种形式吗?

时间:2022-04-20 17:05:35

So far, all the web pages I met contain at most one <form> tag. Why not multiple ones? I can not think of reasons why multiple forms can't coexist within the same web page.

到目前为止,我遇到的所有web页面最多都包含一个

标记。为什么不多重的呢?我想不出为什么多个表单不能在同一个web页面*存的原因。

Also, to be specific to ASP.NET - why are all the server controls are placed within the <form> tag? Why not place them somewhere else?

另外,要针对ASP。NET -为什么所有的服务器控件都放在

标签中?为什么不把它们放到别的地方呢?

Plus,

另外,

I noticed that in an .aspx file, the <form> tag has the runat=server attribute, while a normal server control such as Button also has one. So it seems the <form> is also a server control. But strangely enough, I cannot find it in the Visual Studio Toolbox.

我注意到,在.aspx文件中,

标记具有runat=server属性,而像Button这样的普通服务器控件也有一个。所以看起来也是一个服务器控件。但奇怪的是,我在Visual Studio工具箱中找不到它。

6 个解决方案

#1


23  

There can be multiple forms, with hacks.

有了黑客,可以有多种形式。

It is indeed a shortcoming of WebForms. In ASP.NET MVC you can implement as many forms as you want (and it is valid & correct behavior of web pages).

这确实是WebForms的一个缺点。在ASP。你可以实现任何你想要的形式(它是有效的和正确的行为的网页)。

The reason all server controls are placed inside <form> tag is to allow the WebForms engine to recognize them, load their values & save their values from/to the ViewState. Almost all infrastructure of control management in WebForms is based on the idea that a tag contains everything you access from the code-behind.

所有服务器控件都放在

标签内的原因是允许WebForms引擎识别它们,加载它们的值,并将它们的值保存到ViewState中。WebForms中几乎所有的控制管理基础设施都是基于这样一种思想,即标记包含了从代码后面访问的所有内容。

#2


3  

As pointed out, this is one of the shortcomings of WebForms. I do want to point out, additionally, that with cross-page posting and validation groups, you can typically reach your desired behavior (for most "multi-form" solutions).

正如所指出的,这是WebForms的缺点之一。另外,我想指出的是,通过跨页面发布和验证组,您通常可以达到所需的行为(对于大多数“多形式”解决方案)。

#3


2  

Regarding the additional question: the <form runat="server"> is parsed as HtmlForm class behind the scenes, which inherits from HtmlControl like any other HTML element with runat="server".

关于附加的问题:

被解析为幕后的HtmlForm类,该类继承自HtmlControl,与runat="server"的任何其他HTML元素一样。

Unlike any other HtmlControl though, there can exist only one instance per page and it does not appear in the toolbox as it's added automatically to every new Form you create, so it's quite pointless.

与其他HtmlControl不同的是,每个页面只能存在一个实例,而且它不会出现在工具箱中,因为它会自动添加到您创建的每个新表单中,所以这是毫无意义的。

#4


0  

Yes, it can be done - by creating a custom HtmlForm object and toggling the forms as needed. I've just answered a similar question here (with code):

是的,可以通过创建一个自定义HtmlForm对象并根据需要切换表单来完成。我刚刚用代码回答了一个类似的问题:

Paypal Form Ruins My ASP.NET webforms layout -> How to Solve?

贝宝破坏了我的ASP。NET webforms布局->如何解决?

#5


0  

many non server forms - you can , but only one runAt Server form

许多非服务器表单——您可以,但只有一个runAt服务器表单。

i also found this :

我还发现:

A server-side form tag is the tag which has a runat="server" attribute. If this attribute is missing, then it's a typical HTML form tag. The conclusion is that you are allowed to use multiple form tags on a page, as long as only one has the runat="server" attribute. The disadvantage of the form that doesn't have this attribute, is that view state won't work (meaning form values will disappear when using the back/forward browser buttons). It's a small price to pay if you really need multiple forms on a page.

服务器端表单标记是具有runat="server"属性的标记。如果这个属性缺失,那么它就是一个典型的HTML表单标记。结论是,允许在页面上使用多个表单标记,只要只有一个具有runat="server"属性。没有此属性的表单的缺点是,视图状态不能工作(意味着使用后退/前进浏览器按钮时表单值将消失)。如果你真的需要在一个页面上创建多个表单,这是一个很小的代价。

#6


-5  

  1. Take master page & set design.
  2. 以master page和set design为例。
  3. Take one form in master page.
  4. 在母版页中选择一个表单。
  5. Second form take in contain place holder.
  6. 第二种形式采取包含地点持有人。
  7. In contain place holder in only for write form tag (not use)
  8. 只用于写表单标签(不使用)
  9. Add aspx page & design second form but not write form tag only for control put
  10. 添加aspx页面和设计第二表单,而不是只为控件放置写表单标签
  11. Take button click event fire code write
  12. 点击按钮,点击事件火警代码

This is proper way of two form

这是两种形式的固有方式

#1


23  

There can be multiple forms, with hacks.

有了黑客,可以有多种形式。

It is indeed a shortcoming of WebForms. In ASP.NET MVC you can implement as many forms as you want (and it is valid & correct behavior of web pages).

这确实是WebForms的一个缺点。在ASP。你可以实现任何你想要的形式(它是有效的和正确的行为的网页)。

The reason all server controls are placed inside <form> tag is to allow the WebForms engine to recognize them, load their values & save their values from/to the ViewState. Almost all infrastructure of control management in WebForms is based on the idea that a tag contains everything you access from the code-behind.

所有服务器控件都放在

标签内的原因是允许WebForms引擎识别它们,加载它们的值,并将它们的值保存到ViewState中。WebForms中几乎所有的控制管理基础设施都是基于这样一种思想,即标记包含了从代码后面访问的所有内容。

#2


3  

As pointed out, this is one of the shortcomings of WebForms. I do want to point out, additionally, that with cross-page posting and validation groups, you can typically reach your desired behavior (for most "multi-form" solutions).

正如所指出的,这是WebForms的缺点之一。另外,我想指出的是,通过跨页面发布和验证组,您通常可以达到所需的行为(对于大多数“多形式”解决方案)。

#3


2  

Regarding the additional question: the <form runat="server"> is parsed as HtmlForm class behind the scenes, which inherits from HtmlControl like any other HTML element with runat="server".

关于附加的问题:

被解析为幕后的HtmlForm类,该类继承自HtmlControl,与runat="server"的任何其他HTML元素一样。

Unlike any other HtmlControl though, there can exist only one instance per page and it does not appear in the toolbox as it's added automatically to every new Form you create, so it's quite pointless.

与其他HtmlControl不同的是,每个页面只能存在一个实例,而且它不会出现在工具箱中,因为它会自动添加到您创建的每个新表单中,所以这是毫无意义的。

#4


0  

Yes, it can be done - by creating a custom HtmlForm object and toggling the forms as needed. I've just answered a similar question here (with code):

是的,可以通过创建一个自定义HtmlForm对象并根据需要切换表单来完成。我刚刚用代码回答了一个类似的问题:

Paypal Form Ruins My ASP.NET webforms layout -> How to Solve?

贝宝破坏了我的ASP。NET webforms布局->如何解决?

#5


0  

many non server forms - you can , but only one runAt Server form

许多非服务器表单——您可以,但只有一个runAt服务器表单。

i also found this :

我还发现:

A server-side form tag is the tag which has a runat="server" attribute. If this attribute is missing, then it's a typical HTML form tag. The conclusion is that you are allowed to use multiple form tags on a page, as long as only one has the runat="server" attribute. The disadvantage of the form that doesn't have this attribute, is that view state won't work (meaning form values will disappear when using the back/forward browser buttons). It's a small price to pay if you really need multiple forms on a page.

服务器端表单标记是具有runat="server"属性的标记。如果这个属性缺失,那么它就是一个典型的HTML表单标记。结论是,允许在页面上使用多个表单标记,只要只有一个具有runat="server"属性。没有此属性的表单的缺点是,视图状态不能工作(意味着使用后退/前进浏览器按钮时表单值将消失)。如果你真的需要在一个页面上创建多个表单,这是一个很小的代价。

#6


-5  

  1. Take master page & set design.
  2. 以master page和set design为例。
  3. Take one form in master page.
  4. 在母版页中选择一个表单。
  5. Second form take in contain place holder.
  6. 第二种形式采取包含地点持有人。
  7. In contain place holder in only for write form tag (not use)
  8. 只用于写表单标签(不使用)
  9. Add aspx page & design second form but not write form tag only for control put
  10. 添加aspx页面和设计第二表单,而不是只为控件放置写表单标签
  11. Take button click event fire code write
  12. 点击按钮,点击事件火警代码

This is proper way of two form

这是两种形式的固有方式