Tech Off Thread

10 posts

Quick Asp.Net 2.0 question

Back to Forum: Tech Off
  • User profile image
    sgomez

    Why isn't this label displaying "Hello World!" ???

    <asp:Label ID="Label1" runat="server" Text='<%# "Hello " + "World!"%>'></asp:Label>

    ...and why isn't this one displaying the current date?

    <asp:Label ID="Label2" runat="server" Text="<%# System.DateTime.Now.ToShortDateString() %>"></asp:Label>

    Anybody?

    TIA.
    Regards,
    .seb

  • User profile image
    W3bbo

    Memorise these:

    <% is the ASP.NET Page Render Function initial delimiter
    <%# is the databinding ASP.NET Page Render Function initial delimiter.
    <%= is the Response.Write ASP.NET Page Render Function initial delimiter
    <%-- is the Comment ASP.NET Page Render Function initial delimiter

    In your case, swap <%# for <%=

  • User profile image
    sgomez

    Ok, I don't know why but writing this in the Page_Load method, it works...

    Page.DataBind();

    It would be nice if someone could explain though...

    BTW: with <%#

  • User profile image
    sgomez

    Great tip, thanks...

    But still not working... Sad

    this:
    <asp:Label ID="Label1" runat="server" Text='<%= "Hello " + "World!"%>'></asp:Label>

    generates this:
    <span id="Label1"><%= "Hello " + "World!"%></span>

  • User profile image
    Sven Groot

    My guess would be that because "Hello World" is a constant expression it is evaluated at page compile time so the call to Page.DataBind isn't necessary.

  • User profile image
    mawcc

    <% = ... %> is shorthand for Response.Write, but Response.Write doesn't return a value, so there's nothing to be assigned to the Label's Text property.
    <%# ... %> is a databinding expression which is only evaluated during databinding. Therefore it works when calling Page.DataBind() explicitly.
    In your case I'd recommend setting the Label's Text in code, not in markup. I'm not sure about the overhead of Page.DataBind()

  • User profile image
    W3bbo

    Sven Groot wrote:
    My guess would be that because "Hello World" is a constant expression it is evaluated at page compile time so the call to Page.DataBind isn't necessary.


    The <%=Foo %> statement didn't work because it's shorthand for <% Response.Write( Foo ) %>, obviously you can't put that in a property setter.

  • User profile image
    Aptly Goodie

    sgomez wrote:
    

    Great tip, thanks...

    But still not working...

    this:
    <asp:Label ID="Label1" runat="server" Text='<%= "Hello " + "World!"%>'></asp:Label>

    generates this:
    <span id="Label1"><%= "Hello " + "World!"%></span>


    Text property expects a string, and if the tag is not <%# %>, it will consider that a literal string; <%= Hello + World %>  in this case. If you want evaluation on the front page, you'd need <%# %>, but that will require you to raise DataBind event.

    One nice thing about <%# %> is you can put them anywhere... such as

    <asp:Repeater ... DataSource="<%# SomeObject.SomeCollection %>" ...>

    So you can actually assign data to controls without doing it in code behind. I prefer this method because it lets me see where I'm getting the data from.

  • User profile image
    Aptly Goodie

    Dupe

  • User profile image
    typemismatch

    I thought you were not allowed to use any <% %> inside a server side tag? You can use them anywhere else just not there. Since it is a server control, you your code behind to populate it.

    -c

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.