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

Discussion in 'ASP .Net' started by Peter Rilling, Apr 5, 2005.

  1. 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
    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">
    <TD vAlign="top">
    <asp:Label id="lblScenario" Font-Size="17px" Font-Bold="True"
    Runat="server">sdfsadf<%= ConditionalText.Eval("ADMIN_MARKET_HEADER")
    <asp:Label id="lblScenarioExpl" Font-Bold="False" Runat="server">
    <%= ConditionalText.Eval("ADMIN_MARKET_INSTRUCTION") %>
    Peter Rilling, Apr 5, 2005
    1. Advertisements

  2. Peter Rilling

    Brock Allen Guest

    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.
    Brock Allen, Apr 5, 2005
    1. Advertisements

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

    news.microsoft.com, Apr 5, 2005
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.