tags:

views:

4960

answers:

9

I have a repeater control where in the footer I have a DropDownList. In my code-behind 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 page appear to PostBack however my EventHandler does not get called. Any ideas?

+2  A: 

I think it's because you're probably not databinding on postbacks. I haven't tested this, but try hooking that code up to the ItemCreated event for your repeater instead.

John Rudy
+1  A: 

Is the AutoPostBack property set to True on the DropDownLists on the ASPX side? I know sometimes this property doesn't get set initially and it will prevent the SelectedIndexChanged event from firing.

Dillie-O
I have tried setting AutoPostBack in the markup and the code-behind.
jwarzech
+8  A: 

If you just want to fire the 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 - Code-behind

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

And that's it. Nothing more is needed.

KyleLanser
This does not work for me. The event never fires.
johnny
Worked for me... thanks.
Kyle B.
+1  A: 

In this case your parent repeater (ddMyRepeater) must databind itself in page_load on every postback. This is the only way I've found to get nested controls to fire their events.

This may not be the ideal scenario for you, though. Depending on what your page is doing, you may have to databind this control, twice. Once to get the events to fire and a second time if a fired event causes the repeater's data to change in any way.

Chad Braun-Duin
A: 

I have the same situation - some controls in a repeater...a couple LinkButtons, a DropDownList - didn't do anything to manually add event handlers durng databind.

The event's for the LinkButtons fire fine, but OnSelectedIndexChanged and OnTextChanged are doing NOTHING.

+2  A: 

I think the problem comes from the fact that the dropdownlist control is not inside the repeter, but on the footer. I don't think that the envent of the reperter fires for the controls that are on the footer. You should try to put the dropdowncontrol out of the repeater control.

Florjon
+1  A: 

Make sure ViewState is enabled for dropdownlist

+1  A: 

If the DropDownList is within a Repeater then to make the SelectIndexChanged event fire, you need to disable EnableViewState on the GridView / Repeater.

e.g.

EnableViewState="false"

You also need to databind the GridView / Repeater on each postback so databind it in the Page Load method.

KevinUK
I had this same problem after being dragged back to WebForms from MVC for the last three years. Disabling ViewState on the grid was the solution that finally fixed my problem. Thanks for posting this.
Tim Hardy
A: 

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

With respect to the above code, the events of the elements in the footer row does not get reflected in the code behind file. How to trace the events of the dropdownlist present in the Footer row of a datagrid in the code behind file?

Archana