Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

asp.net - Set Visible property with server tag <%= %> in Framework 3.5

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

Thanks in advance,

Tom

[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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...