简体   繁体   中英

asp net label break line after x characters

I have this asp net label that reads input from a textbox, the problem is that if it has to many characters, it overrides the page like this:

在此处输入图片说明

I want that the text breaks to a new line as soon as its to many characters, is it possible?

<asp:Label runat="server" ID="lblNotes" />

You can use CSS word-wrap: break-word; which will fix this.

Example:

 #par1 { word-wrap: break-word; } 
 <p id="par1"> Very long texttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt </p> 

Change your Label to a TextBox , using the following properties:

Wrap: True

Rows: Something greater than 0

ReadOnly: (if you want to simulate a label, set it to true)

TextMode: Multiline

BorderStyle : None (simulate label)

BorderWidth : 0 (simulate label)

Edit to add example: Test in your YourAspx.aspx this two examples:

<asp:Label ID="Label1" runat="server" Text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet."></asp:Label>
<asp:TextBox runat="server" Wrap="true" Rows="15" ReadOnly="true" TextMode="MultiLine" BorderStyle="None" BorderWidth="0" Text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet."></asp:TextBox>

As an alternative, you could have a small helper method that breaks the text at some specified amount of characters, and add the unbroken text to the ToolTip property:

protected void Page_Load(object sender, EventArgs e)
{
    string theText = "some_very_long_text_here_that_is_too_long_to_fit_nicely_in_the_ui";
    test.Text = LimitText(theText, 15);
    test.ToolTip = theText;
}
public string LimitText(object input, int nrChars)
{
    string inputAsString = input as string;
    if (inputAsString == null)
        return "";
    if (inputAsString.Length <= nrChars)
        return inputAsString;
    return inputAsString.Substring(0, nrChars - 1) + "…";
}

In my sample, the method ( LimitText ) takes an object and checks if the object is in fact a string. This makes it easy to use the method in data binding expressions...

 lbl.Text = "hello"+"<br/>"+"hello1"+"<br/>"+"hello2";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM