如何在页面中使用一项PostBackUrl与母版 [英] How to use PostbackUrl in Pages with Masterpage

查看:209
本文介绍了如何在页面中使用一项PostBackUrl与母版的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能给我一项PostBackUrl工作的例子,其中两个目标页面和previous页有母版。

Can anyone give me a working example of PostbackUrl where both the target page and previous page have masterpage.

例如,假设让我有两页 default1.axpx default2.aspx 。他们都有一个母版的 MyMasterpage.masterpage

For example, let assume I have two pages default1.axpx and default2.aspx. Both of them have a masterpage MyMasterpage.masterpage

我想从回发的 default1.aspx default2.aspx ,然后在默认2页提取缺省1页控件的数据。

I want to postback from default1.aspx to default2.aspx and then extract data from default1 page controls in default2 page.

我怎样才能做到这一点?

How can I do that?

推荐答案

您应该已经题为这个问题:我如何找到一个控件的ContentPlaceHolder ?,因为你的问题不在于 previousPage 不工作,那就是你不知道如何的ContentPlaceHolder 取值工作。

You should have titled this question "How do I find a control in a ContentPlaceholder?", because your problem is not that PreviousPage doesn't work, it's that you don't understand how ContentPlaceholders work.

这问题无关与母版页本身,而完全与使用的ContentPlaceHolder ,这是在asp.net说法命名容器。 FindControls 里面的命名容器,而这正是他们如何设计不搜索。​​

This problem has nothing to do with a master page per se, and is entirely related to using a ContentPlaceholder, which is a Naming Container in asp.net parlance. FindControls does not search inside Naming Containers, which is exactly how they are designed.

previousPage 正常工作与母版页,因此我混淆他们与你的问题做什么。您可以在您需要的previous页面上访问任何财产,这将在事实上的工作。例如:

PreviousPage works fine with master pages, thus my confusion about what they have to do with your problem. You can access any property on the previous page that you want, and it will in fact work. For example:

 HtmlForm form = PreviousPage.Form; // this works fine
 Control ctrl = PreviousPage.Master.FindControl("TextBox1"); // this works as well

您可能会遇到的问题是,你正在尝试使用的FindControl()来查找内容页特定的控制,而且它不工作,precisely因为你呼吁previousPage的FindControl,而不是你要找的控制中存在的命名容器上。

The problem you are likely encountering is that you're trying to use FindControl() to find a specific control in the content page, and it's not working, precisely because you're calling FindControl on the PreviousPage, and not on the naming container that the control you're looking for exists in.

要找到你想要的控制,你就必须做一个的FindControl 命名容器上。下面code正常工作,假设占位符名为ContentPlaceHolder1。

To find the control you want, you just have to do a FindControl on the naming container. The following code works fine, assuming the placeholder is named ContentPlaceHolder1.

var ph = PreviousPage.Controls[0].FindControl("ContentPlaceHolder1");
var ctl = ph.FindControl("TextBox1");

您可以验证这个问题有什么用以下code,只使用一个单一的页面,查找做 previousPage 控制本身。将一个文本框的名为TextBox1的Default.aspx页面上。然后,在Default.aspx.cs后面的code的的Page_Load 函数把这个code,然后在调试器中运行,并通过它一步。

You can verify that this problem has nothing to do with PreviousPage by using the following code, which uses only a single page and looks for the control on itself. Place a textbox on your Default.aspx page called TextBox1. Then, in the Page_Load function of the code behind of Default.aspx.cs put this code, then run it in the debugger and step through it.

protected void Page_Load(object sender, EventArgs e)
{
    // Following code should find the control, right?  Wrong. It's null
    var ctrl = Page.FindControl("TextBox1"); 

    // assuming your content placeholder in the masterpage is called MainContent, this works.
    var ctrl = Page.Controls[0].FindControl("MainContent").FindControl("TextBox1");
}

所以,请不要去说一些如 previousPage 理所应当,如果网页上有母版不起作用,因为它的工作EXACTLY正如它应该。问题是,你不知道它应该如何工作。了解页面的对象模型是如何工作的。

So please, don't go saying things like "the PreviousPage doesn't work as it should if the pages have masterpage", because it's working EXACTLY as it should. The problem is that you don't know how it should be working. Learn how the page object model works.

这篇关于如何在页面中使用一项PostBackUrl与母版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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