Bind Multiple lines of text to a label
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
You could just try this
Label.AutoSize = false
I dont know in what "units" you will measure the text length. but adding <br />
to a ur_string
will start a new line.
EDITED
this is not valid for all asp.net
controls. just asp.net label
renders as html span
, so for label <br />
will have a desired effect.
精彩评论