给定一个 asp 中继器和一个下拉列表。我正在中继器上进行数据绑定并有一个 ItemDataBound 事件。触发事件的我将下拉列表绑定到某个数据源,并为 SelectedIndexChanged 处理程序分配 autopostback true。
每个下拉菜单都会调用处理程序(在我的情况下我有 5 个),但对于每个下拉菜单,“发件人”始终是第一个。
风景 :
<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.
}