用FindControl找不到控件

时间:2015-10-05 15:29:17

标签: asp.net vb.net

我想找到控件ddlMaster

以下是有问题的摘录aspx rah_sync_output.aspx

<%@ Page Language="vb" MasterPageFile="~/admin/Admin.master" AutoEventWireup="false" CodeBehind="rah_sync_output.aspx.vb" Inherits="TCDS.Web.admin.RahSyncOutput" EnableEventValidation="false" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick = "ExportToExcel" /><br />

    Locations
    <asp:DropDownList ID="ddlMaster" runat="server" OnSelectedIndexChanged = "FilterChanged" AutoPostBack = "true" AppendDataBoundItems = "true">
    </asp:DropDownList>

</asp:Content>

这是主文件/admin/Admin.master摘录:

<%@ Master Language="VB" Inherits="XXX.Web.admin.AdminAdmin" Codebehind="Admin.master.vb" MasterPageFile="~/fullPage.master" %>

<asp:Content ID="headerContent" ContentPlaceHolderID="headContent" runat="server">
    <asp:contentplaceholder id="head" runat="server">
    </asp:contentplaceholder>
</asp:Content>
<asp:Content  ID="menuContent" ContentPlaceHolderID="tcdsMenu" runat="server">
    <ul class="ulMenu" style="">
        <asp:Literal ID="ltrMenu" Text='' runat="server" />            
    </ul>
</asp:Content>
<asp:Content ID="tcdsContent" ContentPlaceHolderID="tcdsContent" runat="server">
    <div id="content">
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
</asp:Content>

这是masterfile的主文件fullPage.master摘录:

<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div class="box">
    <div id="divHeader" runat="server">
        <asp:Literal ID="ltrAgencyLogo" runat="server" />
        <asp:Literal ID="ltrAgencyName" runat="server" />
        <a id="xxxicon" class="icnMCLLC" href="http://www.hidden.com" target="mcllc" runat="server">
            <img src="/tcds/images/xxx.gif" border="0" alt="Click for technical assistance" width="110" height="36" />
        </a>
        <div class="bannerIcon"><a href="javascript:jsHelp()"><img src="/tcds/images/icnHelp.gif" border="0" alt="TCDS Help" style='width: 24px;height: 24px;' /></a></div>
        <span class="header"><asp:Literal ID="ltrHeader" runat="server" /></span>
        <asp:contentplaceholder id="tcdsMenu" runat="server">
        </asp:contentplaceholder>
    </div>
        <div id="content">
            <asp:contentplaceholder id="tcdsContent" runat="server">
            </asp:contentplaceholder>
        </div>
    </div>
</form>

我在rah_sync_output.aspx.vb的尝试:

    Dim ddl = DirectCast(Master.FindControl("tcdsContent").FindControl("ContentPlaceHolder1").FindControl("ddl" + columnName), DropDownList)

其中columnName在运行时是“主”。

结果是null。我要么误解Master是指当前的主人还是主人的主人。我想要做的就是ContentPlaceHolderID="ContentPlaceHolder1"

中的引用控件

1 个答案:

答案 0 :(得分:0)

Master.FindControl("tcdsContent").FindControl("ContentPlaceHolder1").FindControl("ddl" + columnName)

这是一种方法,但您可能在控件层次结构中的某个位置缺少命名容器。 find the control recursively会更容易。

/// <summary>
/// recursively finds a child control of the specified parent.
/// </summary>
/// <param name="control"></param>
/// <param name="id"></param>
/// <returns></returns>
public static Control FindControlRecursive(this Control control, string id)
{
    if (control == null) return null;
     //try to find the control at the current level
    Control ctrl = control.FindControl(id);

    if (ctrl == null)
    {
        //search the children
        foreach (Control child in control.Controls)
        {
            ctrl = FindControlRecursive(child, id);

            if (ctrl != null) break;
        }
    }
    return ctrl;
}