4

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?

1
  • Have you gotten it working? if not, post some more info: the aspx source, current code behind, .net version(1.1/2.0/3.5), vs.net version(2003/2005/2008)
    – KyleLanser
    Oct 7, 2008 at 15:16

7 Answers 7

11

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.

1
  • I discovered that it was EnableViewState="false" on the Repeater itself that was blocking registration and execution of the postback handler registered for its footer control. Once set true the simple/obvious specification worked.
    – fortboise
    Oct 18, 2011 at 16:25
5

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.

1
  • 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
    May 13, 2010 at 5:45
2

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.

2

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.

1

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.

1
  • I have tried setting AutoPostBack in the markup and the code-behind.
    – jwarzech
    Oct 6, 2008 at 20:04
1

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.

1

Make sure ViewState is enabled for dropdownlist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.