Hi, I want to use if condition
in .ascx file. As shown below:
<%= if (value.equals("xyz")) {} %>
as shown above, if i use like that. then i am getting error of "invalid expression if
".
please guide me.
Instead of <%=
you should use <%
(without the =
sign):
<% if (value.equals("xyz")) { } %>
<%=
is used when you want to output the result of the expression directly to the HTML.
This is because the expression does not evaluate to a string, that can be included in the markup, so the <%=
notation cannot be used. You can do it with the conditional operator:
<%= condition ? "value if true" : "value if false" %>
Or you can insert a code block using this notation:
<% if (value.equals("xyz")) { } %>
Just be aware that you then need to Response.Write
any output you want within the curly braces. This is not best practice - try to avoid logic in your markup.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With