如何在asp.net中创建html标签,如ul,li,a等

Neo*_*Neo 7 asp.net

如何在asp.net中创建HTML标签,如ul,li,a等

详细而且以最简单的方式使用所有HTML标签和动态方式.

小智 9

如果我理解正确,您可以使用Repeater控件:

<ul>
<asp:Repeater runat="server" ID="repeater1">
    <ItemTemplate>
        <li> <%# Container.DataItem %> </li>
    </ItemTemplate>
</asp:Repeater>
</ul>
Run Code Online (Sandbox Code Playgroud)

然后将此控件与您的数据集合绑定:

repeater1.DataSource = YourCollectionOfStringHere;
repeater1.DataBind();
Run Code Online (Sandbox Code Playgroud)

希望它会有所帮助.如果没有,抱歉这个)


Sko*_*ioh 7

嗯,最简单但不是面向对象的方法:


添加到.aspx文件:

<asp:Literal ID="myLiteral" runat="server"></asp:Literal>
Run Code Online (Sandbox Code Playgroud)


添加到.aspx.cs文件:

myLiteral.Text = "<ul><li>apple</li><li>orange</li><li>kiwi</li></ul>";
Run Code Online (Sandbox Code Playgroud)


不好,但工作:)




最简单的面向对象的方法是使用HtmlGenericControl类.您可以使用它创建任何标签,至少可以确保正确关闭这些标签.创建<div>的示例:

string s="This is <strong>a test</strong> of the html HtmlGenericControl class";
HtmlGenericControl ge = new HtmlGenericControl("div");
ge.InnerHtml=s;
this.PlaceHolder1.Controls.Add(ge);
Run Code Online (Sandbox Code Playgroud)



请记住,这两种解决方案都有效,但两者都不是完成任务的好方法.要生成<a>标签,你应该使用HtmlAnchor控件,对于<div>,你应该使用<asp:Panel>作为<span>你应该使用<asp:Label>,对于你的列表,你应该使用<asp:BulletedList>或者中继器控制等.

希望这可以帮助.


Mad*_*uja 7

使用HtmlGenericControl.这正是你要找的东西:

http://msdn.microsoft.com/en-us/library/7512d0d0(v=VS.90).aspx