I get this error message:
Cannot create an object of type 'System.Boolean' from its string representation <%: false %> for the 'Visible' property.
When I try to run this code within my ASP.net website:
<a runat="server" visible='<%: false %>' href="~/" >Home</a>
Is there a syntax error? false
should be replaceable by any method result same with:
<asp:Panel runat="server" Visible='<%: GetTrueOrFalse() %>'>Home</a>
ASP.NET (and ASP too) uses the delimiters <% and %> to enclose script commands. Within the delimiters, you can include any command that is valid for the scripting language you are using.
Inline expression is a piece of Groovy code in essence, which can return the corresponding real data source or table name according to the computation method of sharding keys.
The runat="server" tag in ASP.NET allows the ability to convert/treat most any HTML element as a server-side control that you can manipulate via code at generation time. Some controls have explicit implementations, others simply revert to a generic control implementation.
Suppose you have a method that returns bool
value like this:
public bool IsVisible()
{
if (some_condition) // example condition test
{
return true;
}
else
{
return false;
}
}
You need to use binding like this:
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();
}
NB: This trick apply for either methods or properties which returns bool
.
Update 1:
Since a
tag doesn't set any 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>
Reference:
Is code rendering block <%=%> useful for boolean type?
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