在Gridview中绑定图像

时间:2014-02-27 09:22:18

标签: asp.net vb.net visual-studio-2010

我正在使用gridview在页面上显示信息。条件是当我从数据库结果得到Y时,我需要绑定/images/goldx.png Else /images/check.gif我怎么能这样做我使用asp.net和vb.net作为后端

<asp:GridView  ID="grdLocation" runat="server" Width="100%" AutoGenerateColumns="false"  >
   <Columns>
     <asp:TemplateField HeaderText="Monthly" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
     <ItemTemplate>
        <asp:Label ID="lblLotName" runat="server" Text='<%#Eval("is_monthly") %>'></asp:Label>
        <asp:Image ID="resultImage" runat="server" ImageUrl='<%# Eval("is_monthly") == 'Y'  ? "~/Images/check.gif" : "~/Images/goldx.png" %>' />
     </ItemTemplate>
   </asp:TemplateField>
 <Columns>
</asp:GridView>

我绑定Gridview的代码:

Protected Function bindLocations()
    Try
        Dim _ds As DataSet
        If _locComp Is Nothing Then
            _locComp = New LocationComponent()
        End If
        _ds = _locComp.GetAllLots()

        If _ds.Tables(0).Rows.Count > 0 Then
            grdLocation.DataSource = _ds
            grdLocation.DataBind()

        End If
    Catch ex As Exception

    End Try

End Function

感谢您的评论和回答。

1 个答案:

答案 0 :(得分:1)

您可以检查OnRowDataBound GridView事件。像

Private Sub grdLocation_OnRowDataBound(sender As Object, e As GridViewRowEventArgs) Handles grdLocation.RowDataBound

 If e.Row.RowType = DataControlRowType.DataRow Then
  Dim lblLotNametxt as String = CType(e.Row.FindControl("lblLotName"),Label).Text
  If lblLotNametxt = "Y" Then
    CType(e.Row.FindControl("resultImage"),Image).ImageUrl = "~/Images/check.gif"
  Else
    CType(e.Row.FindControl("resultImage"),Image).ImageUrl = "~/Images/goldx.png"
  End If
 End If

End Sub

希望这会对你有所帮助。