ASP.NET 母版页和内容页中的表单元素
好的,我当前项目中的另一个障碍。
我的母版页和内容页中从未有过表单元素,我倾向于在相关的内容中包含所有表单。
然而,在当前的项目中,我们有一个他们想要两者的页面。 右上角有一个登录表单,内容中有一个问题表单。
在尝试将其加入后,我遇到了 ASP.NET 抱怨需要单个表单元素的问题在母版页中。 说实话,我真的不明白为什么这是 ASP.NET 的要求,但是嘿嘿。
有谁知道我是否/如何使母版页和内容页包含独立工作的表单元素?
如果没有,您能否提供有关如何继续获得所需外观/功能的建议?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我想我应该回顾一下我的一些悬而未决的问题,看看是否可以解决其中的一些问题。
这是一件有趣的事。 我完全不相信 ASP.NET 页面上只能有一个表单。 这对我来说毫无意义。 我见过很多网页上有多个表单,为什么 ASP.NET 页面应该有所不同?
所以,这让我开始思考。
为什么 ASP.NET 页面需要表单元素?
ASP.NET 页面尝试通过 PostBack 模型提供状态持久性来模拟 WinForms 环境。 这为无状态环境提供了状态元素。 为了做到这一点,运行时需要能够在每个“表单”内维护此状态。 它通过将数据发回给自身来实现这一点。 需要注意的是:
那么为什么只有一个呢?
这对我来说是一个百万英镑的问题(我是英国人)。 我知道 ASP.NET 需要这个,特别是如果您使用 ASP.NET 服务器控件,但是为什么我不能制作自己的附加表单?
所以,我想搞砸了,只需制作你自己的形式!
我做到了。 我添加了一个沼泽标准的简单表单,其中提交操作为“#”。 然后,这将使用请求中给定表单的表单数据对当前页面执行 POST。
你猜怎么了? 一切都很好。 所以我最终得到:
标签。 这意味着所有内容页面都会自动有一个可以使用的表单。
这为我的问题提供了一个相对简单、干净的解决方案。 我的登录表单与创建的所有内容表单配合得很好,其中一些是复杂的表单,另一些则使用大量服务器控件和许多回发,等等。
我希望这对其他人有帮助。
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?
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:
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?
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.
Guess what? It all worked fine. So I ended up with:
<form runat="server" id="aspNetForm"...> </form>
tags. This meant that all content pages automatically had a form to work with.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.
I hope this helps others.
表单标签本身位于 MasterPage 中,因此,您可以将任何 ASP.NET 服务器控件编码到您希望的母版页上。 您可以在母版页的代码隐藏文件中编写这些服务器控件的处理逻辑。
因此,在您的示例中,您可以将登录控件放在母版页的右上角,然后将身份验证逻辑放在母版页的代码页中,而不是内容页中。
这允许您在每个页面上拥有登录控件,并维护该处理,以及维护内容控件及其在各个页面上的处理。
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.
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.
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.
其他人都已经提到过,在给定的 ASP.NET 页面中只能有一个表单元素,并且它将包含在母版页中。 到目前为止,一切都很好。 但我不认为这可以帮助您完全到达您想要的位置...
在您的母版页中,您已经(我假设!)定义了
asp:ContentPlaceHolder
控件。 使用母版的页面将具有相应的asp:Content
标记。 您的所有页面内容都必须位于这些相应的asp:Content
标记中。一旦进入该标签,它们就成为母版页表单的一部分。 母版页可以响应来自其自己的控件的事件,并且页面本身也响应来自其自己的控件的事件,这样就可以了。
如果您需要页面与母版页交互,可以通过
Page.Master
属性访问它。 要与母版页中的任何公开可见的代码(方法、属性等)进行交互,您需要将此属性转换为正确的类型,并从那里访问公开可见的代码。在这种情况下,这应该可以帮助您到达所需的位置。 (它在多个网站上对我有用!)
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 ...
In your master pages, you've (I assume!) defined
asp:ContentPlaceHolder
controls. Your pages which use the master then have correspondingasp:Content
tags. All your page content must go in these correspondingasp:Content
tags.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.That should get you where you need to be in this scenario. (It's worked for me on multiple sites!)
罗布,
有趣的解决方案。 我不认为你所做的有任何问题。 然而,有些人可能会遇到的问题是,如果他们尝试使用 2 个服务器表单来执行此操作。 ASP.NET 中没有规定一页上不能有超过 1 个 HTML 表单,只是页面上不能有超过一个“runat='server'”表单。 显然,您已经找到了一种非常简单的方法来满足您的需求。
我发现在大多数情况下处理单个表单不是问题,因为 ASP.NET 框架基本上通过命名容器为我们分隔了所有内容。 但在您最初的评论中,您提到了一个重要因素,该因素虽然不存在,但对原始问题的本质至关重要:输入关键行为。 这总是会给工作带来麻烦。
如果您要使用标准的“无所不包”服务器表单,您是否无法使用文本框文本更改事件捕获正确的操作? 当然,如果用户在按回车键之前更改了这两个值,您会得到奇怪的行为。 我认为 Enter 键的核心问题是,一旦您在 HTML 表单上提交了多个输入,在文本框中按 ENTER 键不会执行任何操作。 仅当存在单个 INPUT 元素时,回车键才会导致“单击”元素。
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.
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.
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".
之前的答案都没有给出代码示例。 下面是 Visual Studio 2012 Site.Master 的简化版本,说明了如何执行此操作:
因此,您可以将所有内容都包含在单个 Form 元素中,这样您就可以将控件放置在母版页中,但您的内容页也可以使用控件。
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:
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.
一个 ASP.NET 页面上只能有一个表单。 处理此问题的一种方法是在母版页的登录按钮上放置一个事件处理程序。 该处理程序将验证用户并在成功时重定向到同一页面(以正确运行 Page_Load 处理程序,该处理程序在事件处理程序之前运行)。
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).
我通过将 iframe 嵌入到母版页中解决了当前项目中的“单击登录子表单中的返回键导致主表单提交”问题。 iframe 指向对用户进行身份验证的login.aspx 页面。
(出于某种原因,我需要结束 /iframe 标签,否则设计视图会感到困惑)
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.
(form some reason I needed the closing /iframe tag otherwise design view got confused)
您可以通过以下方式从 aspx 表单访问 MasterPage 控件:
将破坏性标签添加到 aspx 表单 <%@ MasterType VirtualPath="~/Site.Master %>
并在后面的代码中使用 Master.FindControl(); 通过 ID 获取
示例的控件(如果您想获取
Control form = Master.FindControl("form")
现在您可以在代码中使用母版页的表单。
我希望这有帮助。
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
for Example if you want to get the
Control form = Master.FindControl("form")
now you can use the master page's form in your code.
I hope this help.
药膏! 在类似的帖子中,我发布了一个可能对您有帮助的答案。 您可以使用 jquery 将内容添加到空 div。 该内容可以包括表单标签,甚至包括独立于服务器端代码正在执行的任何操作的提交函数。 唯一的缺点是用户没有启用 JavaScript!
不要重新发布相同的答案(以及代码),而是链接:
Jquery Ajax 在 asp.net web 表单上加载表单
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!
Instead of reposting the same answer (and the code too), here is the link:
Jquery Ajax loading form on asp.net webform
这是 ASP.NET 的限制
ASP.NET 被设计为每页有一个表单,并且只有一个表单。 最初设计时这不是问题。
然而从那时起,这已被认为是可访问性的一个巨大问题。
Microsoft 对此的修复是 ASP.NET MVC,如果可以的话,我建议考虑迁移到 ASP.NET MVC,因为它解决了 ASP.NET 的大量问题
This is a limitation of 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.
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
您可以有超过 1 个表格。 (一次只有 1 个可见)代码行 1 = form 1visable / form 2hidden 。 代码 2 表 2 可见/表 1 隐藏。 = 已解决(这对于静态接触形式也非常有用
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
不可以,每一页只能有一个 ASP.NET 表单。
这是从 1.0 开始的规则
他们应该共享相同的形式
no, you can only have one asp.net form per page.
That has been the rule since 1.0
They should both share the same form