Aakash Singh Aakash Singh - 2 months ago 11
C# Question

I want to display an image on a grid view column if it is null and other image if its not null

<asp:BoundField HeaderText="Assigned Tag" DataField="NBFCTagID"/>


If value of NBFCTagID is null then I want to display
red_mark.jpg
else
green_mark.jpg
.

I already tried
<imagefield>
but it wasn't very helpful.

Answer

Add template field like this.

<asp:TemplateField>
    <ItemTemplate>
       <asp:Image ID="imgStatus" runat="server" CssClass="label" ImageURL='<%# GetImage((object)Eval("NBFCTagID")) %>' />
    </ItemTemplate>
</asp:TemplateField>



 public static string GetImage(object value)
    {
        if (value != null)
        { 
            return "../Images/green_mark.jpg";
        }
        else
        {
            return "../Images/red_mark.jpg";
        }
    }