ASP.Net:添加客户端onclick到HyperLinkField字段中的GridView [英] ASP.Net: Adding client side onClick to a HyperlinkField in GridView
问题描述
我有一个现有的GridView,其中包含的领域合作伙伴名称。它是由合作伙伴名称排序。结果
现在,我需要改变合作伙伴名称字段,并在一定条件下,使其可点击和警报()什么的。
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.
现有code是:
<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"/>
我添加列:
<asp:hyperlinkfield datatextfield="PartnerName" SortExpression="PartnerName" headertext="Name" ItemStyle-CssClass="text2"/>
这使我能够控制的CSS和排序。但是,我找不到如何将客户端的JavaScript函数添加到它。结果
我发现,添加:
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>
使我通过ID访问LNK,并添加到它的属性。不过,我失去了排序的能力。
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.
推荐答案
我发现用ASP解决方案:的TemplateField这种方式,又不失排序能力,并使用使用eval的datatextfield:
The solution I found was using asp:TemplateField this way, without losing the Sorting ability and using the datatextfield using Eval:
<asp:TemplateField HeaderText="Name" SortExpression="PartnerName">
<ItemTemplate>
<a onclick="javascript:alert('ok')" href="http://<%#Eval("PartnerName")%>"><%#Eval("PartnerName")%></a>
</ItemTemplate></asp:TemplateField>
这篇关于ASP.Net:添加客户端onclick到HyperLinkField字段中的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!