Why can I not use script blocks within ASP:Label controls?

P

Peter Rilling

Below is some code that I do not know why it does not work. Okay, as you
can see it is simple HTML with two ASP:Label controls. Each label has some
code that basically gets a string that will be written to the browser.
ConditionalText.Eval is a method from my own library. As you can see, I
have written some static text before and after the first Eval call. If I
open this page, I get no content in the Label, even the static content. I
just get "<span id="lblScenario"
style="font-size:17px;font-weight:bold;"></span>". Now, suppose that I
remove first Eval call and open the page again, I get "<span
id="lblScenario"
style="font-size:17px;font-weight:bold;">sdfsadffdsfsda</span>", which this
time contains the static content as expected. I have even removed my
reference to Eval and just placed static content in the <% %> block and the
same thing happens. Any ideas at what the server is doing?


<form id="Form1" method="post" runat="server">
aaaaaaaa
<table>
<TR>
<TD vAlign="top">
<asp:Label id="lblScenario" Font-Size="17px" Font-Bold="True"
Runat="server">sdfsadf<%= ConditionalText.Eval("ADMIN_MARKET_HEADER")
%>fdsfsda</asp:Label>
<br>
<br>
<asp:Label id="lblScenarioExpl" Font-Bold="False" Runat="server">
<%= ConditionalText.Eval("ADMIN_MARKET_INSTRUCTION") %>
</asp:Label>
</TD>
</TR>
</table>
bbbbbbbb
</form>
 
B

Brock Allen

It doesn't work because ASP.NET is taking the <asp:Label x=y z=w> and creating
a method to do the instantiation and assignment of those properties on the
Label (BTW, the text inside the tags is just a Text property assignment).
This is done at creation time of the ASPX Page object. The <%%> syntax is
Rendering syntax which is done at a much later time in the page lifecycle
andn thus conflicts with the initialization code mentioned above. The idea
is that if you've gone to all the work to make this a server control, then
you'd do those sorts of assignments (dynamic or otherwide) in your code for
your page, like Page_Load. In short, the ASP.NET parser and object model
is at odds with their attempt to provide backwards classic ASP style inline
rendering syntax.
 
N

news.microsoft.com

I'm not sure to undersand what do you want to do,
but in ASP.NET sou shouldn't use <%= %> to render some dynamic content like
ASP developers used to do before ASP.NET

It is better to write the code like :
lblScenario.Text = "mystring"+ConditionalText.Eval("ADMIN_MARKET_HEADER")

and searching why or why not your result is strange is not the good question
I mean...

Sincerely,
Daniel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top