ransems
ransems

Reputation: 671

Add text to an asp.net label before the bound label text

I have seen it done before but i cannot remember where (vs2012 even):

<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

Some tag I can add to the label (or textbox?)

So that when I bind "Hello World!" to the label, I can have the label output: "Welcome, Hello World!"

My label would look like:

<asp:Label ID="lblMessage" runat="server" Text="" PrependedText="Welcome, "></asp:Label>

Upvotes: 0

Views: 960

Answers (2)

mason
mason

Reputation: 32694

Several ways to go about this...

Welcome, <asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

or

<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>

lblMessage.Text="Welcome, Hello World!";

or

public class MySpecialLabel: Label
{
public string PrependedText {get;set;}

//logic here to handle combining the strings when Text is set.
}

or

<div class="WelcomeSection">
  Welcome, <asp:Label runat="server" id="lblMessage">
</div>

Upvotes: 1

Ivan Doroshenko
Ivan Doroshenko

Reputation: 942

You can write your own control derived from Label or just put another before with text from resources or hardcoded:

<asp:Label ID="lblPrepend" runat="server" Text="Welcome, " />
<asp:Label ID="lblMessage" runat="server" Text="" />

Upvotes: 1

Related Questions