DropDownList in repeater control cannot start SelectedIndexChanged

I have repeater control where there is a DropDownList in the footer. In my code, I have:

protected void ddMyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { // Item binding code } else if (e.Item.ItemType == ListItemType.Footer) { DropDownList ddl = e.Item.FindDropDownList("ddMyDropDownList"); // Fill the list control ddl.SelectedIndexChanged += new EventHandler(ddMyDropDownList_SelectedIndexChanged); ddl.AutoPostBack = true; } } 

The PostBack page will appear, however my EventHandler is not called. Any ideas?

+4
source share
7 answers

If you just want to run OnSelectedIndexChanged, this is how it should look:

Page.aspx - Source

 <FooterTemplate> <asp:DropDownList ID="ddlOptions" runat="server" AutoPostBack="true" onselectedindexchanged="ddlOptions_SelectedIndexChanged"> <asp:ListItem>Option1</asp:ListItem> <asp:ListItem>Option2</asp:ListItem> </asp:DropDownList> </FooterTemplate> 

Page.aspx.cs - back code

 protected void ddlOptions_SelectedIndexChanged(object sender, EventArgs e) { //Event Code here. } 

What is it. Do not need anymore.

+11
source

If DropDownList is inside the repeater, then to create the SelectIndexChanged event, you need to disable EnableViewState in the GridView / Repeater.

eg.

 EnableViewState="false" 

You also need to attach a GridView / Repeater file to each postback to attach it to the page loading method.

+5
source

I think this is because you probably are not attached to the return mail. I have not tested this, but try hooking this code before the ItemCreated event for your repeater.

+2
source

I think the problem is that the dropdownlist control is not located inside the repeater, but on the footer. I don't think envent of the reperter works for controls that are on the footer. You should try to get dropdowncontrol out of the repeater.

+2
source

Is the AutoPostBack property equal to True in ASPD side DropDownLists? I know that sometimes this property is not set initially, and this will prevent the SelectedIndexChanged event from being fired.

+1
source

In this case, your parent repeater (ddMyRepeater) should have a database binding in page_load for every postback. This is the only way to find nested controls to trigger your events.

This may not be the perfect scenario for you. Depending on what your page is doing, you may need to bind this control twice. Once, to activate events, and a second time, if the event caused by the dismissal causes the relay data to change in any way.

+1
source

Make sure ViewState is enabled for the drop down list.

+1
source

Source: https://habr.com/ru/post/1277510/


All Articles