Use server-side scripting delimiters / ASP.NET inline expressions in non-ASP elements.

I get this error message:

Unable to create an object of type "System.Boolean" from the string view <%: false%> for the property "Visible".

When I try to run this code on my ASP.net website:

<a runat="server" visible='<%: false %>' href="~/" >Home</a> 

Is there a syntax error? false should be replaced with any method result similar to:

 <asp:Panel runat="server" Visible='<%: GetTrueOrFalse() %>'>Home</a> 
+5
source share
4 answers

You can also declare a public boolean value and use it. You will need to use DataBind() if the link is outside of GridView / Repetater, etc.

 public bool isVisible = true; protected void Page_Load(object sender, EventArgs e) { DataBind(); } 

Now you can use this on aspx.

 <a runat="server" visible='<%# isVisible %>' href="~/">Home</a> 

However, you can also use a three-dimensional operator based on another variable or class value inside your code.

 public int myValue = 11; public Book myBook = new Book() { category = "Fantasy" }; protected void Page_Load(object sender, EventArgs e) { DataBind(); } 

Now you can set the visibility based on myValue , although it is not logical.

 <a runat="server" visible='<%# myValue > 10 ? true : false %>' href="~/">Home</a> //or <a runat="server" visible='<%# myBook.category == "Fantasy" ? true : false %>' href="~/">Home</a> 
+1
source

Suppose you have a method that returns a bool value as follows:

 public bool IsVisible() { if (some_condition) // example condition test { return true; } else { return false; } } 

You need to use binding, for example:

Aspx

 <a runat="server" visible='<%# IsVisible() %>' href="~/" >Home</a> 

ASPX.CS (code-behind)

 protected void Page_Load(object sender, EventArgs e) { // do something Page.DataBind(); } 

Note. This trick applies to any methods or properties that return a bool .

Update 1:

Since the a tag does not set the id attribute, you can remove runat="server" :

 <a visible='<%# IsVisible() %>' href="~/" >Home</a> 

Or use CSS with display: none or visibility: hidden :

 <a visible='<%# IsVisible() %>' href="~/" style="visibility:hidden; display:none;">Home</a> 

Link:

Is the <% =%> code rendering block useful for the boolean type?

+2
source

Using this syntax <%: ... %> will increase your parser error. The correct syntax for the data binding server management values ​​is <%# ... %> . A detailed description inside the line is here .


And you can do it differently:

 <% if(GetTrueOrFalse()) { %> <a ID="alink" runat="server" href="~/" >Home</a> //... other code <% } %> 
+2
source

Learn the technique that you are going to use. Server controls or containers can be easily handled on the server side. How? it’s good that you did the first part, and you play runat="server" now you just need to give it an identifier so that it looks something like this, let it call id MyLink

 <a runat="server" id="MyLink" href="~/" >Home</a> 

-Then you noticed that I removed the Visible attribute. yes, because now we will have full control of it on the server side. Suppose you want to start from the first page with a hidden, well, that’s easy: in your pageload event, we will use a good technique to determine that the code we will write will only run on the first load.

  protected void Page_Load(object sender, EventArgs e) { //this condition means if is not post back (meaning the very first time only) if(!IsPostBack) { MyLink.Visible = false; } } 

Now you got it, you can just make your control visible again whenever you just want

 MyLink.Visible = true; 

and done. lemme knows if you need more help!

if you want to make it string, it is a string value, not a bool, so you must wrap it in double quotes visible='<%: "false" %>' <= mark ""

+1
source

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


All Articles