1

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?

Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
Agniva De Sarker
  • 778
  • 2
  • 12
  • 22
  • http://stackoverflow.com/questions/9603640/sortexpression-in-asp-net-gridview-not-working-with-headertemplate –  Nov 28 '13 at 15:55

2 Answers2

3

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>
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

This is a quite old thread I have stumbled over while trying to solve exactly that described problem but the solution provided here didn't worked for me. If you have a Sorting method defined for the GridView then

<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" Text="Sort"  />

will call that method

protected void GridView_Sorting(object sender, GridViewSortEventArgs e)
{
    dt.DefaultView.Sort = e.SortExpression;

but e.SortExpression will be null and no sorting is happening. You have to first pass the Column's name through the CommandArgument of the LinkButton. Only then it worked in my case!

<asp:LinkButton ID="LbSort" runat="server" CommandName="Sort" CommandArgument="ColumnName" Text="Sort"  />
Barnabeck
  • 459
  • 1
  • 6
  • 26