【问题标题】:How to access content page controls from master page in asp.net如何从 asp.net 中的母版页访问内容页面控件
【发布时间】:2011-02-12 18:03:56
【问题描述】:

从内容页面访问母版页控件非常容易,例如

protected void Page_Load(object sender, EventArgs e)
{
    // content page load event
    DropDownList thisDropDown = this.Master.FindControl("someDropDown") as DropDownList;
    userLabel.Text = thisDropDown.SelectedValue;
}

但是我如何从母版页访问内容页的控件。假设内容页面中有一个文本框,母版页面中有一个按钮。我希望当我单击母版页按钮时,我想在母版页标签的内容页中显示文本框的文本。如何实现它。请帮我提供代码示例。谢谢。

【问题讨论】:

    标签: asp.net


    【解决方案1】:

    在母版页按钮单击事件应通过以下方式访问页面内容:-

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox TextBox1 = (TextBox)ContentPlaceHolder1.FindControl("TextBox1");
        if (TextBox1 != null)
        {
            Label1.Text = TextBox1.Text;
        }
    }
    

    【讨论】:

    • 愚蠢到需要这么多代码...我不是将其归因于您的答案,而是归因于 ASP.net 模型
    • 破坏派对;不确定“彻底” cmets 是否用于“错误处理”或什么,但在这种情况下,我建议您实际上不要检查 FindControl 的结果。如果您没有做任何花哨的事情,那么您很可能更希望获得一个异常来发出错误信号,而不是默默地什么都不做。更好的是:使用断言,并且不要在生产代码中进行检查。如果您仍然设法以某种方式交付一个缺少预期控件的项目,则更愿意将用户重定向到友好的错误页面,并修复您的发布过程。
    【解决方案2】:

    已经有一段时间了,但我相信您可以通过使用 ContentPlaceHolder 作为参考来做到这一点:

    Control control = this.myContentPlaceHolder.FindControl("ContentPageControlID");
    

    【讨论】:

      【解决方案3】:

      在我看来,例如,最好使用母版页面中的事件引发并在内容页面中捕获此事件以更改此页面上的某些内容。主要优点是可重用性。将来您可能希望从母版页更改其他内容页上的内容,在这种情况下,您应该只将事件处理程序添加到此内容页,而不更改母版页上的代码。在这种方法中,您不需要从某些内容页面硬编码控件名称。此外,您根本不应该为某些内容的控件添加依赖项。

      例如,您可以找到here 的实现示例。

      【讨论】:

        【解决方案4】:

        您应该从母版页中查找 contentplaceholder,然后在母版页的子页中查找 contentplaceholder

        this.Page.Master.FindControl("ContentPlaceHolder1").FindControl("controlAFromPage");
        

        【讨论】:

        • 我松了一口气……母版页,哇!然后是这条漫长的寻找之路!!
        【解决方案5】:

        您可以通过以下方式找到控制:

        ContentPlaceHolder contentPage = Page.MasterPage.FindControl("ContentPlaceHolder1") as ContentPlaceHolder;
        Label lblHead =(Label)contentPage.FindControl("lblHeading");
        Response.Write(lblHead.Text);
        

        来源: http://xpode.com/ShowArticle.aspx?ArticleId=629

        【讨论】:

          【解决方案6】:

          Site.Master 中的 C# 代码:

          <div>
            <asp:ContentPlaceHolder ID="MainContent" runat="server">
            </asp:ContentPlaceHolder>
          </div>
          

          Site.Master.cs 中的代码:

          public partial class SiteMaster : MasterPage
          {
              protected void Page_Load(object sender, EventArgs e)
              {          
                  ToolkitScriptManager1.RegisterPostBackControl(this.MainContent.FindControl("btnOnDefaultPage"));            
              }
          }
          

          这是一个如何从 Site.Master.cs 引用 Default.aspx 上的客户端控件的示例

          【讨论】:

          • 欢迎来到 Stack Overflow!请注意,您正在回答一个非常古老且已经回答的问题。这是How to Answer 的指南。
          【解决方案7】:

          试试这个代码

          Page.Master.FindControl("MainContent").FindControl("DivContainer_MyProfile").Visible = True
          

          【讨论】:

          • 欢迎来到 Stack Overflow!我推荐你take the tour。在给出答案时,最好解释一下为什么你的答案是这个答案。
          猜你喜欢
          • 2020-01-14
          • 1970-01-01
          • 2014-09-20
          • 2010-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多