Jon
Jon

Reputation: 15200

asp.net inline code <%# MyboolVal %>

I must be missing something stupid here but I can not see it. My work uses inline code on their sites, for example:

<panel runat="server" id="myid" visible='<%# MyboolVal %>'>
    some stuff
</panel>

That seems to work great for them, the panel will display when their condition is meet.

I am trying to use a similar approach on a site of mine at home (its late friday evening so asking my boss is not the best idea at this point). I can not get it to output anything at all. I have tried it in the visible field which didn't work, so I thought I would just get it to write something to the screen:

<p>some text <%# String.Format("meeee {0}", Mybool) %></p>

But I do not get any output from the inline code. the "some text" appears but no "meeee" or the bool value.

I am doing this inside a user control, at this moment but do not imagine that would be the cause.

any ideas please?

Thanks

EDIT....

OK so thanks to Freddy Rios for the reply I can get the text to appear but when I try that in:

Visible='<%= mybool %>'

I get the compilation error of:

Cannot create an object of type System.boolean from its string representation for the visible property.

I am confused as to what exactly is happening. There must be part of the process under the bonnet I don't get.

EDIT 2:

I get the error on line 123:

<fieldset class="myclass" id="projectarea" runat="server" visible='<%= ShowProjectSearchArea %>'>

ShowProjectSearchArea is my bool value, set to false.

If I double click the error in the Error List window I get the following in a popup, which I have never seen before:

  Cannot open file '%1'. It might not be in the solution.

Upvotes: 2

Views: 16338

Answers (3)

Canavar
Canavar

Reputation: 48088

<%# is databinding tag which is used to set values to server side controls, especially databound controls.

<%= is shorthand of Response.Write(), it writes the value to the output. So we use it with static html elements.

Upvotes: 14

Marko
Marko

Reputation: 1924

I think that problem is because visible property expect value of type string and you are trying to set it with bool.try to cast your value to string

Cheers

Upvotes: 0

eglasius
eglasius

Reputation: 36035

Try using = instead of # in your version:

<p>some text <%= String.Format("meeee {0}", Mybool) %></p>

The # is for databinding, so in the original code there must be a call to DataBind somewhere.

Upvotes: 10

Related Questions