Reputation: 148514
I have a dropdownlist which i'm binding to DataTable.
ddlItems.DataSource = dt;
ddlItems.DataBind();
in the final html I have :
...
<option value="-1">aaa</option>
<option value="-2">bbb</option>
...
But I want to catch the bind event in the DataBound event and to add an attribute to each listItem , so that the Final Html will be :
...
<option value="-1" MyAttr="lalala1" >aaa</option>
<option value="-2" MyAttr="lalala2" >bbb</option>
...
But the signiture of the databound event is :
protected void ddlItemsDataBound(object sender, EventArgs e)
and e has only :
How can i catch the specific bounded listItem ?
p.s.
I do NOT want to cancel the databound event , and to use regular loop (adding lisItems in a loop)
Upvotes: 1
Views: 7467
Reputation: 50728
What you want is something similar to GridView.RowDataBound
, which doesn't exist for the DropDownList. What you need to do is a foreach loop in the DataBound event, or you can add this feature by building your own custom DropDownList. The only problem with the latter option is Microsoft doesn't always expose the methods you need to override... so I can't advise how easy or hard it would be to do what you want.
Upvotes: 1
Reputation: 20617
DropDownList.DataBound
event fires after DropDownList.DataBind()
is called for the entire DropDownList
.
DropDownList.Items
are a ListItemCollection
that have no events.
You will have to manually loop through the DropDownList.Items
collection again or manually build the ListItemCollection
and add it then.
NOTE: An alternative that you probably won't like is to extend DropDownList and ListItemCollection and add the events you want.
Upvotes: 3