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.

8
  • set or get? do you have an error or a n exception Commented Oct 1, 2014 at 16:20
  • normally this <asp:Column ..... Visible="<%#Config.Property %>" > must work Commented Oct 1, 2014 at 16:23
  • "Cannot create an object of type 'System.Boolean' from its string representation '<% Config.Property %>' for the 'Visible' property."} Commented Oct 1, 2014 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' Commented Oct 1, 2014 at 16:29
  • possible duplicate of Web Forms error message: "This is not scriptlet. Will be output as plain text" Commented Oct 1, 2014 at 16:30

1 Answer 1

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.

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

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.