4

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>
1
  • see my updated answer and let's wrap it up! Commented Sep 14, 2017 at 13:57

4 Answers 4

2

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?

Sign up to request clarification or add additional context in comments.

3 Comments

What thing doesn't disappear then? Please provide further details.
the a element doesn't dsappear when I write public bool IsVisible() { return false; } and add the method call into visible=""
Seems runat="server" may cause the problem. Try remove the runat attribute to set it as plain HTML tag.
1
+100

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>

2 Comments

There is no DataBind(); method to call
Yes there is. A Page Control has this method (Page.DataBind() or this.DataBind() are the same as DataBind())
1

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
<% } %>

Comments

0

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 ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.