2009-06-29 73 views
6

好日子大家时访问内容控制在C#中使用母版页

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

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

谢谢!

Guillaume Gervais。

+1

你是不是想从内容页面的代码隐藏访问控制,或母版页的代码隐藏在一个TextBox的? – wulimaster 2009-06-29 20:53:42

+0

内容页面的CodeBehind。 – 2009-06-29 22:50:23

+0

这很奇怪。您应该能够直接从您的内容页面的代码隐藏中访问您的控件,除非它们是动态创建和添加的。 – 2009-06-30 00:00:48

回答

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); 
      } 
     } 
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"; 
3

无事可做不同。只需将此代码写入子页面即可访问母版页标签控件。

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

嗨只是想我会分享我的解决办法,发现这个工程的访问“控制”是一个< ASP内部:面板>这是一个“ContentPage”,但是从C#代码隐藏'MasterPage'。希望它有助于一些。与ID = “PanelWithLabel” 和RUNAT = “服务器” 您ContentPage面板>:

  1. 添加< ASP。

  2. 在Panel内部,添加一个< asp:Label>控件,ID =“MyLabel”。

  3. 在您的MasterPage Code-behind中写入(或复制/粘贴以下)一个函数,如下所示:(这会访问面板内的标签控件,它们都位于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; 
    } 
    } 
    
相关问题