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.
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With