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

查看:200
本文介绍了在ASP.NET母版页和内容页表单元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OK,另一路颠簸在我目前的项目。

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.

有谁知道/我怎样才能得到主人和内容页中包含的工作independantly表单元素?

如果没有,可以为您提供如何进行,以获得所需的外观/功能的建议?

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?

所以,我开始思考。

ASP.NET页面试图通过回传模式效仿的WinForms环境,提供的持久性状态。这提供了状态的无状态环境的元素。为了做到这一点,运行时需要能够向具有到每个形式内保持这种状态的能力。它通过数据回发到自身做到这一点。需要注意的是很重要的:

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:


  • 没有什么真正看中的约回传。

  • 它使用一个HTTP表单和POST,同其他任何形式,任何其他协议栈。

  • 只是因为它看起来像它可能会做一些特别的东西,它不是,所发生的一切是邮政的关于是什么导致它的一些信息回来,所以你可以做这样的事情处理客户端事件,在服务器端$ C $角

这对我来说是百万英镑的问题(我是英国人)。据我所知,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中code-背后的主人,我然后加code进行检查,看是在请求中传递什么样的数据请求。如果它包含的数据(比如一个隐藏字段),那么我知道该职位是从主网页上的表格来源,如果没有,那么它是最liekly由内容回传,可以忽略不计。

  • 然后我包围内容的标签以<形式=服务器ID =aspNetForm...> < /形式为GT; 标记。这意味着所有的内容页面会自动有一个表格的工作。

  • 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天全站免登陆