从访问内容页控制与嵌套母版页 [英] Accessing control from content page with nested master pages

查看:213
本文介绍了从访问内容页控制与嵌套母版页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的与此挣扎。现在的问题是只与内容的网页。结果
我试图从一个内容页(Page1.aspx的),在其他内容页(Page2.aspx)来访问一个文本框的值。我不知道它是否是相关的,他们是嵌套母版页的孩子,但我想我会扔在

Page1.aspx的是文本框和一个提交按钮的基本方式。在Page1.aspx的文本框被称为tbFirst。提交按钮具有以下code:

 < ASP:按钮的ID =Button1的=服务器文本=新会员表一项PostBackUrl =Page2.aspx/>

Page2.aspx 是一种新的形式,应该从previous页面文本框的值来填充。
第二行显示<%@ previousPageType VirtualPath =〜/ Page1.aspx的%>结果
出于测试目的,我使用一个标签(lblResult),以显示我的结果。

codebehind看起来是这样的:

 如果(previousPage!= NULL)
        {
            文本框SourceTextBox =
                (文本框)previousPage.FindControl(tbFirst);
            如果(SourceTextBox!= NULL)
            {
                lblResult.Text = SourceTextBox.Text;
            }
            其他
            {
                lblResult.Text =无文本中找到;
            }
        }
        其他
            {
            lblResult.Text =没有找到控制;
            }
        }
        }

问题是,在Page2.aspx标签文本说,没有找到的文本。

我想这是所有相关的信息。任何人有什么想法?我花了整个下午拖网我试过工程论坛并没有什么。


解决方案

  

我不知道它是否是相关的,他们的孩子
  嵌套母版页,但我想我会扔在


母版究竟是什么导致了这个问题。你不能用 Page.FindControl(控件ID)找到一个母版页上的控件,因为页面是不是的文本框但NamingContainer 中的的ContentPlaceHolder 。在唯一的控制页面的的ControlCollection 母版用的是母版本身。

原因:我最近回答说,描述这种行为问题

这里有一些方法如何从第二页访问文本框:


  1. 您可能有运气与以下办法(最直接的的FindControl方法)

      Page.Master.FindControl(ContentPlaceHolder1)的FindControl(tbFirst);


  2. 另外,更好的方法是,以提供第1页的回报 tbFirst.Text 的公共属性。然后,你可以从访问它以下列方式第2页

     如果(previousPage = NULL&放大器;!&安培; previousPage是第1页){
        lblResult.Text =((第1页)previousPage).TbFirstText;
    }


  3. 您也可以添加文本作为URL的参数,因此,它不是必需的第2页的 previousPage 第1页


  4. 最后但并非最不重要。如果您使用 Server.Transer与 preserveFor​​m 设置为星期二,你就可以通过引用恢复原始页面文本框控件的值的Request.Form(TbFirst)


注意:我不建议递归的FindControl 方法(从<$开始C $ C>母版),因为它也将硬线两页,并会


  • 严重错误的原因


  • 不透明

I'm really struggling with this. The problem is only with the content pages.
I am trying to access a Text Box value from one content page ("Page1.aspx") in another content page ("Page2.aspx"). I'm not sure whether it is relevant that they are the children of nested master pages, but I thought I'd throw it in.

Page1.aspx is a basic form with text boxes and a submit button. The text box in Page1.aspx is called "tbFirst". The submit button has the following code:

<asp:Button ID="Button1" runat="server" Text="New Member Form" PostBackUrl="Page2.aspx"/>    

Page2.aspx is a new form which should be populated with a textbox value from the previous page. The second line show <%@ PreviousPageType VirtualPath="~/Page1.aspx" %>
For testing purposes I am using a label ("lblResult") to display my results.

Codebehind looks like this:

        if (PreviousPage != null)
        {
            TextBox SourceTextBox =
                (TextBox)PreviousPage.FindControl("tbFirst");
            if (SourceTextBox != null)
            {
                lblResult.Text = SourceTextBox.Text;
            }
            else
            {
                lblResult.Text = "No text found";
            }  
        }
        else
            {
            lblResult.Text = "No Control found";
            }
        }
        }

The problem is that the label text in Page2.aspx says "No text found".

I think that's all the relevant info. Anyone got any ideas? I've spent the whole afternoon trawling the forums and nothing I've tried works.

解决方案

I'm not sure whether it is relevant that they are the children of nested master pages, but I thought I'd throw it in.

The MasterPage is exactly what's causing this issue. You cannot find a control on a page with MasterPage by using Page.FindControl("ControlID"), because the Page is not the NamingContainer of the TextBox but the ContentPlaceholder. The only control in the page's ControlCollection with MasterPage is the MasterPage itself.

Reason: I've recently answered a question that describes this behaviour.

Here are some ways how you can access the TextBox from Page2:

  1. You might have luck with following approach(the most direct FindControl way):

    Page.Master.FindControl("ContentPlaceHolder1").FindControl("tbFirst");
    

  2. Another, better approach would be to provide a public property in Page1 that returns tbFirst.Text. Then you could access it in the following way from Page2:

    if (PreviousPage != null && PreviousPage is Page1){
        lblResult.Text = ((Page1)PreviousPage).TbFirstText;
    }
    

  3. You could also add the Text as URL-Parameter, so that it's not required that Page2's PreviousPage is Page1.

  4. Last but not least. If you use Server.Transer with preserveForm set to tue, you would be able to retrieve the value of the original page TextBox control by referencing Request.Form("TbFirst").

Note: I don't recommend a recursive FindControl approach(starting from MasterPage), because it would also hardwire both pages and would be

  • a cause of nasty errors
  • slow
  • untransparent

这篇关于从访问内容页控制与嵌套母版页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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