2014-02-27 66 views
1

我正在使用gridview在页面上显示信息。条件是,当我从数据库结果获得Y赠我需要绑定/images/goldx.png否则/images/check.gif我该怎么办,我现在用asp.net与vb.net作为后端在Gridview中绑定图像

<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 

感谢您的意见和解答。

+0

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

回答

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 

希望这可以帮助你。