简体   繁体   English

gridview HyperLinkField NavigateUrl中的JavaScript

[英]JavaScript in gridview HyperLinkField NavigateUrl

I have a List of Items which is binded to GridView: 我有一个绑定到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 in the alert window instead of 'Hello world'. 我需要在警报窗口中显示CategoryName而不是“ 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 此外,最佳做法建议在锚点中使用onclick而不是javascript:伪协议

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM