6

我正在尝试在后面的代码中制作嵌套ul&li标签。为此,我在我的.aspx页面中编写了初步代码

<ul class="dropdown" runat="server" id="tabs"> </ul>

我的 C# 代码

DatTable dtOutput = Generix.getData("Get Some Data");

foreach (DataRow drOutput in dtOutput.Rows)
{
    HtmlGenericControl li = new HtmlGenericControl("li");                    
    tabs.Controls.Add(li);
    HtmlGenericControl anchor = new HtmlGenericControl("a");
    anchor.Attributes.Add("href", "#");
    anchor.InnerText = Convert.ToString(drOutput["ModuleGroup"]);
    li.Controls.Add(anchor);
    HtmlGenericControl ul = new HtmlGenericControl("ul");

    DatTable dtOutputList = Generix.getData("Get another set of Data");

    foreach (DataRow drOutputList in dtOutputList.Rows)
    {                        
        HtmlGenericControl ili = new HtmlGenericControl("li");
        ul.Controls.Add(ili);
        HtmlGenericControl ianchor = new HtmlGenericControl("a");
        foreach (DataColumn dcOutputList in dtOutputList.Columns)
        {
            ianchor.Attributes.Add("href", Convert.ToString(drOutputList["ModuleFileName"]));
        }
        ianchor.InnerText = Convert.ToString(drOutputList["ModuleName"]);
        ili.Controls.Add(ianchor);                        
    }
    //tabs.Controls.Add(li);
}

当我运行我的项目并检查菜单上的元素时,我看到类似

<ul id="ctl00_tabs" class="dropdown">
    <li class="">
        <a href="#">Master</a>
    </li>
    <li class="">
        <a href="#">Cards Management</a>
    </li>
    <li class="">
        <a href="#">Authorization</a>
    </li>
    <li class="">
        <a href="#">Loyalty</a>
    </li>
    <li class="">
        <a href="#">Reports</a>
    </li>
</ul>

内部没有创建嵌套ul标签li??为什么 ??

例如 :-

<ul id="ctl00_tabs" class="dropdown">
    <li class="">
        <a href="#">Master</a>
        <ul>
            <li><a href="Some.aspx"><span>Some Name</span></a></li>
            <li><a href="Some1.aspx"><span>Some Name 1</span></a></li>
        </ul>
    </li>
</ul>
4

4 回答 4

5

你看你在哪里打电话li.Controls.Add(anchor)?您没有li.Controls.Add(ul)在任何地方调用,因此您创建ul的 s 实际上并没有被添加到页面上的任何位置。

于 2013-06-03T10:13:43.950 回答
3

您可以通过以下方式使用 c# 代码将 LI 项添加到 UL 项

<ul class="respond" id="feedbackTab" runat="server"></ul> 

HtmlGenericControl li = new HtmlGenericControl("li");
feedbackTab.Controls.Add(li);

HtmlGenericControl anchor = new HtmlGenericControl("a");
anchor.Attributes.Add("href", "aboutme.aspx");
anchor.InnerText = "Tab Text";

有关更多信息,您可以访问此链接:http ://www.sharepointsol.com/2014/09/dynamically-adding-li-to-ul.html

于 2014-09-16T07:12:44.170 回答
0

这里的问题是您正在向锚添加多个 Href 属性。可能每次都覆盖href。将您的代码更改为:

foreach (DataRow drOutput in dtOutput.Rows)
{
    HtmlGenericControl li = new HtmlGenericControl("li");
    tabs.Controls.Add(li); // tabs is id of ul tag which is runat=server
    foreach (DataColumn dcOutput in dtOutput.Columns)
    {
        HtmlGenericControl anchor = new HtmlGenericControl("a");
        anchor.Attributes.Add("href", Convert.ToString(drOutput["ModuleFileName"]));
        anchor.InnerText = Convert.ToString(drOutput["ModuleName"]);
        li.Controls.Add(anchor);
    }
}
于 2013-06-01T16:52:01.270 回答
0

你可以试试这段代码:

aspx:

<form id="form1" runat="server">
        <div>


            <asp:PlaceHolder ID="ControlContainer"
                runat="server" />
        </div>
</form>

CS:

HtmlGenericControl tabs = new HtmlGenericControl("ul");
            tabs.ID = "myTopnav";
            tabs.Attributes.Add("class", "topnav");

            HtmlGenericControl li = new HtmlGenericControl("li");
            HtmlGenericControl ianchor = new HtmlGenericControl("a");

            li = new HtmlGenericControl("li");
            ianchor = new HtmlGenericControl("a");
            ianchor.ID = "Home";
            ianchor.Attributes.Add("href", "#home");
            ianchor.Attributes.Add("class", "active");
            ianchor.InnerText = "Home";
            li.Controls.Add(ianchor);
            tabs.Controls.Add(li);


            li = new HtmlGenericControl("li");
            ianchor = new HtmlGenericControl("a");
            ianchor.ID = "News";
            ianchor.Attributes.Add("href", "#");
            ianchor.InnerText = "News";
            li.Controls.Add(ianchor);
            tabs.Controls.Add(li);

            ControlContainer.Controls.Add(tabs);

于 2016-09-22T07:57:00.533 回答