开发者

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 ListIte开发者_如何学运维ms 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();


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?


use linq:

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

then bind list to bulletedlist as you have.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜