Using Response.Write or <%= in a Label Control

G

Guest

I'm having some issues with using a Response.Write or the shortcut ( <%= ...)
from within a label control. I cannot do this in the code behind, I need to
do it here at runtime. I have a public function (GetMsgs) in my code behind
page that returns a string back to the caller. I want to call this function
from the ASPX page and return the value to a Label control.

I have tried the following options but cannot get it to work:
1)
<asp:Label Runat="server" ID="lblSelectedMessages"
CssClass="span_normal_bold_altcolor" Text='<%= GetMsgs() %>' />

2)
<asp:Label Runat="server" ID="lblSelectedMessages"
CssClass="span_normal_bold_altcolor" Text='<% Response.Write(GetMsgs()) %>' />

3)
<asp:Label Runat="server" ID="lblSelectedMessages"
CssClass="span_normal_bold_altcolor">
<%= GetMsgs() %>
</asp:Label>
 
T

Teemu Keiski

<%= ... %> constructs are evaluated when Page is rendered, so it doesn't
actually make sense to have them like that because you'd be assigning text
to Text property of a Label which should happen before rendering.

What you can do is put it like this:

Text='<%# GetMsgs() %>' />

(databinding-expression) and call Page.DataBind() or
lblSelectedMessages.DataBind() somewhere (you definately can do that in
inline code as well, you just need to have the server-sidescript block then
but is that a good option...)

You can output text on page with <%= %> if you don't have server control
"around it".
 
S

Steve Richter

Teemu said:
<%= ... %> constructs are evaluated when Page is rendered, so it doesn't
actually make sense to have them like that because you'd be assigning text
to Text property of a Label which should happen before rendering.

What you can do is put it like this:

Text='<%# GetMsgs() %>' />

<%# is new to me. what does Microsoft call "<%=" and "<%#"? I dont
know how to search for the concept at MSDN.
(databinding-expression) and call Page.DataBind() or
lblSelectedMessages.DataBind() somewhere (you definately can do that in
inline code as well, you just need to have the server-sidescript block then
but is that a good option...)

You can output text on page with <%= %> if you don't have server control
"around it".

Which I dont understand at all. Does .NET discriminate against server
controls! :)

thanks,

-Steve
 
G

garethdjames

Really in the code behind you just want to call (on the PageLoad)
lblSelectedMessage.Text = GetMsgs()

this way the logic of setting the label is seperated form the page
layout
 

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

Top