Home > ASP.Net > Display Image in GridView depanding of condition

Display Image in GridView depanding of condition


Hi

try this examples:

(In case there is NULL  value in your data, it will show a blank.gif)

<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# (Eval("yesOrNo").Equals(true) ? "~/Images/logos/yes.gif" : (Eval("yesOrNo").Equals(false) ? "~/Images/logos/no.gif" : "~/Images/logos/blank.gif"))%>' />
</ItemTemplate>

//———- Or —————-
If there is always a value in your bit (boolean) field, you can simplify to:

<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# (Eval("yesOrNo").Equals(true) ? "~/Images/logos/yes.gif" : "~/Images/logos/no.gif")%>' />
</ItemTemplate>

//————-Or—————-

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Image ID="Image1" ImageUrl='<%# ChooseImage(Directcast(Eval("BooleanValue"), Boolean)) %>' runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
</asp:GridView>

in Code-behind:

    Protected Function ChooseImage(ByVal value As Boolean) As String
        If value = True Then
            Return "~/images/GreenTick.gif"
        Else
            Return "~/images/RedCross.gif"
        End If
    End Function

//———–Or ———–

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <ItemTemplate>
                            <asp:Image ID="Image1" ImageUrl="~/images/yes.gif" Visible='<%# Eval("booleanColumn") %>' runat="server" />                           
                            <asp:Image ID="Image2" ImageUrl="~/images/no.gif" Visible='<%# Not Eval("booleanColumn") %>' runat="server" />

                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
</asp:GridView>

—— Or ———

<asp:Label ID="Label2" runat="server" Text='<%# (bool)Eval("MyBool") == true ? "Yes" : "No" %>' />

Good Luck

Advertisements
Categories: ASP.Net
  1. November 23, 2012 at 2:12 pm

    Thank You soooo much!

    Regards!

    • yasserzaid
      January 20, 2013 at 6:37 am

      @Juan Alberto : You are welcome

  2. Jeyachandran
    January 18, 2013 at 12:55 pm

    Good Article.. Very useful..

    • yasserzaid
      January 20, 2013 at 6:38 am

      @Jeyachandra : you are welcome … Good Luck.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: