0

I want to find the control ddlMaster.

Here is the excerpt aspx rah_sync_output.aspx in question:

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

Here is the masterfile /admin/Admin.master excerpt:

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

Here is the masterfile's masterfile fullPage.master excerpt:

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

My attempt in rah_sync_output.aspx.vb:

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

where columnName is "Master" at runtime.

Result is null. I either misunderstand that Master refers to the current master or the master's master. All I want to do is reference controls inside ContentPlaceHolderID="ContentPlaceHolder1"

H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
LearningJrDev
  • 911
  • 2
  • 8
  • 27

1 Answers1

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

That's one way to do it, but you're probably missing a naming container somewhere in the control hierarchy. It would be easier to 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;
}
Community
  • 1
  • 1
jrummell
  • 42,637
  • 17
  • 112
  • 171
  • This ended up working, thanks a lot. Still seems a bit overkill if I can just figure out the exact path, not sure how much overhead it is to recurse like this but this function helped me find it, then I just debugged it and looked at the path. – LearningJrDev Oct 05 '15 at 15:43