我将一个具有AutoPostBack的DropDownList放在一个Repeater中。
(ListItems填充在转发器的ItemDataBound上)
<asp:Repeater ID="rptWishlist" OnItemCommand="rptWishlist_ItemCommand" onItemDataBound="rptWishlist_ItemDataBound" runat="server">
<ItemTemplate>
...
<asp:DropDownList ID="ddlSize" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlSize_SelectedIndexChanged" />
...
protected void ddlSize_SelectedIndexChanged(object sender, EventArgs e)
{
//This function is never called
}
我做错了吗?
先感谢您。
最佳答案
要注册下拉列表以进行回发,请添加以下代码:
protected virtual void RepeaterItemCreated(object sender, RepeaterItemEventArgs e)
{
DropDownList MyList = (DropDownList)e.Item.FindControl("ddlSize");
MyList.SelectedIndexChanged += ddlSize_SelectedIndexChanged;
}
然后在您的aspx文件中,将其添加到中继器标记中:
OnItemCreated="RepeaterItemCreated"
然后,在ddlSize_SelectedIndexChanged函数中,您可以像这样访问父控件:
DropDownList d = (DropDownList)sender;
(RepeaterItem) d.Parent...
希望这可以帮助。
关于c# - 中继器: How to handle SelectedIndexChange and get DataItem?中的DropDownList,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9509834/