Condition on Container.DataItem in inline code

  • Thread starter Thread starter az bij
  • Start date Start date
A

az bij

Hi,

I have a dataset bound to a repeater and want to place some logic in
my ascx file. I want to display an image link if and only if an image
is available.

I have tried without success variations of:

<%#if(DataBinder.Eval(Container.DataItem,"ImageFilename"!="")){%>
<a href="#"><img src=<%#DataBinder.Eval(Container.DataItem,
"ImageFilename")%>/></a>
<%}%>


The above gives: Compiler Error Message: CS1513: } expected

Replacing the last <%}%> with <%#}%> gives: CS1525: Invalid expression
term 'if'
And if I remove the # from the first directive: CS0246: The type or
namespace name 'Container' could not be found

It's driving me mad. Can anyone help please?

Thanks,
Andy
 
Have not used the Repeater before, but for DataBinding statement in DataGrid
and DataList you need to use the IIF statement for conditional processing.

e.g.
iif(DataBinder.Eval(Container.DataItem, "ImageFilename") = "", "", "<a href
..... etc")

Also, note position of first closing parenthesis. You need to resolve the
DataBinder.Eval statement before testing it against the empty string.

Cheers,
Neil
 
Thanks a lot Neil.

got there in the end..

<%# ((DataBinder.Eval(Container.DataItem,
"ImageFilename").ToString()=="") ? "" :"<a
href="+DataBinder.Eval(Container.DataItem, "link")+"><img
src='/Images/Products/"+DataBinder.Eval(Container.DataItem,
"ImageFilename")+"' border='0' /></a>")%>
 
Back
Top