views:

585

answers:

3

Hi all,

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.
+4  A: 

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.

eglasius
Thank you for your reply, I have edited my question accordingly. :)
Jon
@Jon I meant the comment on using = on your inline version when you wanted to output the text. You can't use that with a property of a server control, in that case you can use your original one and call DataBind on your panel (or on the control that contains it).
eglasius
OMG, your spot on. The lightbulb came on as soon as I read the word "DataBind"... I knew I was missing something really stupid. I would love to put it down to 1am coding but I think I was just being an idiot. Thanks so much. I have learnt a bit today, so thank you to all that helped.
Jon
+2  A: 

<%# 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.

Canavar
ah ok, that makes sense then. When using it in the Visible I am saying write a text value into a boolean field and its getting upset...
Jon
A: 

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

Marko
I believe it is the opposite, that the visible property wants a boolean value. I tried <%# String.Format("{0}", true) %> so it would just put a string of true, but I get "Cannot convert type string to bool" compiler error
Jon
Settings properties that don't use string as type requires the <# %> syntax that the the runtime will automaticly match the types when databinding.<%= %> is a shorthand for Response.Write and can never be used to set property-values to objects, since its just writing out the string as-is at the corresponding place in the OutputStream.
BurningIce