ASP.Net: Adding client side onClick to a HyperlinkField in GridView

Posted by Nir on Stack Overflow See other posts from Stack Overflow or by Nir
Published on 2010-05-30T10:31:53Z Indexed on 2010/05/30 10:32 UTC
Read the original article Hit count: 658

Filed under:
|
|

I have an existing GridView which contains the field "partner name". It is sortable by partner name.
Now I need to change the Partner Name field and in some condition make it clickable and alert() something.

The existing code is:

  <asp:GridView ID="gridViewAdjustments" runat="server" AutoGenerateColumns="false" AllowSorting="True" OnSorting="gridView_Sorting" OnRowDataBound="OnRowDataBoundAdjustments" EnableViewState="true">
         <asp:BoundField DataField="PartnerName" HeaderText="Name" SortExpression="PartnerName"/>

I've added the column:

<asp:hyperlinkfield  datatextfield="PartnerName" SortExpression="PartnerName" headertext="Name" ItemStyle-CssClass="text2"/>

which enables me to control the CSS and sort. However, I can't find how to add a client side javascript function to it.
I found that adding :

    <asp:TemplateField HeaderText="Edit">                                     
<ItemTemplate>
      <a id="lnk" runat="server">Edit</a>      

enable me to access "lnk" by id and add to its attributes. However, I lose the Sort ability.

What's the correct solution in this case?
Thanks.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about gridview