ternary trouble in datalist/repeater

  • Thread starter Thread starter Darren
  • Start date Start date
D

Darren

Hi there,

Recently I have changed languages from VB.NET to C#.

While in VB.NET, I wrote the below code to determine if a specific
product in a repeater control had an image. If so, it would display
the appropriate image; if not, would display a placeholder.gif.

I now need to convert the below code to C# and am having a brutal time
at it. I do understand the simple syntax behind using the ternary
operator
ie: (bBoolean)?(aImage):(bImage), but for the life of me cannot make
the below code fly in C#.

<%# IIf(Container.DataItem("hasimage") = true, "<img
src='ProductImgSmall/" & Container.DataItem("prodid") ".gif'>",
"<img 'ProductImgSmall/PlaceHolder.gif' >") %></a>

Thanks in advance,

Darren
 
I'm typing this off of the top of my head, so be warned.

<img src='<%# ((bool)DataBinder.Eval(Container, "DataItem.hasimage")) ?
string.Format( "ProductImgSmall/{0}.gif", DataBinder.Eval(Container,
"DataItem.prodid") ) : "ProductImgSmall/PlaceHolder.gif"%>'>

I'm not familiar with VB.NET, but it seems like it must be doing some
implicit casting. DataBinder.Eval always returns an object, which sometimes
needs to be cast (depending on what you're doing). For instance hasimage
needs to be cast to a bool, but prodid (as an argument to string.Format) does
not.
 
Back
Top