ASP.Net:将客户端 onClick 添加到 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.
现有代码为:
<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 使用数据文本字段:
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 添加到 GridView 中的超链接字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!