x字符后的asp网标签断行

时间:2017-01-30 08:12:13

标签: c# asp.net label

我有这个asp网络标签从文本框中读取输入,问题是如果它有多个字符,它会覆盖页面,如下所示:

enter image description here

我希望文本尽可能多地突破到新行,是否可能?

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

4 个答案:

答案 0 :(得分:4)

你可以使用CSS word-wrap: break-word;来解决这个问题。

示例:

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

答案 1 :(得分:2)

使用以下属性将Label更改为TextBox

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)

编辑以添加示例: 在你的YourAspx.aspx中测试这两个例子:

<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>

答案 2 :(得分:0)

作为替代方案,您可以使用一个小帮助方法在一些指定数量的字符处中断文本,并将完整的文本添加到ToolTip属性中:

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) + "…";
}

在我的示例中,方法(LimitText)接受一个对象并检查该对象是否实际上是一个字符串。这使得在数据绑定表达式中使用该方法变得容易......

答案 3 :(得分:0)

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