在内容页面内查找控件

发布于 2024-12-09 08:56:33 字数 2903 浏览 0 评论 0原文

我在母版页中使用以下代码来查找内容页面内的控件。在第一页加载时,它工作正常;但是,当我从内容页面上的下拉列表中选择一个项目并且启用了自动回发时,我收到错误“对象引用未设置到对象的实例”。这意味着 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>

I'm using the following code in my master page to find controls inside content pages. On the first page load, it works fine; but when i select an item from a dropdown which is on content page and it autopostback is enabled, then i'm getting the error "object reference not set to an instance of a object". This means the FindControl() function was unable to find the controls inside content pages. What is the reason for this issue?

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

edit...

Html code for the dropdown:

 <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千仐 2024-12-16 08:56:33

您可以查看这篇文章: ASP .NET 2.0 MasterPages 和 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; 

You can check this post : 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;
}

use it

this.dgItemList = FindControlRecursive(this.Master, "dgItemList") as DataGrid; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文