Search code examples
c#asp.netservercontrols

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

Solution

  • 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?