ternary trouble in datalist/repeater

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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top