ASP.NET 母版页和内容页中的表单元素 [英] Form Elements in ASP.NET Master Pages and Content Pages

查看:21
本文介绍了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.

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

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.

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

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

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

推荐答案

我想回顾一下我的一些悬而未决的问题,看看是否可以关闭其中的一些.

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

这个很有趣.我完全拒绝相信在 ASP.NET 页面上只能有一个表单.这对我来说毫无意义.我见过很多网页在一个网页上有多个表单,为什么 ASP.NET 页面应该有所不同?

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.

ASP.NET 页面尝试模拟 WinForms 环境,通过 PostBack 模型提供状态持久性.这为无状态环境提供了状态元素.为了做到这一点,运行时需要能够在每个表单"内保持这种状态.它通过将数据回传给自身来做到这一点.需要注意的是:

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:

  • PostBack 没什么特别的.
  • 它使用 HTTP 形式和 POST,与任何其他形式一样,来自任何其他堆栈.
  • 仅仅因为它看起来可能正在做一些特别的事情,其实不然,所有发生的事情都是 POST 返回一些有关导致它的原因的信息,因此您可以在服务器端代码中执行诸如处理客户端事件之类的操作.

这对我来说是百万英镑的问题(我是英国人).我知道 ASP.NET 需要这个,尤其是当您使用 ASP.NET 服务器控件时,但是为什么我不能制作自己的附加表单?

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!

我做到了.我添加了一个带有#"提交操作的沼泽标准的简单表单.然后使用请求中给定表单的表单数据对当前页面执行 POST.

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:

  • 一个母版页,其中包含一个 HTML 表单
  • 此表单回发到当前页面(基本上是使用母版的页面).
  • 在主页面的 Page_Load 代码隐藏中,我添加了代码来检查请求以查看请求中传递了哪些数据.如果它包含数据(比如隐藏字段),那么我知道该帖子来自母版页上的表单,如果没有,那么它很可能是来自内容的回发,可以忽略.
  • 然后我用
    </form> 标签.这意味着所有内容页面都会自动使用一个表单来处理.
  • A master page, with a HTML form in
  • 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.
  • 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.

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

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.

这篇关于ASP.NET 母版页和内容页中的表单元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆