A page can have only one server side form.

huangapple go评论64阅读模式
英文:

A page can have only one server side form

问题

我遇到了这个错误,一个页面上有两个不同ID的表单,我尝试移除了"runat="server"",因为表单没有使用用户控件,页面加载后表单出现了,但按钮事件/表单验证不起作用,请帮忙,如果有更好的解决方案。 页面上有两个不同表单ID时的截图

英文:

I am getting this error where a page has two forms but with different ID, i tried removing runat="server" as the form was not using User controls, the page loads with the forms but there button events/form validation doesn't work, please help if anyone have a better solution to this

Ss from the page when it has two forms with different form ID's

答案1

得分: 1

在网页表单的情况下,是的,这是正确的,你只能在页面中有一个服务器端表单标签。

但是,为什么你需要多个表单标签呢?

在绝大多数情况下,对于特定内容,一个简单的div部分就足够了,多个表单标签没有什么实际问题或好处。

我怀疑这里的问题是对表单标签的误用,或者你认为它有一些与给定页面的内容定义之外的功能(实际上在这里没有其他目的 - 因此你不应该关心、干扰或者甚至在这个问题上花费大脑能量 - 每个页面都将有一个表单标签)。

如果这是一个主页面+子页面的设置,那么事实证明主页面已经为你提供了表单标签,因此你只需要将内容放在内容区域内,就像这样:

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2><%: Title %></h2>
    <p>Your contact page.</p>

    <address>
        One Microsoft Way<br />
        Redmond, WA 98052-6399<br />
        <abbr title="Phone">P:</abbr>
        425.555.0100
    </address>

    <address>
        <strong>Support:</strong><a href="mailto:Support@example.com">Support@example.com</a><br />
        <strong>Marketing:</strong><a href="mailto:Marketing@example.com">Marketing@example.com</a>
    </address>
</asp:Content>

因此,对于任何具有主页面的网页,你不使用、不需要在该页面中放置表单标签。

但是,对于不是主/子页面的任何页面,当然你只需要并且将只有一个表单标签,没有任何有效的理由或优势来在一个给定页面上使用或希望有多个表单标签。

我怀疑你的目标和问题实际上与表单标签无关,但对于aspx网页表单页面,你只需要一个表单标签,事实上只能有一个表单标签。

请随时在这篇文章中添加评论,说明你的“真正”目标是什么,我可能可以建议一些想法来帮助你实现最终的目标。

英文:

In the case of web forms, yes, that is correct, you can ONLY have one server side form tag in that page.

However, WHY do you need more then one form tag?

In the VAST majority of cases, a simple div section for some particular content is fine, and there is no real issue or gain to having more then one form tag.

I suspect the issue here is miss-use of form tag, or that you think it has some function other then what defines the content for that given page. (it really has no other purpose here - and thus you should not care, mess with, or even give this issue any brain power here - it simple that each and every page will have a form tag.

If this is a master + child page setup, then it turns out the master page has the form tag for you already, and thus you ONLY placing content inside of the content area, say like this:

&lt;asp:Content ID=&quot;BodyContent&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt;
    &lt;h2&gt;&lt;%: Title %&gt;&lt;/h2&gt;
    &lt;p&gt;Your contact page.&lt;/p&gt;

    &lt;address&gt;
        One Microsoft Way&lt;br /&gt;
        Redmond, WA 98052-6399&lt;br /&gt;
        &lt;abbr title=&quot;Phone&quot;&gt;P:&lt;/abbr&gt;
        425.555.0100
    &lt;/address&gt;

    &lt;address&gt;
        &lt;strong&gt;Support:&lt;/strong&gt;&lt;a href=&quot;mailto:Support@example.com&quot;&gt;Support@example.com&lt;/a&gt;&lt;br /&gt;
        &lt;strong&gt;Marketing:&lt;/strong&gt;&lt;a href=&quot;mailto:Marketing@example.com&quot;&gt;Marketing@example.com&lt;/a&gt;
    &lt;/address&gt;
&lt;/asp:Content&gt;

So for any web page that has a master page, then you do NOT use or have nor place a form tag in that page.

However, for any page that is not part of a master/child page, then of course you only need and will have one form tag, and there is no valid reason or advantage to having or even wanting more then one form tag on a given page anyway.

I suspect your goal and question is not really related to a form tag, but for aspx web form pages, you only need one form tag, and in fact can only have one form tag.

Do feel free to add in comments to this post what you "real" goal here is, I can probably suggest some ideas to help you in your final goal that you looking to achieve here.

huangapple
  • 本文由 发表于 2023年5月18日 10:31:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277365.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定