开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜