Given an asp repeater and a dropdown inside it. I am doing a databind on the repeater and having an ItemDataBound event. Ones the event is fired I am binding the dropdown to some data source and assigning SelectedIndexChanged handler with autopostback true.
The handler is called for each dropdown ( where I have 5 in my case ) but for each of them the "sender" is always the first one.
The view :
<asp:Repeater ID="OptionsRepeater" runat="server">
<ItemTemplate>
<asp:DropDownList ID="ddlOptions" runat="server">
</ItemTemplate>
</asp:Repeater>
リピーター データソース コードは正常です。
OptionsRepeater.DataSource = someDataSource;
OptionsRepeater.ItemDataBound += new RepeaterItemEventHandler(this.OptionsRepeaterItemDataBound);
OptionsRepeater.DataBind();
イベント ハンドラー:
protected virtual void OptionsRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// Get the view class for the current repeater record
var dropDownData = e.Item.DataItem;
DropDownList dropDownList = (DropDownList)e.Item.FindControl("ddlOptions");
if (dropDownList != null)
{
dropDownList.DataSource = dataSource;
dropDownList.DataTextField = "Title";
dropDownList.DataValueField = "Id";
dropDownList.AutoPostBack = true;
dropDownList.DataBind();
dropDownList.SelectedIndexChanged += DropDownListSelectedIndexChanged;
}
}
}
protected void DropDownListSelectedIndexChanged(object sender, EventArgs e)
{
//((DropDownList)sender).UniqueID is always the id of the first combo changed and never on the real one.
}