DropDownList SelectedIndexChanged event not firing within GridView HeaderTemplate

Adam Sharif picture Adam Sharif · Jan 21, 2010 · Viewed 9.7k times · Source

I am working on a job board website and specifically the job search page at the moment. I am returning the jobs found into a DataBound GridView (namely gvwJobs), and within the HeaderTemplate I have a DropDownList named ddlSortDirection which specifies the direction in which the sorting is performed:

<asp:DropDownList runat="server" ID="ddlSortDirection" AutoPostBack="true" OnSelectedIndexChanged="ddlSortDirection_SelectedIndexChanged">

    <asp:ListItem Value="DESC">DOWN</asp:ListItem>
    <asp:ListItem Value="ASC">UP</asp:ListItem>

</asp:DropDownList>

As you can see, I have ensured that AutoPostBack is on. I have also created a custom event handler on the GridView's DataBound event as so:

Protected Sub gvwJobs_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvwJobs.RowDataBound

    If e.Row.RowType = DataControlRowType.Header Then

                ddlSortDirection.AutoPostBack = True
                AddHandler ddlSortDirection.SelectedIndexChanged, AddressOf ddlSortDirection_SelectedIndexChanged


     End If

End Sub

I am finding that the ddlSortDirection_SelectedIndexChanged SubProcedure is not called when the selection is changed in ddlSortDirection. I can see that a PostBack occurs, but the method is definitely not called. What I have tried doing is creating a similar DropDownList outside of the GridView and that successfully triggered the SelectedIndexChanged event without even having a custom event handler!

Please could you help me to achieve my goal of having a DropDownList firing the SelectedIndexChanged event when within a HeaderTemplate of a GridView?

Answer

Fawad Ali picture Fawad Ali · Nov 28, 2011

set the EnableViewState of that GridView to false