ASP.NET: ListView display Image based on database column being Null or not.

Display images from SQL server database using asp.net.

I try and got solution is below:

Just typed below code in your aspx page:


<ItemTemplate>
<tr style="">
<td>
<asp:Literal ID="lblPic" runat="server" />
...
...
</td>
<td>
<a href="?team=<%# TeamNumber() %>&player=<%# Eval("PlayerKey") %>"><asp:Label ID="PlayerLabel"

runat="server" Text='<%# Eval("Player") %>' /></a>
</td>
<td>
<asp:Label ID="PositionLabel" runat="server" Text='<%# Eval("Position") %>' />
</td>
</tr>
</ItemTemplate>

Just type the below code into your code behind.


Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim lblPic As Literal = CType(e.Item.FindControl("lblPic"), Literal)
Dim rowView As System.Data.DataRowView
rowView = CType(e.Item.DataItem, System.Data.DataRowView)

If Convert.IsDbNull(rowView("hasPic") = true then
lblPic.Text = "<img src=\"/Images/sports/never.gif\" alt=\"Picture Available\" CssClass=\"icon right\" />"
Else
lblPic.Text = "<img/> To Display Pic"
End If
End If
End Sub

About the Author

avatar