Replacing HeaderText with Headertemplate in TemplateField
Asked Answered
H

1

4

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.

Herefordshire answered 19/5, 2014 at 19:22 Comment(0)
H
6

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>

Herefordshire answered 20/5, 2014 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.