2009-06-29 72 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; 
    } 
    } 
    
相關問題