views:

132

answers:

2

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();
A: 

use linq:

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

then bind list to bulletedlist as you have.

FiveTools
this did nothing to my li ids, it gave me the same result as my code, do i need to bind it in a special way after that?
Amr ElGarhy
Did you change the datasource, eg:bulletedList.DataSource = list;
FiveTools
Yes i did, and nothing new happened, i can't imagine how this code can edit the ID attribute of the ListItem
Amr ElGarhy
+1  A: 

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?

FiveTools
Yes very nice easy solution, thanks for your effort
Amr ElGarhy