Pomster
Pomster

Reputation: 15197

How can i insert a C# list into a Div?

I have a C# list:

List<string> Listtags =  GetListTag.GetTagList().ToList();

And, I would like to put it into a Div:

<div id="tags">
    <ul>
        <li><This should be populated with my list></li>
         //This list can have any number of items depending on how big my list tags is
    </ul>
</div>

Could someone show me how to do this?

Upvotes: 0

Views: 4604

Answers (5)

Mironline
Mironline

Reputation: 2799

you can also use Repeater

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

and in runtime

List<string> ListTags =  GetListTag.GetTagList().ToList();
R.DataSource = ListTags;
R.DataBind();

Upvotes: 7

Tim Schmelter
Tim Schmelter

Reputation: 460228

If you're using ASP.NET, you could use a BulletedList webserver control:

<asp:BulletedList ID="BulletedList1" runat="server"
    BulletStyle="Circle" 
    DisplayMode="Text">
</asp:BulletedList>

and in codebehind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        List<string> Listtags = GetListTag.GetTagList().ToList();
        Listtags.ForEach(t => BulletedList1.Items.Add(t));
    }
}

Edit: "I want to add something like this::: Listtags.ForEach(t => BulletedList1.Items.Add(t),"$tag$ "); weight being a variable in my code"

So i assume that you want to add hyperlinks and apply a different css class on the items.

<asp:BulletedList ID="BulletedList1" runat="server"
    CssClass="TagList"
    DisplayMode="HyperLink">
</asp:BulletedList>

and for example the css:

<style>
.TagList a {text-decoration:none}
.TagList a:link {text-decoration:none}
.TagList a:visited {text-decoration: none; color: blue}
.TagList a:hover {text-decoration: underline; color: red}
</style>

and how you add the links dynamically(the value of the ListItem is the URL):

Listtags.ForEach(t => 
     BulletedList1.Items.Add(new ListItem(t, browseUrl + "?tag=$urlencodetag$"))
); 

Upvotes: 3

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48560

In Asp.Net

<ul id = "myul" runat = "server">
</ul>

In Code Behind (In Page Load I suppose or button click)

Listtags.ForEach(x => new ListItem(){Text = x });

Upvotes: 2

Rob Angelier
Rob Angelier

Reputation: 2333

If you are using MVC3 you can do something like the following:

<div id="tags">
  <ul>
  @foreach(var item in Model.Listtags)
  {
    <li>@item.YourPropertyName</li>
  }
  </ul>
</div>

Upvotes: 3

Pranay Rana
Pranay Rana

Reputation: 176934

Use asp:bulletedList and your list will be much easier.

<div id="tags">
    <asp:BulletedList id="blTabs" 
      BulletStyle="Disc"
      DisplayMode="LinkButton" 
      runat="server">
    </asp:BulletedList>
</div>

Code Behind:

    ListItem li = new ListItem();
    li.Value = "html text";  //html goes here i.e.  xtab1.html
    li.Text = "New Text";  //text name goes i.e. here tab1
    blTabs.Items.Add(li);

Upvotes: 4

Related Questions