Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

c# - Assign an event to a custom control inside a Repeater control

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...