Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind Multiple lines of text to a label

Tags:

c#

.net

asp.net

I need to bind large text data to a label dynamically. I will get some large text data from a datasource and have to bind it to lable. So how to display multiple lines of text in a label.

like image 768
karthik k Avatar asked Jul 14 '11 11:07

karthik k


2 Answers

Easiest would be just

string value = "one\r\ntwo\r\nthree";

label.Text = value.Replace(Environment.NewLine, "<br/>");

But if you have a list of strings you can try a repeater approach

<asp:Label ID="label" runat="server">
    <asp:Repeater ID="repeater" runat="server">
        <ItemTemplate>
            <%# Container.DataItem %> <br />
        </ItemTemplate>
    </asp:Repeater>
</asp:Label>

And the code

List<string> listOfStrings = new List<string>()
    {
        "One", "Two", "Three"
    };

repeater.DataSource = listOfStrings;
repeater.DataBind();
like image 122
Lourens Avatar answered Sep 19 '22 04:09

Lourens


In your case use of literal is better because you write html in literal from your code like this:

Literal1.Text = "Hello<br/>"+"How are you?"....

Or you can use Text box as set property TextMode=Multiline and readonly=True to behave as Label

like image 40
SMK Avatar answered Sep 22 '22 04:09

SMK