2011-10-11 77 views
0

我在我的母版頁中使用以下代碼來查找內容頁面內的控件。在首頁加載時,它工作正常;但是當我從內容頁面上的下拉菜單中選擇一個項目,並且啓用了autopostback時,那麼我得到錯誤「對象引用未設置爲對象的實例」。這意味着FindControl()函數無法在內容頁面中找到控件。這個問題的原因是什麼?內容頁面中的Findcontrols

PlaceHolder pHolder = (PlaceHolder)ContentPlaceHolder2.FindControl("PlaceHolder1"); 
Label lblPage = (Label)pHolder.FindControl("lblPageName"); 
if (lblPage.Text == "DesignSerachSQGrid") 
{ 
} 

編輯...

的下拉HTML代碼:

<telerik:RadComboBox ID="ddlSearchL1" runat="server" Width="400px" Skin="Office2007" 
        onselectedindexchanged="ddlSearchL1_SelectedIndexChanged" 
        AutoPostBack="True" Font-Names="Eras Medium ITC" Font-Size="X-Small" 
        onprerender="ddlSearchL1_PreRender"> 
        <Items> 
         <telerik:RadComboBoxItem runat="server" Text="Please Select ................" 
          Value="Please Select ................" Font-Name="Eras Medium ITC" /> 
         <telerik:RadComboBoxItem runat="server" Text="Design Documents" 
          Value="Design Documents" Font-Name="Eras Medium ITC" /> 
         <telerik:RadComboBoxItem runat="server" Text="Vendor Documents" 
          Value="Vendor Documents" Font-Name="Eras Medium ITC" /> 
         <telerik:RadComboBoxItem runat="server" Text="Business Documents" 
          Value="Business Documents" Font-Name="Eras Medium ITC" /> 
         <telerik:RadComboBoxItem runat="server" Text="Management Documents" 
          Value="Management Documents" Font-Name="Eras Medium ITC" /> 
         <telerik:RadComboBoxItem runat="server" Text="Squad Check - Management" 
          Value="Squad Check - Management" Font-Name="Eras Medium ITC" /> 
         <telerik:RadComboBoxItem runat="server" Text="Transmittal - Management" 
          Value="Transmittal - Management" Font-Name="Eras Medium ITC" /> 
          <telerik:RadComboBoxItem runat="server" Text="Transmittal - Design" 
          Value="Transmittal - Design" Font-Name="Eras Medium ITC" /> 
          <telerik:RadComboBoxItem runat="server" Text="Squad Check - Design" 
          Value="Squad Check - Design" Font-Name="Eras Medium ITC" /> 
      <telerik:RadComboBoxItem runat="server" Text="Transmittal - Vendor" 
          Value="Transmittal - Vendor" Font-Name="Eras Medium ITC" /> 
          <telerik:RadComboBoxItem runat="server" Text="Squad Check - Vendor" 
          Value="Squad Check - Vendor" Font-Name="Eras Medium ITC" /> 
        </Items> 
      </telerik:RadComboBox> 
+0

你可以爲MasterPage添加一些HTML嗎? – 2GDev

+0

這是一個很長的主頁,不知道html部分可能有幫助 – chamara

回答

1

您可以檢查這個帖子:ASP.NET 2.0 MasterPages and FindControl()

/// <summary> 
/// Finds a Control recursively. Note finds the first match and exists 
/// </summary> 
/// <param name="ContainerCtl"></param> 
/// <param name="IdToFind"></param> 
/// <returns></returns> 
public static Control FindControlRecursive(Control Root, string Id) 
{ 
    if (Root.ID == Id) 
     return Root; 

    foreach (Control Ctl in Root.Controls) 
    { 
     Control FoundCtl = FindControlRecursive(Ctl, Id); 
     if (FoundCtl != null) 
      return FoundCtl; 
    } 

    return null; 
} 

使用它

this.dgItemList = FindControlRecursive(this.Master, "dgItemList") as DataGrid;