asp.netgridviewtemplatefield

SortExpression in ASP.NET GridView not working with <HeaderTemplate>


I have an ASP.NET GridView. Now I am adding the SortExpression property tothe <TemplateField> tags, to make specific columns sortable.

Now, one of the columns has some markup content to be added in the header. The problem is, SortExpression does not work if there is a <HeaderTemplate> tag in a <TemplateField>, you have to put it inside the HeaderText property of <TemplateField>. But, all the HTML content does not work properly if I dump it inside the HeaderText property.

<asp:TemplateField SortExpression="FK_TesterID" ItemStyle-Width="300px" FooterStyle-Width="300px" ItemStyle-HorizontalAlign="Center" FooterStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
        <HeaderTemplate>
        <table width="100%">
        <tr>
        <td align="center">
        Tester
        </td>
        </tr>
        <tr>
       <td>
   <asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName"  DataValueField = "PK_ID" Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged" ToolTip="Bulk Assign Testers !"  ></asp:DropDownList>
         </td>
       </tr>
        </table>
         </HeaderTemplate>

So you can see, if I put the entire <HeaderTemplate> property inside a headertext, it doesn't work.

But I want to have both functionalities. Can anyone help?


Solution

  • Then you need to provide a control in your HeaderTemplate with CommandName="Sort", for example a LinkButton.

        <HeaderTemplate>
            <table width="100%">
                <tr>
                    <td align="center">
                        <asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort"  />
                    </td>
                </tr>
                <tr>
                    <td>
                        <asp:DropDownList ID="cmbTestersHeader" ClientIDMode="Static" runat="server" Width="300px"
                            DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField="PK_ID"
                            Visible="false" AutoPostBack="true" OnSelectedIndexChanged="cmbTestersHeader_SelectedIndexChanged"
                            ToolTip="Bulk Assign Testers !">
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
        </HeaderTemplate>