简体   繁体   中英

Access Dropdown on Masterpage via ContentPlaceHolder

Im still new to aspx dev and I'm having some trouble to access a dropdownlist I placed on to navigation bar. Markup looks like this:

        <div class="navbar-collapse collapse">                    
            <ul class="nav navbar-nav">
                <asp:ContentPlaceHolder ID="cphTournamentDropdown" runat="server">
                    <li class="dropdown" style="background-color: orangered" id="ZZZ" runat="server">
                        <a class="dropdown-toggle" id="WWW" style="margin-right: 5px; color: navy" href="#" data-toggle="dropdown">Tournaments<b class="caret"></b></a>
                    </li>
                </asp:ContentPlaceHolder>
                <li><a runat="server" href="~/" style="color: orangered">Logs</a></li>
                <li><a runat="server" href="~/About" style="color: orangered">Fixtures</a></li>
                <li><a runat="server" href="~/Contact" style="color: orangered">Results</a></li>
            </ul>
        </div>

I can get to the ContentPlaceholder just fine using code below, but after that things go wrong, getting null reference when creating the DropDownList object...

    ContentPlaceHolder cph = (ContentPlaceHolder)form.FindControl("cphTournamentDropdown");
    HtmlGenericControl genericControl = (HtmlGenericControl)cph.FindControl("ZZZ");
    DropDownList cbo = genericControl.Controls[0] as DropDownList;

    cbo.DataSource = tournaments;  //arrayList of objects
    cbo.DataTextField = "Title";
    cbo.DataBind();

Any suggestions?

<a class="dropdown-toggle" runat="server" id="WWW" ...


ContentPlaceHolder cph = (ContentPlaceHolder)form.FindControl("cphTournamentDropdown");
HtmlGenericControl genericControl = (HtmlGenericControl)cph.FindControl("ZZZ");
HtmlAnchor cbo = (HtmlAnchor)genericControl.FindControl("WWW");

for (int i = 0; i < 10; i++)
{
    var item = new HtmlGenericControl("li");
    item.InnerText = i.ToString();
    cbo.Controls.Add(item);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM