Snap multiple lines of text to label

I need to associate large text data with a shortcut dynamically. I will get some big text data from the data source and have to bind it to lable. So how to display multiple lines of text in a shortcut.

+6
source share
4 answers

The simplest would be simple

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 the repeater approach

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

And code

 List<string> listOfStrings = new List<string>() { "One", "Two", "Three" }; repeater.DataSource = listOfStrings; repeater.DataBind(); 
+6
source

In your case, using a literal is better because you are writing html in the literal text from your code as follows:

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

Or you can use the text field as the specified property TextMode = Multiline and readonly = True to behave like a label

+3
source

You can just try this

 Label.AutoSize = false 
+1
source

I do not know in what "units" you will measure the length of the text. but adding <br /> to ur_string will start a new line.

EDITED

this is not valid for all asp.net controls. just the asp.net label displayed as an html span , so for the <br /> tag it will have the desired effect.

0
source

Source: https://habr.com/ru/post/892742/


All Articles