60

I have been working in a .NET Framework 4 project using server tags like <%=whatever %> to set the visibility of runat="server" controls, like the following:

  <div id="MyId" runat="server" visible="<%=MyVisiblePropertyOnCodeBehind %>" >
    Content
  </div>

This works on framework 4, but now trying to use this on a Framework 3.5 project it doesn't seems to work. Is this a Framework 4 only feature? Is there a coolest (and .aspx side) alternative to setting the visibility from codebehind? I'm using the ugly:

    MiId.Visible = MyVisiblePropertyOnCodeBehind

[EDITED] SOLUTION:

Thanks for your comments that makes me understand my problem and the solution!

It was my fault in more than one thing.

In the VS2010 project we were using <%# instead of <%=

Also, I didn’t notice that in the VS2010 project we were using pages inherited not from “Page”, but from a CustomPage class, that was making the binding automatically, without me noticing it, and that makes me think that was a Framework 4.0 only feature.

As you told here, if you have the following markup:

  <div id="MyId" runat="server" visible="<%# MyVisiblePropertyOnCodeBehind %>" >
    Content
  </div>

you can make it work, adding the following to the codebehind:

    public bool  MyVisiblePropertyOnCodeBehind = true;
    protected void Page_Load(object sender, EventArgs e) {
        DataBind();
        // Or if you want only for one control, MyId.DataBind();             
    }

As I read, this DataBind() can reduce performance of the application. Do you have idea of how much? Could this be understood as a “professional” technique to be used on big projects, or do you think it should be avoided?

I love the way it makes markup readable and easy to understand in a single view, but I wouldn’t like to be guilty of slow code because that.

2
  • What is the signature of your 'MyVisiblePropertyOnCodeBehind' property? Mar 7, 2012 at 7:01
  • I actually don't think it's ugly to set it in code behind. Set visible="false" as default in the code front if you want a reminder that the control isn't necessarily visible. Sep 19, 2017 at 14:20

5 Answers 5

82

The code you posted is not valid syntax for server tags in the ASP.NET 2.0 or ASP.NET 4.0 runtimes. In either version, trying to set the visible property using <%= ... %> in a server tag should result in a parser error:

Parser Error Message: Cannot create an object of type 'System.Boolean' from its string representation '<%=MyVisiblePropertyOnCodeBehind%>' for the 'Visible' property.

You have two options other than just setting the Visible property in the codebehind or a <script runat="server"> tag. The first is to use a databinding on the Visible property. You'll need to call the DataBind() method on either MyId or one of its parent controls for the value to be bound.

<div id="MyId" runat="server" visible="<%# MyVisiblePropertyOnCodeBehind %>" >
    Content
</div>

The other option is to write the code as follows:

<% if(MyVisiblePropertyOnCodeBehind) { %>
<div id="MyId" runat="server">
    Content
</div>
<% } %>

The disadvantage of this approach is that you won't be able to programmatically add controls to the page or control that contains the code blocks. If you try to you should get an error:

The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)

All that being said, I think just setting the property the way you are doing it now is the way to go.

3
  • I finally understood my problem thanks to this, and the links in "arunes" message. Thanks. (sorry but i'm new and can't vote).
    – tomasofen
    Mar 11, 2012 at 5:41
  • 1
    @rsbarro I have found another oddity using an <% if block, such that validation errors are thrown on postback. If the block contains a RequiredFieldValidator which shouldn't be rendered, the postback will still use this as if it exists and cause a validation failure. I found that I still had to use a boolean expression to set Enabled='<%# false %>' and call someControlId.DataBind(); in the code behind Page_Load to get the validator to be disabled on postback.
    – AaronLS
    Jan 9, 2013 at 18:01
  • is it possible to call this.DataBind(); in code behind of usercontrol, and make it ok for listviews and datagrids? like in OnInit method?
    – Roar
    Jul 21, 2014 at 13:45
3

As for ASP.NET aspx page's inline expression. <% %> can only be used at aspx page or user control's top document level, but can not be embeded in server control's tag attribute (such as <asp:Button... Text =<% %> ..>). As you've found you can create custom expression builder in ASP.NET 2.0 to add your inline expression.

BTW, another means for supplying values to server control properties in aspx inline tempalte is using <%# %> databinding expression. This is built-in supported. The only different from other inline expression is that method on the target control or its Container control.

Steven Cheng

Microsoft MSDN Online Support Lead

Full post here: http://www.aspnet-answers.com/microsoft/ASP-NET/29389067/dynamically-set-a-control-property.aspx

And workaround here: ASP.net Inline Expression Issue

0
2

Here is another approach which maintains the simplicity of the code from your original question. Here you would have to remove the runat="server" from the div tag and use css "display:none" instead of the "Visible" property. The downside to this approach is that the tag still gets sent to the browser even if it is not visible and the visibility is handled on the client side.

<div style='<%=MyVisiblePropertyOnCodeBehind ? "" : "display: none" %>' >
    Content
</div>
2

Just set a variable to true/false on your pageLoad event like this

private bool IsEditMode {get; set;}      

protected bool IsVisible 
{
    get { retun IsEditMode ;}
    set { IsEditMode =value;}
}  

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // based on some condition set this to true or false 
        isEditMode=true;
    }
}   

Then in your control properties inside aspx page, set their visibility via a property like

Visible="<%# !IsEditMode %>" 
2
  • Sorry, but this i thing this is not related with the question, and the code is confusing, with different property names :(
    – tomasofen
    Mar 11, 2012 at 15:09
  • @tomasofen I haved updated my answer with proper naming.
    – Zo Has
    Aug 28, 2014 at 10:19
0

Another approach is ti give an ID to the control and set the visible in code.

protected void Page_Load(object sender, EventArgs e){
   MyId.Visible = MyVisiblePropertyOnCodeBehind;
} 

<div id="MyId" runat="server">Content</div>

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.