ASP.NET母版页和内容页面中的表单元素

时间:2022-08-04 15:54:57

OK, another road bump in my current project.

好的,我当前项目的另一个路障。

I have never had form elements in both my master and content pages, I tend to have all the forms in the content where relevant.

我的主页和内容页面中都没有表单元素,我倾向于在相关的内容中包含所有表单。

In the current project however, we have a page where they want both. A login form at the top right, and a questions form in the content.

然而,在当前的项目中,我们有一个他们想要的页面。右上角的登录表单,以及内容中的问题表单。

Having tried to get this in, I have run in to the issue of ASP.NET moaning about the need for a single form element in a master page. TBH, I really dont get why this is a requirement on ASP.NET's part, but hey ho.

在尝试了解之后,我遇到了ASP.NET抱怨在母版页中需要单个表单元素的问题。 TBH,我真的不明白为什么这是ASP.NET的要求,但是嘿嘿。

Does anyone know if/how I can get the master and content pages to contain form elements that work independantly?

有谁知道我是否/如何让主页和内容页面包含独立工作的表单元素?

If not, can you offer advice on how to proceed to get the desired look/functionality?

如果没有,您能提供有关如何获得所需外观/功能的建议吗?

12 个解决方案

#1


19  

Thought I would review some of my outstanding questions and see if I can close some of them off.

以为我会回顾一些我未解决的问题,看看我是否可以关闭其中的一些问题。

This one was an interesting one. I outright refused to believe you can only have one form on an ASP.NET page. This to me made no sense. I have seen plenty of webpages that have more than one form on a web page, why should an ASP.NET page be any different?

这是一个有趣的。我完全拒绝相信你只能在ASP.NET页面上有一个表单。这对我来说毫无意义。我看到很多网页在网页上有多个表单,为什么ASP.NET页面会有所不同?

So, it got me thinking.

所以,它让我思考。

Why does a ASP.NET page need a form element?

ASP.NET pages try to emulate the WinForms environment, by provided state persistance through the PostBack model. This provides an element of state to a stateless environment. In order to do this, the runtime needs to be able to have the ability to maintain this state within each "form". It does this by posting back data to itself. It's important to note that:

ASP.NET页面尝试通过PostBack模型提供状态持久性来模拟WinForms环境。这为无状态环境提供了状态元素。为此,运行时需要能够在每个“表单”中维护此状态。它通过将数据发布到自身来实现这一点。重要的是要注意:

  • There is nothing really fancy about a PostBack.
  • 关于PostBack没有什么真正的花哨。
  • It uses a HTTP form and POST, the same as any other form, from any other stack.
  • 它使用HTTP表单和POST,与任何其他表单相同,来自任何其他堆栈。
  • Just because it looks like it might be doing something special, its not, all that happens is it POST's back with some info about what caused it, so you can do things like handle client-side events, in server-side code.
  • 只是因为看起来它可能正在做一些特别的事情,它不是,所有这一切都发生在POST的后面,有一些关于是什么导致它的信息,所以你可以做一些事情,比如在服务器端代码中处理客户端事件。

So why only one?

This to me was the million pound question (I am British). I understand that ASP.NET needs this, especially if you are using ASP.NET server controls, but why the hell can't I make my own additional forms?

对我来说这是一个百万英镑的问题(我是英国人)。我知道ASP.NET需要这个,特别是如果你使用的是ASP.NET服务器控件,但为什么我不能自己创建其他表单呢?

So, I thought screw it, just make your own form!

所以,我认为搞砸了,只做自己的形式!

And I did. I added a bog-standard, simple form with a submit action of "#". This then performs a POST to the current page, with the Form data for the given form in the request.

我做到了。我添加了一个标准的,简单的形式,提交动作为“#”。然后,它会对当前页面执行POST,并在请求中使用给定表单的表单数据。

Guess what? It all worked fine. So I ended up with:

你猜怎么了?一切都很好。所以我最终得到了:

  • A master page, with a HTML form in
  • 一个母版页,其中包含一个HTML表单
  • This form posts back to the current page (basically the page using the master).
  • 此表单回发到当前页面(基本上是使用主页面的页面)。
  • In the Page_Load code-behind for the master, I then added code to check the request to see what data was passed in the request. If it contains data (say a hidden field) then I know the post was sourced from the Form on the master page, if not, then it is most liekly a PostBack from content, and can be ignored.
  • 在主页的Page_Load代码隐藏中,我添加了代码以检查请求以查看请求中传递了哪些数据。如果它包含数据(比如隐藏字段),那么我知道帖子来自母版页面上的表格,如果没有,那么它最可能是来自内容的PostBack,可以忽略。
  • I then surrounded the Content tags with <form runat="server" id="aspNetForm"...> </form> tags. This meant that all content pages automatically had a form to work with.
  • 然后我用
    标签包围了Content标签。这意味着所有内容页面都自动拥有可以使用的表单。

This provided me with a relatively simple, clean solution to my problem. My login form works fine in tandem with all the content forms created, some of which are complex forms, others use lots of server controls and many PostBacks, and so on.

这为我的问题提供了一个相对简单,干净的解决方案。我的登录表单与创建的所有内容表单一起工作正常,其中一些是复杂的表单,另一些使用大量的服务器控件和许多PostBack,等等。

I hope this helps others.

我希望这有助于其他人。

#2


5  

the form tag itself is in the MasterPage, as such, you can code any asp.net server controls onto the master page that you wish. And you can write up the processing logic for those server controls on the master page's code behind file.

表单标签本身在MasterPage中,因此,您可以将任何asp.net服务器控件编码到您希望的母版页上。您可以在母版页的代码隐藏文件上为这些服务器控件编写处理逻辑。

So, in your example, you can have the login controls on the upper right of the master page, and then have the authentication logic in the code page for the MASTER PAGE, not your content page.

因此,在您的示例中,您可以在主页面的右上角安装登录控件,然后在MASTER PAGE的代码页中使用身份验证逻辑,而不是内容页面。

This allows you to have the login controls on every page, and maintain that processing, as well as maintain the content controls and their processing on their individual pages.

这允许您在每个页面上拥有登录控件,并维护该处理,以及维护内容控件及其在各个页面上的处理。

#3


4  

Everyone else has already mentioned that you can only have a single form element in a given ASP.NET page, and that it would be contained in the master page. So far, so good. But I don't think that helps you get fully where you want to be ...

其他人都已经提到过,在给定的ASP.NET页面中只能有一个表单元素,并且它将包含在母版页中。到现在为止还挺好。但我认为这不能帮助你完全达到你想要的地方......

In your master pages, you've (I assume!) defined asp:ContentPlaceHolder controls. Your pages which use the master then have corresponding asp:Content tags. All your page content must go in these corresponding asp:Content tags.

在您的母版页中,您(我假设!)定义了asp:ContentPlaceHolder控件。然后,使用master的页面会有相应的asp:Content标签。您的所有页面内容都必须包含在这些相应的asp:Content标记中。

Once in that tag, they are part of the master page's form. The master page can respond to events from its own controls, and the pages themselves respond to events from their own controls, and you're set.

一旦进入该标签,它们就是母版页表格的一部分。母版页可以响应来自其自身控件的事件,页面本身会响应来自自己控件的事件,并且您已设置好。

If you need the page to interact with the master page, you can access it via the Page.Master property. To interact with any publicly-visible code (methods, properties, etc.) from the master page, you'd cast this property to the correct type, and access the publicly-visible code from there.

如果您需要页面与母版页进行交互,则可以通过Page.Master属性访问它。要与母版页中任何公开可见的代码(方法,属性等)进行交互,您需要将此属性转换为正确的类型,并从那里访问公开可见的代码。

That should get you where you need to be in this scenario. (It's worked for me on multiple sites!)

这应该可以让您在这种情况下需要。 (它在多个站点上对我有用!)

#4


4  

Rob,

抢,

Interesting solution. I don't see any problem with what you are doing. The problem some may encounter however, is if they try to do this with 2 server forms. There's no rule in ASP.NET that you can't have more than 1 HTML form on a page, just that you can't have more than one "runat='server'" form on the page. Obviously you've found a pretty easy way of meeting your needs.

有趣的解决方案。我认为你在做什么没有任何问题。然而,有些人可能遇到的问题是,如果他们尝试使用2个服务器表单执行此操作。在ASP.NET中没有规则,您在页面上不能有超过1个HTML表单,只是您不能在页面上有多个“runat ='server'”表单。显然,您已经找到了一种满足您需求的简单方法。

I've found that for the most part dealing with a single form is not a problem because the ASP.NET framework basically separates everything for us with naming containers. But in your initial post comment you hit on the important factor that was absent yet critical to the essence of the original question: enter key behavior. That always throws a monkey wrench into the works.

我发现在大多数情况下处理单个表单不是问题,因为ASP.NET框架基本上将命名容器与我们分开。但是在你最初的评论中,你找到了一个重要的因素,这个因素对于原始问题的本质是不可能的:输入关键行为。这总是会让猴子扳手投入工作。

If you were to use a standard "all encompassing" server form, couldn't you capture the right action using a textbox text changed event? Of course, if the user changed both values before hitting enter on either you would get strange behavior. And I think the core problem with the enter key is that once you have more than one submit input on an HTML form, hitting ENTER in a textbox doesn't do anything. Only when there is a single INPUT element does the enter key cause one to be "clicked".

如果您使用标准的“全包”服务器表单,您是否无法使用文本框文本更改事件捕获正确的操作?当然,如果用户在点击输入之前改变了两个值,你会得到奇怪的行为。我认为enter键的核心问题是,一旦你在HTML表单上有多个提交输入,在文本框中点击ENTER就不会做任何事情。只有当存在单个INPUT元素时,输入键才会导致“单击”。

#5


3  

None of the previous answers gave a code example. Here's a simplified version of the Visual Studio 2012 Site.Master that illustrates how to do this:

之前的答案都没有给出代码示例。这是Visual Studio 2012 Site.Master的简化版本,它说明了如何执行此操作:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site - Copy.Master.cs" Inherits="WebApplication1.Site1Master" %>
<!DOCTYPE html>
<html>
    <head runat="server">
        <title>This is a title</title>
        <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
    </head>
    <body>
    <form runat="server">
    <header>
        <div class="content-wrapper">
            <div class="float-right">
                <section id="login">
                    <asp:LoginView runat="server" ViewStateMode="Disabled">
                        <AnonymousTemplate>
                             <asp:ContentPlaceHolder runat="server" ID="AnonContent" />
                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            <asp:ContentPlaceHolder runat="server" ID="LoggedInContent" />
                        </LoggedInTemplate>
                    </asp:LoginView>
                </section>
            </div>
        </div>
    </header>
        <div id="body">
        <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
        <section class="content-wrapper main-content clear-fix">
            <asp:ContentPlaceHolder runat="server" ID="MainContent" />
        </section>
    </div>
    </form>
</body>
</html>

So you have everything wrapped by a single Form element, so you can place controls in the master page, yet your content pages can also use controls.

因此,您拥有由单个Form元素包装的所有内容,因此您可以将控件放在母版页中,但您的内容页面也可以使用控件。

#6


2  

You can only have one form on an ASP.NET page. One way to handle this is to put an event handler on the login button in the master page. The handler will validate the user and redirect to the same page on success (to correctly run the Page_Load handler, which is run before event handlers).

ASP.NET页面上只能有一个表单。处理此问题的一种方法是在主页面的登录按钮上放置一个事件处理程序。处理程序将验证用户并在成功时重定向到同一页面(以正确运行在事件处理程序之前运行的Page_Load处理程序)。

#7


0  

I solved the "clicking the return key in the login sub-form causes the main form to submit" problem in my current project by embedding an iframe into the master page. The iframe pointed to the login.aspx page which authenticated the user.

我通过在主页中嵌入iframe解决了“在登录子表单中单击返回键导致主表单提交”当前项目中的问题。 iframe指向login.aspx页面,该页面对用户进行了身份验证。

<iframe id="login" src="login.aspx" frameborder="0" enableviewstate="false" scrolling="no" runat="server"></iframe>

(form some reason I needed the closing /iframe tag otherwise design view got confused)

(形成某些原因我需要关闭/ iframe标签,否则设计视图会混淆)

#8


0  

You can access MasterPage controls from the aspx form by: add the detractive tag to the aspx form <%@ MasterType VirtualPath="~/Site.Master %> and in the code behind use Master.FindControl(); to get the control by ID

您可以通过以下方式从aspx表单访问MasterPage控件:将detractive标签添加到aspx表单<%@ MasterType VirtualPath =“〜/ Site.Master%>并在后面的代码中使用Master.FindControl();来获取控件ID

for Example if you want to get the Control form = Master.FindControl("form")

例如,如果你想获得Control form = Master.FindControl(“form”)

now you can use the master page's form in your code.

现在您可以在代码中使用母版页的表单。

I hope this help.

我希望这有帮助。

#9


0  

Salve! In a similar thread, I posted an answer that might help you. You can use jquery to add content to an empty div. That content can include form tags, and even a submit function independant of anything the server-side code is doing. The only downside to this is if the user does not have javascript enabled!

药膏!在类似的帖子中,我发布了一个可能对您有帮助的答案。您可以使用jquery将内容添加到空div。该内容可以包括表单标签,甚至是一个提交函数,它独立于服务器端代码所做的任何事情。唯一的缺点是如果用户没有启用javascript!

Instead of reposting the same answer (and the code too), here is the link:

这里是链接,而不是重新发布相同的答案(以及代码):

Jquery Ajax loading form on asp.net webform

在asp.net webform上加载表单的Jquery Ajax

#10


0  

This is a limitation of ASP.NET

这是ASP.NET的限制

ASP.NET is designed to have one form per page and only one form. When it was originally designed that was not a problem.

ASP.NET旨在每页只有一个表单,只有一个表单。当它最初设计时,这不是一个问题。

However since then this has been identified as a huge problem with accessibility.

然而,从那以后,这被认为是可访问性的一个巨大问题。

Microsoft Fix for this was ASP.NET MVC, if you are able to I would suggest considering moving to ASP.NET MVC as it solves a large number of problems with ASP.NET

Microsoft Fix for this是ASP.NET MVC,如果你能够我建议考虑转移到ASP.NET MVC,因为它解决了ASP.NET的大量问题

#11


-2  

You can have more than 1 form. (just only 1 visiable at a time) codeline 1 = form 1 visable / form 2 hidden . Code 2 Form 2 visable / form 1 hidden. = solved (this is great for static contact forms as well

您可以拥有多个表单。 (一次只能看到1个)代码行1 =形式1可见/形式2隐藏。代码2表格2可见/表格1隐藏。 =已解决(这对于静态联系表单也很有用

#12


-3  

no, you can only have one asp.net form per page. That has been the rule since 1.0

不,每页只能有一个asp.net表单。这是自1.0以来的规则

They should both share the same form

他们应该共享相同的形式

#1


19  

Thought I would review some of my outstanding questions and see if I can close some of them off.

以为我会回顾一些我未解决的问题,看看我是否可以关闭其中的一些问题。

This one was an interesting one. I outright refused to believe you can only have one form on an ASP.NET page. This to me made no sense. I have seen plenty of webpages that have more than one form on a web page, why should an ASP.NET page be any different?

这是一个有趣的。我完全拒绝相信你只能在ASP.NET页面上有一个表单。这对我来说毫无意义。我看到很多网页在网页上有多个表单,为什么ASP.NET页面会有所不同?

So, it got me thinking.

所以,它让我思考。

Why does a ASP.NET page need a form element?

ASP.NET pages try to emulate the WinForms environment, by provided state persistance through the PostBack model. This provides an element of state to a stateless environment. In order to do this, the runtime needs to be able to have the ability to maintain this state within each "form". It does this by posting back data to itself. It's important to note that:

ASP.NET页面尝试通过PostBack模型提供状态持久性来模拟WinForms环境。这为无状态环境提供了状态元素。为此,运行时需要能够在每个“表单”中维护此状态。它通过将数据发布到自身来实现这一点。重要的是要注意:

  • There is nothing really fancy about a PostBack.
  • 关于PostBack没有什么真正的花哨。
  • It uses a HTTP form and POST, the same as any other form, from any other stack.
  • 它使用HTTP表单和POST,与任何其他表单相同,来自任何其他堆栈。
  • Just because it looks like it might be doing something special, its not, all that happens is it POST's back with some info about what caused it, so you can do things like handle client-side events, in server-side code.
  • 只是因为看起来它可能正在做一些特别的事情,它不是,所有这一切都发生在POST的后面,有一些关于是什么导致它的信息,所以你可以做一些事情,比如在服务器端代码中处理客户端事件。

So why only one?

This to me was the million pound question (I am British). I understand that ASP.NET needs this, especially if you are using ASP.NET server controls, but why the hell can't I make my own additional forms?

对我来说这是一个百万英镑的问题(我是英国人)。我知道ASP.NET需要这个,特别是如果你使用的是ASP.NET服务器控件,但为什么我不能自己创建其他表单呢?

So, I thought screw it, just make your own form!

所以,我认为搞砸了,只做自己的形式!

And I did. I added a bog-standard, simple form with a submit action of "#". This then performs a POST to the current page, with the Form data for the given form in the request.

我做到了。我添加了一个标准的,简单的形式,提交动作为“#”。然后,它会对当前页面执行POST,并在请求中使用给定表单的表单数据。

Guess what? It all worked fine. So I ended up with:

你猜怎么了?一切都很好。所以我最终得到了:

  • A master page, with a HTML form in
  • 一个母版页,其中包含一个HTML表单
  • This form posts back to the current page (basically the page using the master).
  • 此表单回发到当前页面(基本上是使用主页面的页面)。
  • In the Page_Load code-behind for the master, I then added code to check the request to see what data was passed in the request. If it contains data (say a hidden field) then I know the post was sourced from the Form on the master page, if not, then it is most liekly a PostBack from content, and can be ignored.
  • 在主页的Page_Load代码隐藏中,我添加了代码以检查请求以查看请求中传递了哪些数据。如果它包含数据(比如隐藏字段),那么我知道帖子来自母版页面上的表格,如果没有,那么它最可能是来自内容的PostBack,可以忽略。
  • I then surrounded the Content tags with <form runat="server" id="aspNetForm"...> </form> tags. This meant that all content pages automatically had a form to work with.
  • 然后我用
    标签包围了Content标签。这意味着所有内容页面都自动拥有可以使用的表单。

This provided me with a relatively simple, clean solution to my problem. My login form works fine in tandem with all the content forms created, some of which are complex forms, others use lots of server controls and many PostBacks, and so on.

这为我的问题提供了一个相对简单,干净的解决方案。我的登录表单与创建的所有内容表单一起工作正常,其中一些是复杂的表单,另一些使用大量的服务器控件和许多PostBack,等等。

I hope this helps others.

我希望这有助于其他人。

#2


5  

the form tag itself is in the MasterPage, as such, you can code any asp.net server controls onto the master page that you wish. And you can write up the processing logic for those server controls on the master page's code behind file.

表单标签本身在MasterPage中,因此,您可以将任何asp.net服务器控件编码到您希望的母版页上。您可以在母版页的代码隐藏文件上为这些服务器控件编写处理逻辑。

So, in your example, you can have the login controls on the upper right of the master page, and then have the authentication logic in the code page for the MASTER PAGE, not your content page.

因此,在您的示例中,您可以在主页面的右上角安装登录控件,然后在MASTER PAGE的代码页中使用身份验证逻辑,而不是内容页面。

This allows you to have the login controls on every page, and maintain that processing, as well as maintain the content controls and their processing on their individual pages.

这允许您在每个页面上拥有登录控件,并维护该处理,以及维护内容控件及其在各个页面上的处理。

#3


4  

Everyone else has already mentioned that you can only have a single form element in a given ASP.NET page, and that it would be contained in the master page. So far, so good. But I don't think that helps you get fully where you want to be ...

其他人都已经提到过,在给定的ASP.NET页面中只能有一个表单元素,并且它将包含在母版页中。到现在为止还挺好。但我认为这不能帮助你完全达到你想要的地方......

In your master pages, you've (I assume!) defined asp:ContentPlaceHolder controls. Your pages which use the master then have corresponding asp:Content tags. All your page content must go in these corresponding asp:Content tags.

在您的母版页中,您(我假设!)定义了asp:ContentPlaceHolder控件。然后,使用master的页面会有相应的asp:Content标签。您的所有页面内容都必须包含在这些相应的asp:Content标记中。

Once in that tag, they are part of the master page's form. The master page can respond to events from its own controls, and the pages themselves respond to events from their own controls, and you're set.

一旦进入该标签,它们就是母版页表格的一部分。母版页可以响应来自其自身控件的事件,页面本身会响应来自自己控件的事件,并且您已设置好。

If you need the page to interact with the master page, you can access it via the Page.Master property. To interact with any publicly-visible code (methods, properties, etc.) from the master page, you'd cast this property to the correct type, and access the publicly-visible code from there.

如果您需要页面与母版页进行交互,则可以通过Page.Master属性访问它。要与母版页中任何公开可见的代码(方法,属性等)进行交互,您需要将此属性转换为正确的类型,并从那里访问公开可见的代码。

That should get you where you need to be in this scenario. (It's worked for me on multiple sites!)

这应该可以让您在这种情况下需要。 (它在多个站点上对我有用!)

#4


4  

Rob,

抢,

Interesting solution. I don't see any problem with what you are doing. The problem some may encounter however, is if they try to do this with 2 server forms. There's no rule in ASP.NET that you can't have more than 1 HTML form on a page, just that you can't have more than one "runat='server'" form on the page. Obviously you've found a pretty easy way of meeting your needs.

有趣的解决方案。我认为你在做什么没有任何问题。然而,有些人可能遇到的问题是,如果他们尝试使用2个服务器表单执行此操作。在ASP.NET中没有规则,您在页面上不能有超过1个HTML表单,只是您不能在页面上有多个“runat ='server'”表单。显然,您已经找到了一种满足您需求的简单方法。

I've found that for the most part dealing with a single form is not a problem because the ASP.NET framework basically separates everything for us with naming containers. But in your initial post comment you hit on the important factor that was absent yet critical to the essence of the original question: enter key behavior. That always throws a monkey wrench into the works.

我发现在大多数情况下处理单个表单不是问题,因为ASP.NET框架基本上将命名容器与我们分开。但是在你最初的评论中,你找到了一个重要的因素,这个因素对于原始问题的本质是不可能的:输入关键行为。这总是会让猴子扳手投入工作。

If you were to use a standard "all encompassing" server form, couldn't you capture the right action using a textbox text changed event? Of course, if the user changed both values before hitting enter on either you would get strange behavior. And I think the core problem with the enter key is that once you have more than one submit input on an HTML form, hitting ENTER in a textbox doesn't do anything. Only when there is a single INPUT element does the enter key cause one to be "clicked".

如果您使用标准的“全包”服务器表单,您是否无法使用文本框文本更改事件捕获正确的操作?当然,如果用户在点击输入之前改变了两个值,你会得到奇怪的行为。我认为enter键的核心问题是,一旦你在HTML表单上有多个提交输入,在文本框中点击ENTER就不会做任何事情。只有当存在单个INPUT元素时,输入键才会导致“单击”。

#5


3  

None of the previous answers gave a code example. Here's a simplified version of the Visual Studio 2012 Site.Master that illustrates how to do this:

之前的答案都没有给出代码示例。这是Visual Studio 2012 Site.Master的简化版本,它说明了如何执行此操作:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site - Copy.Master.cs" Inherits="WebApplication1.Site1Master" %>
<!DOCTYPE html>
<html>
    <head runat="server">
        <title>This is a title</title>
        <asp:ContentPlaceHolder runat="server" ID="HeadContent" />
    </head>
    <body>
    <form runat="server">
    <header>
        <div class="content-wrapper">
            <div class="float-right">
                <section id="login">
                    <asp:LoginView runat="server" ViewStateMode="Disabled">
                        <AnonymousTemplate>
                             <asp:ContentPlaceHolder runat="server" ID="AnonContent" />
                        </AnonymousTemplate>
                        <LoggedInTemplate>
                            <asp:ContentPlaceHolder runat="server" ID="LoggedInContent" />
                        </LoggedInTemplate>
                    </asp:LoginView>
                </section>
            </div>
        </div>
    </header>
        <div id="body">
        <asp:ContentPlaceHolder runat="server" ID="FeaturedContent" />
        <section class="content-wrapper main-content clear-fix">
            <asp:ContentPlaceHolder runat="server" ID="MainContent" />
        </section>
    </div>
    </form>
</body>
</html>

So you have everything wrapped by a single Form element, so you can place controls in the master page, yet your content pages can also use controls.

因此,您拥有由单个Form元素包装的所有内容,因此您可以将控件放在母版页中,但您的内容页面也可以使用控件。

#6


2  

You can only have one form on an ASP.NET page. One way to handle this is to put an event handler on the login button in the master page. The handler will validate the user and redirect to the same page on success (to correctly run the Page_Load handler, which is run before event handlers).

ASP.NET页面上只能有一个表单。处理此问题的一种方法是在主页面的登录按钮上放置一个事件处理程序。处理程序将验证用户并在成功时重定向到同一页面(以正确运行在事件处理程序之前运行的Page_Load处理程序)。

#7


0  

I solved the "clicking the return key in the login sub-form causes the main form to submit" problem in my current project by embedding an iframe into the master page. The iframe pointed to the login.aspx page which authenticated the user.

我通过在主页中嵌入iframe解决了“在登录子表单中单击返回键导致主表单提交”当前项目中的问题。 iframe指向login.aspx页面,该页面对用户进行了身份验证。

<iframe id="login" src="login.aspx" frameborder="0" enableviewstate="false" scrolling="no" runat="server"></iframe>

(form some reason I needed the closing /iframe tag otherwise design view got confused)

(形成某些原因我需要关闭/ iframe标签,否则设计视图会混淆)

#8


0  

You can access MasterPage controls from the aspx form by: add the detractive tag to the aspx form <%@ MasterType VirtualPath="~/Site.Master %> and in the code behind use Master.FindControl(); to get the control by ID

您可以通过以下方式从aspx表单访问MasterPage控件:将detractive标签添加到aspx表单<%@ MasterType VirtualPath =“〜/ Site.Master%>并在后面的代码中使用Master.FindControl();来获取控件ID

for Example if you want to get the Control form = Master.FindControl("form")

例如,如果你想获得Control form = Master.FindControl(“form”)

now you can use the master page's form in your code.

现在您可以在代码中使用母版页的表单。

I hope this help.

我希望这有帮助。

#9


0  

Salve! In a similar thread, I posted an answer that might help you. You can use jquery to add content to an empty div. That content can include form tags, and even a submit function independant of anything the server-side code is doing. The only downside to this is if the user does not have javascript enabled!

药膏!在类似的帖子中,我发布了一个可能对您有帮助的答案。您可以使用jquery将内容添加到空div。该内容可以包括表单标签,甚至是一个提交函数,它独立于服务器端代码所做的任何事情。唯一的缺点是如果用户没有启用javascript!

Instead of reposting the same answer (and the code too), here is the link:

这里是链接,而不是重新发布相同的答案(以及代码):

Jquery Ajax loading form on asp.net webform

在asp.net webform上加载表单的Jquery Ajax

#10


0  

This is a limitation of ASP.NET

这是ASP.NET的限制

ASP.NET is designed to have one form per page and only one form. When it was originally designed that was not a problem.

ASP.NET旨在每页只有一个表单,只有一个表单。当它最初设计时,这不是一个问题。

However since then this has been identified as a huge problem with accessibility.

然而,从那以后,这被认为是可访问性的一个巨大问题。

Microsoft Fix for this was ASP.NET MVC, if you are able to I would suggest considering moving to ASP.NET MVC as it solves a large number of problems with ASP.NET

Microsoft Fix for this是ASP.NET MVC,如果你能够我建议考虑转移到ASP.NET MVC,因为它解决了ASP.NET的大量问题

#11


-2  

You can have more than 1 form. (just only 1 visiable at a time) codeline 1 = form 1 visable / form 2 hidden . Code 2 Form 2 visable / form 1 hidden. = solved (this is great for static contact forms as well

您可以拥有多个表单。 (一次只能看到1个)代码行1 =形式1可见/形式2隐藏。代码2表格2可见/表格1隐藏。 =已解决(这对于静态联系表单也很有用

#12


-3  

no, you can only have one asp.net form per page. That has been the rule since 1.0

不,每页只能有一个asp.net表单。这是自1.0以来的规则

They should both share the same form

他们应该共享相同的形式

相关文章