JavaScript in gridview HyperLinkField NavigateUrl
I have a List of Items which is binded to GridView:
class Item
{
public string CategoryName { get; set; }
public int CategoryID { get; set; }
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="CategoryID" />
<asp:HyperLinkField DataTextField="CategoryName" NavigateUrl="javascript:alert('Hello World');"/>
</Columns>
</asp:GridView>
All that I need to show the CategoryName开发者_Go百科 in the alert window instead of 'Hello world'.
Use a template field instead:
<asp:TemplateField>
<ItemTemplate>
<a href="javascript:alert('<%# Eval("CategoryName") %>');"><%# Eval("CategoryName") %></a>
</ItemTemplate>
</asp:TemplateField>
Also, best practices suggest using onclick in Anchors instead of javascript: Pseudo-Protocol
精彩评论