开发者

JavaScript in gridview HyperLinkField NavigateUrl

开发者 https://www.devze.com 2023-01-28 19:23 出处:网络
I have a List of Items which is binded to GridView: class Item { public string CategoryName { get; set; }

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

精彩评论

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