Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DropDownList in Repeater control, can't fire SelectedIndexChanged

Tags:

c#

asp.net

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?

like image 757
jwarzech Avatar asked Nov 29 '22 21:11

jwarzech


2 Answers

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.

like image 148
KyleLanser Avatar answered Dec 01 '22 09:12

KyleLanser


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.

like image 26
KevinUK Avatar answered Dec 01 '22 11:12

KevinUK