as shown above if ...">

Use if condition in ascx file

Hi, I want to use if condition in a .ascx file. As below:

 <%= if (value.equals("xyz")) {} %> 

as shown above if i use it like that. then I get the error " invalid expression if ".

please guide me.

+6
source share
3 answers

Instead of <%= you should use <% (unsigned = ):

 <% if (value.equals("xyz")) { } %> 

<%= used when you want to output the result of an expression directly in HTML.

+15
source

This is because the expression does not evaluate the string that can be included in the markup, so note <%= cannot be used. You can do this with a conditional statement:

 <%= condition ? "value if true" : "value if false" %> 

Or you can insert a block of code using this notation:

 <% if (value.equals("xyz")) { } %> 

Just remember that then you need Response.Write any output you want in braces. This is not a good practice - try to avoid logic in your markup.

+5
source

The above answers cannot be used for boolean attributes such as Visible. Instead, enter this code in the BindData () function.

 if (condition) { this.pnlMyPanel.Visible = true; } else { this.pnlMyPanel.Visible = false; } 

If you are not using the BindData () font, do not put it in Page_Load in

 if (!this.IsPostBack) {} 

block.

0
source

Source: https://habr.com/ru/post/893766/


All Articles