分配一个事件自定义控件Repeater控件内 [英] Assign an event to a custom control inside a Repeater control
问题描述
我有它的一些细胞包含其中包含一个DropDownList一个用户控件Repeater控件。在Repeater控件的ItemDataBound事件,我像这样分配一个事件DropDownList的:
I have a Repeater control which in some of its cells contains a UserControl which contains a DropDownList. On the ItemDataBound event of the Repeater control I'm assigning an event to the DropDownList like so:
protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
...
MyControl myControl = (MyControl)e.Item.FindControl("MyControl01");
myControl.DataSource = myObject;
myControl.DataBind();
myControl.DropDownList.SelectedItemChange += MyMethod_SelectedIndexChanged;
myControl.DropDownList.AutoPostBack = true;
....
}
protected void MyMethod_SelectedIndexChanged(object sender, EventArgs e)
{
//Do something.
}
该事件永远不会触发。请我需要帮助。
The event never fires. Please I need help.
推荐答案
您事件不被在回发提出,因为你的事件处理程序尚未连接(它只是在页面生命周期的迭代过程中附着当你中继器数据绑定)。
Your event is not being raised in a PostBack because your event handler has not been attached (it is only attached during the iteration of the page life-cycle when your repeater is databound).
如果您声明附上您的事件处理程序的标记,如:
If you attach your event handler declaratively in the markup such as:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" />
</ItemTemplate>
</asp:Repeater>
那么你的事件处理程序将回送过程中被调用。
Then your event handler will be called during PostBacks.
这篇关于分配一个事件自定义控件Repeater控件内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!