【问题标题】:Binding data in Hyperlink Column of GridViewGridView超链接列绑定数据
【发布时间】:2013-10-17 19:56:47
【问题描述】:

我有一个名为customerArrayList 的ArrayList 来绑定一个GridView。通常当我将数据绑定到模板列时,我使用 Eval 从 ArrayList Datasource 读取和显示数据,它可以工作。

<asp:TemplateColumn HeaderText="Address">
  <ItemTemplate>
    <asp:Label ID="Label4" runat="server" Text='<%# Eval("Customer.Address1") %>' />
  </ItemTemplate>
</asp:TemplateColumn>

但是如何将数据绑定到超链接列?当我使用以下代码时,它给了我一个错误。

<asp:HyperLinkColumn HeaderText="Customer Name" DataTextField="Customer.CustomerName" 
DataNavigateUrlFormatString="CustomerPage.aspx?funcName=fromSearch&customerID={0}"
DataNavigateUrlField="Customer.CustomerID" />   

在所选数据源中找不到具有该名称的字段或属性

我认为这行给了我错误:

DataTextField="Customer.CustomerName"

谁能告诉我我的代码有什么问题?

【问题讨论】:

  • 你确定表Customer中有Customer列
  • 您检查过数据源的输出是否包含名为 customername 的字段
  • @sansknwoledge 是的,它有 CustomerName
  • 您使用的是 DataGrid 还是 GridView? asp:HyperLinkColumn 用于 DataGrid
  • 简单地使用CustomerName怎么样?

标签: c# asp.net gridview data-binding hyperlink


【解决方案1】:

使用这个例子

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">           
        <Columns>
            <asp:BoundField DataField="CustomerID" />
            <asp:HyperLinkField DataNavigateUrlFields="CustomerID"                  DataNavigateUrlFormatString="CustomerPage.aspx?funcName=fromSearch&customerID={0}"
                DataTextField="Customer Name" NavigateUrl="CustomerPage.aspx" />
            <asp:BoundField DataField="datafield" />
        </Columns>
    </asp:GridView>

或在 Grid_RowDataBound() 事件上播放。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-10
    • 2014-01-27
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多