sortingwebformscustom-attributesaspxgridviewtemplatefield

Replacing HeaderText with Headertemplate in TemplateField


I have a gridview with templatefield columns. The TemplateFields are something like this:

<asp:TemplateField HeaderText="Title" SortExpression="name" meta:resourcekey="BoundFieldResource1">
    <ItemTemplate>
        <asp:Label ID="lblTitle" runat="server" Text='<%# Bind("Name") %>' meta:resourcekey="BoundFieldResource1"></asp:Label>
    </ItemTemplate>
</TemplateField>

I have to add a custom attribute for to the header of this columns so I removed HeaderText and added the following:

<Headertemplate>
    <asp:Label ID="lblTitleHeading" runat="server" Text="Title" data-custom="tbl-th_title_heading"></asp:Label>
</Headertemplate>

My problem is when I do this, it will break the sorting of that column and I cannot click on the header to sort it anymore, I tried changing the asp:Label to asp:LinkButton but that didn't do anything. I would appreciate your answers.


Solution

  • Ok I just found out how to do this, I just needed to change the Label to LinkButton and add CommandName="Sort" and CommandArgument="name" (or any SortExpression)

    <Headertemplate>
         <asp:LinkButton ID="lblTitleHeading" runat="server" Text="Title" data-custom="tbl-th_title_heading" CommandName="Sort" CommandArgument="name"></asp:LinkButton>