我仍然是aspx dev的新手,我在访问导航栏上的下拉列表时遇到了一些麻烦。 标记看起来像这样:
<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>
我可以使用下面的代码很好地访问ContentPlaceholder,但之后出现问题,在创建DropDownList对象时获取空引用...
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();
有什么建议吗?
答案 0 :(得分:0)
<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);
}