Reputation: 2608
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>
Upvotes: 4
Views: 735
Reputation:
Using this <%: ... %>
syntax will raise your above parser error. The correct syntax for data binding server-control values is <%# ... %>
. Inline expression's more detail is here.
And you can done it in another way:
<% if(GetTrueOrFalse()) { %>
<a ID="alink" runat="server" href="~/" >Home</a>
//... other code
<% } %>
Upvotes: 1
Reputation: 35544
You can also declare a public boolean and use that. You will need to use DataBind()
if the link is outside a GridView/Repetater etc.
public bool isVisible = true;
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
Now you can use that on the aspx.
<a runat="server" visible='<%# isVisible %>' href="~/">Home</a>
However you can also use a ternary operator based on a different variable or class value within 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
, even though it is not a Boolean.
<a runat="server" visible='<%# myValue > 10 ? true : false %>' href="~/">Home</a>
//or
<a runat="server" visible='<%# myBook.category == "Fantasy" ? true : false %>' href="~/">Home</a>
Upvotes: 1
Reputation:
Learn this technique you're gonna use it a lot. Server side controls or containers can easily be manipulated server side. How? well you did the first part right you game it runat="server"
now all you have to do is to give it an id so it looks something like this let's name the id MyLink
<a runat="server" id="MyLink" href="~/" >Home</a>
-Now you noticed that I've removed the attribute Visible. yes because now we're gonna take full control of it server side wise. Let's assume you wanna start off the page first time with a hidden , well that's easy: in your pageload event we'll use a good technique to determine that the code we'll write will only run once at the very 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 simply make your control visible again whenever wherever you want simply
MyLink.Visible = true;
and done. lemme know if you need any more help!
if you wanna do it inline it's a string value not bool so you should wrap it in double quotes visible='<%: "false" %>'
<= notice the ""
Upvotes: 0
Reputation: 24957
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?
Upvotes: 2