asp.net - Set Visible property with server tag <%= %> in Framework 3.5 -
i have been working in .net framework 4 project using server tags <%=whatever %> set visibility of runat="server" controls, following:
<div id="myid" runat="server" visible="<%=myvisiblepropertyoncodebehind %>" > content </div>
this works on framework 4, trying use on framework 3.5 project doesn't seems work. framework 4 feature? there coolest (and .aspx side) alternative setting visibility codebehind? i'm using ugly:
miid.visible = myvisiblepropertyoncodebehind
thanks in advance,
tom
[edited] solution:
thanks comments makes me understand problem , solution!
it fault in more 1 thing.
in vs2010 project using <%# instead of <%=
also, didn’t notice in vs2010 project using pages inherited not “page”, custompage class, making binding automatically, without me noticing it, , makes me think framework 4.0 feature.
as told here, if have following markup:
<div id="myid" runat="server" visible="<%# myvisiblepropertyoncodebehind %>" > content </div>
you can make work, adding following codebehind:
public bool myvisiblepropertyoncodebehind = true; protected void page_load(object sender, eventargs e) { databind(); // or if want 1 control, myid.databind(); }
as read, databind() can reduce performance of application. have idea of how much? understood “professional” technique used on big projects, or think should avoided?
i love way makes markup readable , easy understand in single view, wouldn’t guilty of slow code because that.
the code posted not valid syntax server tags in asp.net 2.0 or asp.net 4.0 runtimes. in either version, trying set visible property using <%= ... %>
in server tag should result in parser error:
parser error message: cannot create object of type 'system.boolean' string representation '<%=myvisiblepropertyoncodebehind%>' 'visible' property.
you have 2 options other setting visible
property in codebehind or <script runat="server">
tag. first use databinding on visible
property. you'll need call databind()
method on either myid
or 1 of parent controls value bound.
<div id="myid" runat="server" visible="<%# myvisiblepropertyoncodebehind %>" > content </div>
the other option write code follows:
<% if(myvisiblepropertyoncodebehind) { %> <div id="myid" runat="server"> content </div> <% } %>
the disadvantage of approach won't able programmatically add controls page or control contains code blocks. if try should error:
the controls collection cannot modified because control contains code blocks (i.e. <% ... %>)
all being said, think setting property way doing way go.
Comments
Post a Comment