GridView中的HyperLink列

时间:2013-01-31 18:22:33

标签: c# asp.net gridview hyperlink

我在GridView FirstNameLastName中有两列。我希望FirstName列中的所有行都在超链接中。我试过这个但是没用。我该怎么办?

分析器错误

Parser Error Message: Databinding expressions are only supported on objects 
that have a  DataBinding event. System.Web.UI.WebControls.HyperLinkField does not
have a DataBinding event. 

代码:

<asp:GridView ID="CustomersGridView" runat="server" AutoGenerateColumns="False">
        <Columns>

        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" />

        <asp:HyperLinkField Text='<%# Eval("FirstName") %>' />

    </Columns>
</asp:GridView>

3 个答案:

答案 0 :(得分:3)

试试这个..

<asp:HyperLinkField 
      DataTextField="FirstName" 
      HeaderText="First Name" 
      SortExpression="FirstName" 
      DataNavigateUrlFields="FirstNameID" 
      DataNavigateUrlFormatString="PersonDetails.aspx?FirstNameID={0}" /> //use if you want to nevigate to other page

答案 1 :(得分:1)

尝试使用DataTextField property

<asp:HyperLinkField DataTextField="FirstName" />

答案 2 :(得分:1)

您需要将其转换为模板字段:

<asp:GridView ID="CustomersGridView" runat="server" AutoGenerateColumns="False">
   <Columns>
      <asp:BoundField DataField="FirstName" HeaderText="First Name" />
      <asp:BoundField DataField="LastName" HeaderText="Last Name" />
      <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
         <asp:itemtemplate>
            <asp:HyperLinkField Text='<%# Eval("FirstName") %>' />
    ...