tags:

views:

1168

answers:

4
<% if(Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES) {  %>
...  

<% } else { %>
...                                        

<% } %>

Gives me a InvalidOperationException? How do I write conditional html generation in ASP?

A: 

I can't find something wrong in your sentences but comparative you made between Config.MAX_ENQUIRY_SALES and Convert.ToInt32(Eval("NbrOrders")). Are these operator of the same type? Can you show the type of each one in your web page?

jaloplo
+3  A: 

I'm not sure you can add brackets for the conditional binding, the only way I know of doing it is with an inline statement like so:

<%# Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES) ? Eval("IfTrueValue") : Eval("IfFalseValue") %>
John_
Yes, I suspected this...my problem is that I need to generated a bunch of html in each case... The whole point of ASP sort is lost...
Niels Bosma
+6  A: 

Use an inline statement as John_ states, or, create a function in your code behind that performs the logic required.

protected string MyFunction(int nbrOrders)
{
    if(nbrOrders>=Config.MAX_ENQUIRY_SALES)
    {
        return "TrueResult";
    }
    else
    {
        return "FalseResult";
    }
}

Then use this as follows

<%# MyFunction(Convert.ToInt32(Eval("NbrOrders"))) %>

EDIT: I've just read a comment on another post that states you want to show different HTML depending on this result. In that case, you can try using the Visible flag of a placeholder containing your code. Such as:

<asp:PlaceHolder id="PlaceHolder1" runat="server" visible='<%# (Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES)%>'>
    <div>My True Html Here</div>
</asp:PlaceHolder>
<asp:PlaceHolder id="PlaceHolder2" runat="server" visible='<%# !(Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES)%>'>
    <div>My FalseHtml Here</div>
</asp:PlaceHolder>
Robin Day
The EDIT is a really useful tip. Thanks.
Si Keep
A: 

if/else blocks work in ASP .NET as you expect them to. The following works just fine.

<% if(DateTime.Now.Second % 2 == 0) {  %>
<div>Even</div>
<% } else { %>
<div>Odd</div>
<% } %>

Perhaps the conditional logic in your example is throwing an exception?

Dustin Campbell
I think its the Eval that causes the problem. You cant mix conditional logic within the databinding.
Si Keep