0

So i have something like: static Config class, which is used everywhere in the project, having a bool property let's say Property. So I want to find a way to set this property in the aspx markup.

<asp:Column ..... Visible='<%: Config.Property %>' > 

does not work. I also tried:

'<%# Config.Property %>'
'<%$ Config.Property %>'
'<%= Config.Property %>'
'<% Config.Property %>'

and all of the above without the "'". Is there any way to work it out? I don't want to set it in the code behind and i have reasons not to.

vasil todorov
  • 145
  • 1
  • 11
  • set or get? do you have an error or a n exception – giammin Oct 01 '14 at 16:20
  • normally this `` must work – Aristos Oct 01 '14 at 16:23
  • "Cannot create an object of type 'System.Boolean' from its string representation '<% Config.Property %>' for the 'Visible' property."} – vasil todorov Oct 01 '14 at 16:25
  • 1
    you need to use the `.ToString()` and check the string representation of the value if it's equal to `true' or `false' – MethodMan Oct 01 '14 at 16:29
  • possible duplicate of [Web Forms error message: "This is not scriptlet. Will be output as plain text"](http://stackoverflow.com/questions/18952849/web-forms-error-message-this-is-not-scriptlet-will-be-output-as-plain-text) – giammin Oct 01 '14 at 16:30
  • I tried this: '<%#Config.Property.ToString() != "false" %>' but still throwing ex – vasil todorov Oct 01 '14 at 16:35
  • @giammin - I don't get how that is a duplicate. – Icemanind Oct 01 '14 at 16:41
  • {"Databinding expressions are only supported on objects that have a DataBinding event. Column does not have a DataBinding event."} this is with code:Visible='<%# bool.Parse(FULLNAMESPACE.Config.Property.ToString()) %>'. I also tried with .ToString() == "true". – vasil todorov Oct 01 '14 at 16:44

1 Answers1

1

The reason you are having issues is because the string-value of each property on a server-control is evaluated and parsed to its desired type. So you could set the property to "True" or "true", but not to an expression that needs to be evaluated. In order to get around this, you must use databinding syntax, like this:

<asp:Column ..... Visible="<%# Config.Property %>" >

The problem with using a databinding expression is that you need to execute a DataBind() method on the control itself or on the page itself. Calling it on the page itself can have bad side effects if you decide to later use a binding control somewhere else on the page. So, to do this, we are going to add a "fake meta tag" that executes the DataBind() method, like so:

<asp:Column ..... Visible="<%# Config.Property %>" meta:bind='<% DataBind(); %>' >

You don't have to call it bind. You can call the meta tag anything you wish. This will force the ASP.NET engine to perform a DataBind on this control, and it should evaluate and parse the Config.Property expression.

Icemanind
  • 47,519
  • 50
  • 171
  • 296