<% if(Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES) { %>
...
<% } else { %>
...
<% } %>
Gives me a InvalidOperationException? How do I write conditional html generation in ASP?
<% if(Convert.ToInt32(Eval("NbrOrders"))>=Config.MAX_ENQUIRY_SALES) { %>
...
<% } else { %>
...
<% } %>
Gives me a InvalidOperationException? How do I write conditional html generation in ASP?
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?
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") %>
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>
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?