Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:25:45+00:00 2026-05-31T03:25:45+00:00

I have been working in a .NET Framework 4 project using server tags like

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:25:46+00:00Added an answer on May 31, 2026 at 3:25 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an Entity Framework project with several models set up using .NET 4
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
On a recent project I have been working on in C#/ASP.NET I have some
I am working on a web app using C# and asp.net I have been
I'm working on a asp.net mvc2 app. I have been using jquery to do
I have been working my way through Scott Guthrie's excellent post on ASP.NET MVC
For the last 4 years I have been working as an ASP.NET developer in
I have been working on a web services related project for about the last
I am currently working in a .Net project but from university I have also
I have been working with ASP.NET MVC for a couple of months now and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.