Amr Elgarhy
Amr Elgarhy

Reputation: 68952

How to set a custom id for listitems in BulletedList control while binding?

I am binding a BulletedList control in page load to a datasource, what i want to do is to set custom IDs for this BulletedList's ListItems while binding
This custom ID will be in my current case "li_"+ItemId.ToString()

Sample Code i used to fill the bulletedList:

bulletedList.DataSource = MyDataCollection;
bulletedList.DataTextField = "Name";
bulletedList.DataValueField = "Id";
bulletedList.DataBind();

Upvotes: 0

Views: 3386

Answers (2)

FiveTools
FiveTools

Reputation: 6030

I understand now...

you mean like this?:

 private void BindBulletList()
     {
         List<string> list = new List<string>();
         list.Add("item1");
         list.Add("item2");
         list.Add("item3");
         list.Add("item4");
         list.Add("item5");

         bullets.DataSource = list;
         bullets.DataBind();

         foreach (ListItem item in bullets.Items)
         {
             item.Attributes.Add("Id", "li_" + item.Text);
         }


     }

Does this help?

Upvotes: 3

FiveTools
FiveTools

Reputation: 6030

use linq:

var list = MyDataCollection.Select(x => new {Name = x.Name, Id = "li_"+ x.Id});

then bind list to bulletedlist as you have.

Upvotes: 0

Related Questions