Web Development & Execution
David Addison
by David Addison
share this
?fl
« Back to the Blog

Asp:DropDownList does not fire OnSelectedIndexChanged event in an Asp:Repeater

03/11/2010
Asp:DropDownList does not fire OnSelectedIndexChanged event in an Asp:Repeater

It may be fairly easy to create dynamically generated drop down lists in repeaters, however how do you retrieve the data? This brief post will look at how to get data from using the OnSelectedIndexChanged handler for the DropDownList (DDL) control. The OnSelectedIndexChanged event handler is supposed to fire when a user selects a new item in the DDL, but without adding a few other attribrutes to the control it will not fire when selected. Here are the basic steps:

  • Create a protected DataSet variable in your codebehind for storing the data that you want to populate the DDL with.
  • Create a method in the codebehind for handling the OnSelectedIndexChanged event.
  • Set the datasource of the DDL to the protected Dataset.
  • Set the DDL AutoPostBack attribute to true.
  • Set the DDL OnSelectedIndexChanged handler to the method you created in the codebehind.
  • Set EnableViewState to false for the DDL, and the Repeater.

The last step is the one that will hang you up. Without setting the EnabledViewState attribute to false, the method for OnSelectedIndexChanged is never called. Here is some sample code relating to a categorization system:

.aspx

           <asp:Repeater
                runat="server"  
                EnableViewState="false"
                ID="uncategorizedCategories">
                <HeaderTemplate>
                    <table border="0" cellspacing="4" cellpadding="4">
                </HeaderTemplate>
               
                <ItemTemplate>
                    <tr>
                        <td><%# DataBinder.Eval(Container.DataItem, "category") %></td>
                        <td><asp:DropDownList
                            DataSource='<%# this.groups %>'
                            DataTextField="title"
                            DataValueField="groupID"
                            runat="server"
                            OnSelectedIndexChanged="categorySelect_IndexChanged"
                            EnableViewState="false"                           
                            id="groupList"
                            AutoPostBack="true"
                            >
                            </asp:DropDownList></td>
                    </tr>               
                </ItemTemplate>
               
                <FooterTemplate>
                    </table>
                </FooterTemplate>           
            </asp:Repeater>

 

.aspx.cs

    protected void categorySelect_IndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = sender as DropDownList;
        if (ddl != null)
        {
            Response.Write(ddl.SelectedValue);
        }
    }

Thanks!

Thank you for contacting us!

We'll be in touch!

Back Home ×