在内容页面中查找控件

时间:2011-10-11 12:04:33

标签: c# asp.net master-pages

我在母版页中使用以下代码来查找内容页面中的控件。在第一页加载,它工作正常;但是当我从内容页面上的下拉列表中选择一个项目并且启用了autopostback时,我得到的错误是“对象引用未设置为对象的实例”。这意味着FindControl()函数无法在内容页面中找到控件。这个问题的原因是什么?

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>

1 个答案:

答案 0 :(得分: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;