How to add element inside ListItem in code behind?

落爺英雄遲暮 提交于 2019-12-11 07:44:57

问题


I want to add something like this in the code behind:

<ul>
   <li><a>A</a></li>
</ul>

However, the ListItem of ASP.NET seems to allow text only:

BulletedList UserSubMenuList = new BulletedList();
ListItem EditUserItem = new ListItem("Edit Profile"); 

Is there other way to add content between <li></li> tag with code behind rather then using HtmlGenericControl?


回答1:


Front :

<ul id="test" runat="server">

</ul>

Back:

Label label1=new Label();

label1="test";

test.Controls.Add(new LiteralControl("<li>")); 

test.Controls.Add(label1); 

test.Controls.Add(new LiteralControl("</li>"))



回答2:


Try this:

BulletedList UserSubMenuList = new BulletedList();
UserSubMenuList.Items.Add(new ListItem(HttpUtility.HtmlEncode(@"<a href=\"link\">Edit Profile</a>"));

Note that you'll be clearing whatever was initially displayed if you do the first line (the constructor).

The Text property is simply the string of text to put in the <li> tags - if it's text (like all HTML markup), it should be able to go there.



来源:https://stackoverflow.com/questions/7226546/how-to-add-element-inside-listitem-in-code-behind

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!