Click here to Skip to main content
15,898,036 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
How to declare if in source file

C#
<% if(Eval("img_url") != ""){%>


I am using like this but it shown error

Please any one can help me
Thanks in advance

can i declare like this:

XML
public string GetAppropriateHTML()
    {
        string html = "";
        if (Eval("a_comment_counter") == "")
        {
            html = "<p style="font-size: .9em; text-align: right; color: #808080; border-bottom: 1px #d8d8d8 solid; padding-bottom: 10px"><i>0 comments</i>";
        }
        else
        {
            html = "<a href="a_comments.aspx?a_id=<%# Eval("a_id")%>"><%# Eval("a_comment_counter")%> comments</a>";
        }
        return html;
    }


but it not supported
Posted
Updated 3-Jun-11 3:02am
v4
Comments
Tech Code Freak 7-Jun-11 10:08am    
Just let everyone know whether you have got the solution and if Yes, then WHAT is IT?
If it is from one of these solutions, then just Accept them!

You cant use if-else construct with Eval function.
This is bcoz:
Eval can be written only in <%# %> whereas if-else cant be written there.
Also, if condition cant be applied to Eval etc.
Applying condition[^]
This link will be very careful and will definitely solve your problem.
 
Share this answer
 
Your sample either isn't cpomplete, or you just didn't copy it all:

C#
<% if(Eval("img_url") != ""){%>
  some html goes here
<%} else {%>
  some different html goes here
<%}%>


Notice the closing brace at the end.

I prefer to put logic in the code-behind in orer to keep the aspx file as clean as possible.

C#
public string GetAppropriateHTML()
{
    string html = "";
    if (Eval("img_url") != "")
    {
        html = "some html goes here";
    }
    else
    {
        html = "some other html goes here"
    }
    return html;
}


Which reduces the clutter in the aspx file to this:

C#
<%=GetAppropriateHTML() %>
 
Share this answer
 
v3
Comments
rahul dev123 3-Jun-11 7:23am    
I already declare like this but error on Eval
#realJSOP 3-Jun-11 7:25am    
Well, you didn't freakin' say that. Use the debugger and see what the problem in your method is. It would be easier to debug if you put your code in the code-bhind file like I suggested.
rahul dev123 3-Jun-11 7:36am    
If condition satisfied then i need
<%# Eval("a_comment_counter")%> comments
like this but how can i declare. please help me
RakeshMeena 3-Jun-11 7:25am    
My 5. Nice solution, I implemented something like this in one of my previous assignments. Thanks!

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