Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use if condition in ascx file

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.

like image 336
Khilen Maniyar Avatar asked Jul 28 '11 11:07

Khilen Maniyar


2 Answers

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.

like image 127
Darin Dimitrov Avatar answered Oct 15 '22 17:10

Darin Dimitrov


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.

like image 5
driis Avatar answered Oct 15 '22 16:10

driis