Click here to Skip to main content
15,904,951 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
Here is my code:
XML
<% if (Eval(art_avatar_url) != "")
            {%>
                <img alt="<%#Eval("name")%>" title="<%#Eval("name")%>" src='../img/avatars/<%#Eval("img_url")%>' style="width: 80px; height: 80px">
            <%}
              else
              {%>
                <img alt="<%#Eval("name")%>" title="<%#Eval("name")%>" src="../img/blank.gif" style="width: 80px; height: 80px">
            <%}%>


in this code the error in if condition. I know this is not a correct format to declare if condition in Eval function.
Please help me how to declare this.

Thanks in advance

How to do this in code behind?

please help me how to declare code behind and how to call in source page


See the code where i get wrong:
XML
<img alt="<%#Eval("name")%>" title="<%#Eval("name")%>"
                        src='<%#Eval("avatar_url") != "" ? "../img/avatars/"+ Eval("avatar_url") : "../img/blank.gif"%>'
                            style="width: 80px; height: 80px">
Posted
Updated 14-Jun-11 2:32am
v4

This is ugly has hell. You should do this in your code behind, so your code is readable. Just set up a literal and set it's properties, or even an asp:Image.
 
Share this answer
 
Try that way, it will work

<img alt="<%#Eval("name")%>" title="<%#Eval("name")%>"
                        src='<%#Eval("art_avatar_url") != "" ? "images/"+ Eval("img_url") : "images/blank.gif"%>'
                            style="width: 80px; height: 80px">
 
Share this answer
 
Comments
rahul dev123 14-Jun-11 8:13am    
No its not working. If i used this then the source is src="images/"
nit_singh 14-Jun-11 10:06am    
see "img_url" column, it might be coming blank. because the same is working here
nit_singh 15-Jun-11 4:08am    
may I know the reason for downvoting it
hi,
Try this code below
XML
<img alt='<%#Eval("name")%>' title='<%#Eval("name")%>'
src='<%# string.IsNullOrEmpty(Eval("art_avatar_url").ToString())?"images/blank.gif":"images/"+ Eval("img_url").ToString() %>'
style="width: 80px; height: 80px">

Hope this will address your issue.
 
Share this answer
 
XML
<% if(Eval("img_url") != ""){%>
  some html goes here
<%} else {%>
  some different html goes here
<%}%>


C#
public string GetAppropriateHTML()
{
    string html = "";
    if (Eval("img_url") != "")
    {
        html = "some html goes here";
    }
    else
    {
        html = "some other html goes here"
    }
    return html;
}
 
Share this answer
 
Comments
rahul dev123 14-Jun-11 5:33am    
If i used this code then Error shown like this:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900