问题
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>
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 to but that didn't do anything. I would appreciate your answers.
回答1:
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>
来源:https://stackoverflow.com/questions/23745611/replacing-headertext-with-headertemplate-in-templatefield