【问题标题】:Access a content control in C# when using Master Pages使用母版页时在 C# 中访问内容控件
【发布时间】:2010-11-06 19:44:27
【问题描述】:

大家好,

我正在 ASP.NET 中构建一个页面,并在此过程中使用母版页。

我的母版页中有一个内容占位符名称“cphBody”,其中包含该母版页作为母版页的每个页面的正文。

在 ASP.NET 网页中,我有一个 Content 标记(引用“cphBody”),其中还包含一些控件(按钮、Infragistics 控件等),我想在 CodeBehind 文件中访问这些控件。但是,我不能直接这样做(this.myControl ...),因为它们嵌套在 Content 标记中。

我找到了 FindControl 方法的解决方法。

ContentPlaceHolder contentPlaceHolder = (ContentPlaceHolder) Master.FindControl("cphBody");
ControlType myControl = (ControlType) contentPlaceHolder.FindControl("ControlName");

效果很好。但是,我怀疑这不是一个很好的设计。你们知道更优雅的方法吗?

谢谢!

纪尧姆·热维斯。

【问题讨论】:

  • 您是尝试从内容页的代码隐藏还是母版页的代码访问控件?
  • 内容页面的代码隐藏。
  • 这很奇怪。您应该能够从内容页面的代码隐藏中直接访问您的控件,除非它们是动态创建和添加的。

标签: asp.net master-pages findcontrol


【解决方案1】:

除非别无选择,否则我会尽量避免使用 FindControl,而且通常有更简洁的方法。

如何在子页面顶部包含母版页的路径

<%@ MasterType VirtualPath="~/MasterPages/PublicUI.Master" %>

这将允许您直接从您的母版页代码中调用代码。

然后从您背后的母版页代码中,您可以使属性返回您的控件,或者使母版页上的方法获取您的控件等。

public Label SomethingLabel
{
    get { return lblSomething; }
}
//or
public string SomethingText
{
    get { return lblSomething.Text; }
    set { lblSomething.Text = value; }
}

指母版页上的标签

<asp:Label ID="lblSomething" runat="server" />

用法:

Master.SomethingLabel.Text = "some text";
//or
Master.SomethingText = "some text";

【讨论】:

  • 反过来呢?我的内容页面中有一个GridView,我希望能够从我的 MasterPage 更新它。
  • 我会从母版页使用您需要的事件数据引发一个事件,并使用您的页面订阅它。 Master.SomeEvent += SomeHandler;
【解决方案2】:

Rick Strahl 在这里有一个很好的解释(和示例代码) - http://www.west-wind.com/Weblog/posts/5127.aspx

【讨论】:

  • 谢谢!这基本上就是我所做的,所以它为我验证了我的方法!
  • 我真的不喜欢 FindControl 它通常只在您无法公开强类型属性并且需要挖掘预渲染输出时才需要。
【解决方案3】:

没有什么不同的。只需在子页面上编写此代码即可访问母版页标签控件。

Label lblMessage = new Label();
lblMessage = (Label)Master.FindControl("lblTest");
lblMessage.Text = DropDownList1.SelectedItem.Text;

【讨论】:

    【解决方案4】:

    我使用此代码递归地访问文件:

        /// <summary>
        /// Recursively iterate through the controls collection to find the child controls of the given control
        /// including controls inside child controls. Return all the IDs of controls of the given type 
        /// </summary>
        /// <param name="control"></param>
        /// <param name="controlType"></param>
        /// <returns></returns>
        public static List<string> GetChildControlsId(Control control, Type controlType)
        {
            List<string> FoundControlsIds = new List<string>();
            GetChildControlsIdRecursive(FoundControlsIds, control, controlType);
    
            // return the result as a generic list of Controls
            return FoundControlsIds;
        }
    
        public static List<string> GetChildControlsIdRecursive(List<string> foundControlsIds, Control control, Type controlType)
        {
            foreach (Control c in control.Controls)
            {
                if (controlType == null || controlType.IsAssignableFrom(c.GetType()))
                {
                    // check if the control is already in the collection
                    String FoundControl = foundControlsIds.Find(delegate(string ctrlId) { return ctrlId == c.ID; });
    
                    if (String.IsNullOrEmpty(FoundControl))
                    {
                        // add this control and all its nested controls
                        foundControlsIds.Add(c.ID);
                    }
                }
    
                if (c.HasControls())
                {
                    GetChildControlsIdRecursive(foundControlsIds, c, controlType);
                }
            }
    

    【讨论】:

      【解决方案5】:

      嗨,我只是想分享我的解决方案,发现这适用于访问位于“ContentPage”上的 内的“控件”,但来自“MasterPage”的 C# 代码隐藏.希望对大家有所帮助。

      1. 将 ID="PanelWithLabel" 和 runat="server" 的 添加到您的 ContentPage。

      2. 在面板中,添加一个 ID="MyLabel" 的

      3. 在您的 MasterPage 代码隐藏中编写(或复制/粘贴以下)函数,如下所示:(这将访问面板内的标签控件,它们都在 ContentPage 上,从母版页代码隐藏和更改它的文本是母版页上的 TextBox 的文本:)

        protected void onButton1_click(object sender, EventArgs e)
        {
        // find a Panel on Content Page and access its controls (Labels, TextBoxes, etc.) from my master page code behind //
        System.Web.UI.WebControls.Panel pnl1;
        pnl1 = (System.Web.UI.WebControls.Panel)MainContent.FindControl("PanelWithLabel");
        if (pnl1 != null)
         {
            System.Web.UI.WebControls.Label lbl = (System.Web.UI.WebControls.Label)pnl1.FindControl("MyLabel");
            lbl.Text = MyMasterPageTextBox.Text;
         }
        }
        

      【讨论】:

        猜你喜欢
        • 2014-09-20
        • 2014-03-27
        • 1970-01-01
        • 1970-01-01
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多