6

今天是个好日子,

我正在 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");

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

谢谢!

纪尧姆·热维斯。

4

5 回答 5

7

除非别无选择,否则我会尽量避免使用 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";
于 2009-06-30T02:44:01.240 回答
4

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

于 2009-06-29T21:02:00.697 回答
3

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

Label lblMessage = new Label();
lblMessage = (Label)Master.FindControl("lblTest");
lblMessage.Text = DropDownList1.SelectedItem.Text;
于 2012-07-06T09:32:03.913 回答
1

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

    /// <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);
            }
        }
于 2009-06-29T20:59:09.377 回答
1

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

  1. 将具有 ID="PanelWithLabel" 和 runat="server" 的 <asp:Panel> 添加到您的 ContentPage。

  2. 在面板内,添加一个 ID="MyLabel" 的 <asp:Label> 控件。

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

    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;
     }
    }
    
于 2013-11-07T21:51:53.450 回答