C# 从代码隐藏在 aspx 中添加 <ul> <li> 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16873363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add <ul> <li> list in aspx from code-behind
提问by Shaggy
I am trying to make nested ul& litags in code behind.
For that i wrote priliminary code in my .aspxpage
我正在尝试在后面的代码中制作嵌套ul&li标签。为此,我在我的.aspx页面中编写了初步代码
<ul class="dropdown" runat="server" id="tabs"> </ul>
My C# Code
我的 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);
}
When i run my project and do inspect element on my menu i see something like
当我运行我的项目并检查菜单上的元素时,我看到类似
<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>
No Nested ultags are created inside li?? Why ??
ul内部没有创建嵌套标签li??为什么 ??
For example :-
例如 :-
<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>
采纳答案by Rawling
You see where you're calling li.Controls.Add(anchor)? You're not calling li.Controls.Add(ul)anywhere so your created uls aren't actually being added anywhere on the page.
你知道你在打电话li.Controls.Add(anchor)吗?你没有li.Controls.Add(ul)在任何地方打电话,所以你的 created uls 实际上没有被添加到页面上的任何地方。
回答by Eric Falsken
The problem here is that you are adding multiple Href attributes to your anchor. Possibly overriding the href each time. Change your code to this:
这里的问题是您要向锚点添加多个 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);
}
}
回答by sudheer kumar
You can add LI items to UL item using the c# code by the following
您可以使用 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";
For More info you can visit this link: http://www.sharepointsol.com/2014/09/dynamically-adding-li-to-ul.html
有关更多信息,您可以访问此链接:http: //www.sharepointsol.com/2014/09/dynamically-adding-li-to-ul.html
回答by Anh Nguyen
You can try this code:
你可以试试这个代码:
aspx:
ASP:
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="ControlContainer"
runat="server" />
</div>
</form>
cs:
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);

