user710502
user710502

Reputation: 11471

How to set ImageUrl based on the value of a field in a gridview

I have the following line of code which evaluates whether the value is true or false, if its true it will show an image if its false it shows a different one.

<itemTemplate>
<img alt="" id="Img1" 
 src='<%# MyAdmin.GetCheckMark((bool)DataBinder.Eval(Container.DataItem, "ShowImg"))%>' 
 runat="server" /></ItemTemplate>

Can I customize the Eval part in the back end code in c# and then pass a different variable to it for example in C# I want to do

  bool ImgFlag = false;
  if(Entity.ShowImg == false && Entity.SomethingElse == true)
   {
      ImgFlag = true;
   }
  else if(Entity.ShowImg == false && Entity.SomethingElse == false)
  {  
      ImaFlag = false;
  }
  else
  {
      ImgFlag = true;
  }

And then I want to use ImgFlag instead of ShowImg in my GridView on each row, so ImgFlag will determine which flag to show..

Or is there a better way?

The issue is that that eval depends now on two things.. not one as before.

Thank you

Upvotes: 1

Views: 2009

Answers (2)

Mubarek
Mubarek

Reputation: 2689

This does the trick

<asp:TemplateField ShowHeader="False">
<ItemTemplate>
   <asp:Image runat="server" ImageUrlt='<%#(bool)Eval("bit_field")? "first.jpg":"second.jpg" %>'>  
  </asp:Image>
</ItemTemplate>

Or this

<asp:TemplateField HeaderText="YourField">
<ItemTemplate>
    <asp:ImageButton runat="server" ImageUrl="True.jpg" Visible='<%# (bool)Eval("bit_field") %>' />
    <asp:ImageButton runat="server" ImageUrl="False.jpg" Visible='<%# !(bool)Eval("bit_field") %>' />
</ItemTemplate>     

Found in this question


Upvotes: 2

the_joric
the_joric

Reputation: 12226

Eval actually returns a property, so you can just call it twice for different properties.

Kind of

MyAdmin.GetCheckMark2((bool)DataBinder.Eval(Container.DataItem, "ShowImg"),
         (bool)DataBinder.Eval(Container.DataItem, "SomethingElse"));

may help

Upvotes: 1

Related Questions