ListItem.Attributes.Add not working

2019-07-19 07:09发布

问题:

I am trying to add a custom attribute to list item but it is not adding it

My code is this

ListItem item = new ListItem(teamMemberGroup.Name, teamMemberGroup.ID);
item.Attributes.Add("teammembergroup", "true");

ddlTeamGroups.Items.Add(item);
ddlTeamGroups1.Items.Add(item);

It is only adding option text and value in the Html but no attributes! My dropdown is inside an update panel but I don't think update panel is making something wrong as I am not looking for postback but it is only about the very first page load loading

Any idea?

回答1:

The attributes you add using

item.Attributes.Add("teammembergroup", "true");

will be added to the listitem only for client side it doesn't maintain its Attribute collection for the server side. If you want to have a look at the added attribute you can go to "view source" of your page & can see the added attribute there in to your rendered drop down list control.