Question

I've had a look through the questions on here, but none seem to answer my question.

I have a ASP.NET repeater grid that is set up to bound to a list that shows what the value of that item is. At the moment, it just shows the text value of the variable i.e. OFF, but what I want it to do is display the appropriate image i.e. if its off, show green image, if its on, show red image.

This is what I'm trying to do in the code, presumed you did it here instead of the aspx.cs?

<td><%# DataBinder.Eval(Container.DataItem, "Spill") == "OFF" ? %>
<asp:Image runat="server" ImageUrl="~/Images/green.JPG" /> 
<%:%> 
<asp:Image runat="server" ImageUrl="~/Images/red.JPG" /> %>
</td> 

The compiler is complaining about the '%>' after the ? and the '%>' after the : so obviously I haven't got it right, but can't think how else to do it.

Thanks for anyones help in advance

Was it helpful?

Solution

Markup:

<asp:Image runat="server" 
   ImageUrl='<%# WhichImage(DataBinder.Eval(Container.DataItem, "Spill")) %>' /> 

Code-behind:

protected string WhichImage(object spill)
{
    string result = "~/Images/green.JPG";
    string spillResult= (string)spill;
    if(!spillResult.Equals("OFF"))
    {
        result = "~/Images/red.JPG";
    }
    return result;
}

OTHER TIPS

To do it on the front side in one line, you would do something like this:

<asp:Image id="imgSpill" runat="server" ImageUrl='<%# (DataBinder.Eval(Container.DataItem, "Spill") == "OFF" ? "~/images/red.jpg" : "~/images/green.jpg") %>' />

Hope this helps! Good Luck!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top