开发者

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

开发者 https://www.devze.com 2023-03-30 03:38 出处:网络
I have a database and I\'m storing images in it along with a person\'s name, and other attributes. I\'ve databound my listview with a stored procedure. I want to know how I can display an icon on a ro

I have a database and I'm storing images in it along with a person's name, and other attributes. I've databound my listview with a stored procedure. I want to know how I can display an icon on a row depending on if the record for that row has a picture or not for the person...

I'm not sure how to accomplish this however with the templates in asp.net

If you need me to provide any additional info I can.

Thanks.

EDIT:

Here is my template and the s where I'm hoping to put them.

<ItemTemplate>
    <tr style="">
        <td class="width100">
            <asp:Image ID="Image1" runat="server" ImageUrl="./Images/PlayerPictures/nothing.g开发者_JAVA百科if" title="Picture Available" CssClass="icon right" />
            <asp:Image ID="Image2" runat="server" ImageUrl="./Images/PlayerPictures/nothing.gif" title="Charts Available" CssClass="icon right" />
            <asp:Image ID="Image3" runat="server" ImageUrl="./Images/PlayerPictures/nothing.gif" title="Reports Available" CssClass="icon right" />
            <asp:Image ID="Image4" runat="server" ImageUrl="./Images/PlayerPictures/nothing.gif" title="Video Available" CssClass="icon right" />
        </td>
        <td class="width350">
                <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>

I have no code-behind that is relevant I don't think because the Listview is databound like so...

<asp:ListView ID="ListView4" runat="server" 
                                    DataSourceID="ListViewPlayersWithFilter"
                                    DataKeyNames="PlayerKey">


You can use ListView.ItemDataBound Event to display an image based on the column value:

Aspx:

<ItemTemplate>
            <tr style="">
                <td class="width100">
                    <asp:Literal ID="lblPic" runat="server" />
                    ...
                    ...
                </td>
                <td class="width350">
                        <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>

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/PlayerPictures/nothing.gif\" alt=\"Picture Available\" CssClass=\"icon right\" />"
        Else
            lblPic.Text = "<img/> tag to Display Pic"
        End If      
    End If
End Sub
0

精彩评论

暂无评论...
验证码 换一张
取 消