populate a html list control using .NET

前端 未结 4 701
暗喜
暗喜 2021-02-06 05:13

I have a list defined like so:

  • Item 1
4条回答
  •  灰色年华
    2021-02-06 06:05

    you can even use that HTML, adding runat="server" you will be able to treat it as a HTMLControl no mater what control it is, I do this often with div's

    • Item 1
    • Item 2

    then you get that HTMLControl and play with it

    HtmlGenericControl li;
    
    for (int x = 3; x <= 10; x++)
    {
        li = new HtmlGenericControl("li");
        li.Attributes.Add("class", "myItemClass");
        li.InnerText = "Item " + x;
    
        myList.Controls.Add(li);
    }
    

    you will end up with:

        
    • Item 1
    • Item 2
    • Item 3
    • Item 4
    • Item 5
    • Item 6
    • Item 7
    • Item 8
    • Item 9
    • Item 10

    of course that you can use an ordered or unorderer list, they also below to the ASP.NET Web Controls.

提交回复
热议问题