【问题标题】:Bind image in Gridview在 Gridview 中绑定图像
【发布时间】:2014-02-27 09:22:18
【问题描述】:

我正在使用 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

感谢您的评论和回答。

【问题讨论】:

  • 其实我不知道vb,但我认为你必须使用gridview的rowdatabound事件,你可以从数据库中捕获Y并在图像控件中使用适当的图像。

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


【解决方案1】:

您可以检查OnRowDataBoundGridView 事件。喜欢

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

希望对你有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    相关资源
    最近更新 更多