Tell Us About Your Business

Your name:
Business name:
E-mail address:
Phone number:
Comments or questions:
Subscribe to our newsletter:
Tickets |  Project Management |  207-347-7360

Search

Popular Tags

Archive

About Dirigo

Dirigo's roots are in retail, catalog, television and radio direct response marketing. We're responsible for multi-million dollar web businesses. Whether its sharing our experience and expertise or helping connect you to some of the best thinkers in our industry, we dig deep to find opportunities that drive revenue.

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

March 11 / Payson Welch, Sr. Tech

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);
        }
    }