开发者

how can i create html tags in asp.net like ul, li, a etc

How can I create HTML tags in asp.net like ul, li, a, etc

In detail and in the simplest way possi开发者_JS百科ble with all HTML tags and dynamic manner.


If I understand you correctly you can use Repeater control:

<ul>
<asp:Repeater runat="server" ID="repeater1">
    <ItemTemplate>
        <li> <%# Container.DataItem %> </li>
    </ItemTemplate>
</asp:Repeater>
</ul>

Then bind this control with your collection of your data:

repeater1.DataSource = YourCollectionOfStringHere;
repeater1.DataBind();

Hope it will be helpful. If not, sorry for this )


Use HtmlGenericControl. That's exactly what you are looking for:

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


Well, the simplest but not object oriented way to do it:


Add to your .aspx file:

<asp:Literal ID="myLiteral" runat="server"></asp:Literal>


Add to your .aspx.cs file:

myLiteral.Text = "<ul><li>apple</li><li>orange</li><li>kiwi</li></ul>";


Not nice, but works :)




The easiest object oriented way to do it is with the HtmlGenericControl class. You can create any tag you want with it and at least it assures that these tags are closed properly. Example to create a <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);



Just keep in mind, that both solutions are working, but neither one is the nice way to get things done. For generating <a> tags you should use HtmlAnchor control, for <div> you should use <asp:Panel> for <span> you should use <asp:Label>, for your list you should use <asp:BulletedList> or the repeater control and so on.

Hope this helps.


You can create a ul / li set using the BulletedList control:

  • http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.bulletedlist.aspx

It also has a DisplayMode which you can use to make it display hyperlinks:

  • http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.bulletedlist.displaymode.aspx


You can create them just like normal html:

example:

<ul type="disc">
        <li>A</li>
        <li>B</li>
        <li>C</li>
        <li>D</li>
        <li>E</li> </ul>

if you want to tranform them to server side controls you can just add runat="server"

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜