将母版页表单数据传递到另一个 Web 表单
我有一个主页,上面有一个表单。 它是一个必须始终可见的搜索表单。 单击该表单的按钮时,我希望将表单数据发送到 search.aspx。 问题是,我不知道怎么办。 我无法将表单操作设置为 search.aspx,因为使用主表单的所有其他页面都将转到 search.aspx。 这是我不想要的。
希望有人能帮助我:)
谢谢。
I have a master page with one form on it. It is a search form which must always be visible. When the button of that form is clicked I want the form data to be sent to search.aspx. The problem is, I don't know how. I cannot set the form action to search.aspx, because all my other pages which use the master form will go to search.aspx. This I don't want.
Hope someone can help me out :)
Thnx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为了传递控件“txtSearch”的值,当执行 Server.Transfer 时,您可以执行许多操作,包括通过查询字符串变量传递它或设置会话变量,然后在 Page_Load 事件中检查其中任何一个Search.aspx 的,如果已填充,则调用当用户单击 Search.aspx 页面上的提交按钮时触发的事件。
另外,如果 Search.aspx 文件使用相同的母版页,那么您可以使用 this.Master.FindControl("txtSearch") 来获取控件(如果您在浏览器中生成文件后查看文件的源,您会注意到母版页中的控件实际上并不是通过其 ID 调用的,而是附加了某些内容(即现在可能称为“ctl00_txtSearch”)
In order to pass the values of the control "txtSearch", when Server.Transfer is executed, you could do many things, including passing it via a querystring variable or setting up a session variable, and then check either of those in the Page_Load event of Search.aspx, and if it's populated, call the event that is fired when the user would hit the submit button on the Search.aspx page.
Also, if the Search.aspx file is using the same masterpage, then you can use this.Master.FindControl("txtSearch") to get the control (it you look a the source of the file after it is generated in the browser, you'll notice that controls in the master page aren't really called by their ID, rather that have something appended to them (i.e. it would now possibly be called "ctl00_txtSearch")
您可以在单独的表单中创建搜索表单,并使其使用 GET 而不是 POST。
或者让主表单处理搜索按钮单击并使用 Server.Transfer 转到搜索表单。
You could create your search form in a separate form, and get it to use GET instead of POST.
Either that, or have the master form handle the search button click and use Server.Transfer to go to the search form.
我相信您可以在一页中包含多个表单。 因此,一种表单(您的搜索表单)的操作将设置为 search.aspx,而另一种表单的操作将设置为页面本身。
You can have multiple forms in one page I believe. So one form (your search form) would have its action set to search.aspx and the other would be set for the page itself.
ASP.NET Web 表单页面只有一种表单(通常包含在母版页上)。 您可以将搜索按钮的回发 URL 设置为您的搜索页面..
..或者只是从母版页中的处理程序重定向到它,如下所示:
..或者按照 David Kemp 的建议使用 Server.Transfer。
注意:如果您在搜索页面上使用 Request.Query[@"q"] 来获取查询,则无需使用 Server.UrlDecode() - 它已为您完成。
ASP.NET webform pages only have one form (which would generally be included on the master page). You can set the postback url for the search button to your search page..
..or just redirect to it from the handler in your master page like this:
..or use Server.Transfer as suggested by David Kemp.
Note: If you use Request.Query[@"q"] on your search page to get your query, you don't need to use Server.UrlDecode() - it's done for you.
我会:
我使用了类似的过程,在母版页上设置了 HTML 登录表单。
我在此处发布了一个问题和后续解决方案 - 检查出来:
ASP.NET 母版页和内容页中的表单元素
一旦我明白了它,这似乎是一个非常简单且相当优雅的解决方案。
这样做的好处是您可以完全控制数据发送到搜索表单的方式。 并且您不需要启用将表单数据传输到搜索表单以及所有那些令人讨厌的东西,只需创建一个到搜索页面的新 GET 请求并让它执行它应该执行的操作:)
希望这会有所帮助。
注意:
ASPX 页面上只能有一种带有
runat="server"
的表单。 其他表单必须是 HTML 表单。I would:
I used a similar process with having a HTML login form on the master page.
I posted a question and subsequent solution here - check it out:
Form Elements in ASP.NET Master Pages and Content Pages
Once I got my head round it, it seemed a pretty simple and reasonably elegant solution.
The benefit of this is that you have complete control over how the data is sent to the search form. And you don't need to enable transfer of form data to the search form and all that nasty stuff, just create a new GET request to the search page and let it do what it is supposed to do :)
Hope this helps.
NOTE:
You can only have one form with
runat="server"
on an ASPX page. Additional forms MUST be HTML FORMS.由于您的搜索表单位于母版页中,因此您可以将其构造为包含 2 个表单。 将操作设置为“search.aspx”的搜索表单标记放置在网站其余部分使用的标记之外。
如果页面结构不允许这样做,您可以将提交按钮的 PosbackUrl 设置为指向“search.aspx”。 在这种情况下,需要对“search.aspx”进行编码,以在 PreviousPage 属性中查找表单数据,或使用 Request.Form 访问输入。
Because your search form is in the master page, you can probably structure it to contain 2 forms. Place the search form tags with the action set to "search.aspx" outside of the tag that is used by the rest of the site.
If the structure of the page will not enable this, you can set the submit button's PosbackUrl to point to "search.aspx". In this case, "search.aspx" would need to be coded to look in the PreviousPage property for the form data, or use Request.Form to access the input.