Hello you all,

I have used label in repeater to display news content.see the code below.

<asp:Label ID="lblNewsContent" Width="1000px" Height="500px" runat="server" Text='<%#Eval("News_Content")%>' ReadOnly="true" BorderStyle="None" BorderColor="#EFF3FB" BackColor="#EFF3FB" TextMode="MultiLine"></asp:Label >

Now problem is that how can i give new line in label after certain amount of characters?means i have say 500 characters record i want break line or give new line after 50 characters.how can i achieve this?i m fetching news content from DB.

Give me some guidence.
Thank you all.

Dani AI

Generated

Quick follow-up for (and building on 's TextBox suggestion): if you want a hard break every N characters (not just visual wrapping), do that by transforming the string on the server or client — CSS will only wrap visually and cannot reliably insert an actual newline at an exact character count. Below are two practical, safe approaches you can apply inside the Repeater.

Server-side (safe for HTML output)

  • Split the plain text into chunks, HtmlEncode each chunk, and join them with <br />. Encoding each chunk prevents XSS while allowing the inserted <br /> tags to render.
  • Use a Literal (or set the control that emits HTML) so the <br /> is not double-encoded.

Example C# helper:

protected string WrapSafeHtml(string text, int maxChars)
{
    if (string.IsNullOrEmpty(text)) return string.Empty;
    var sb = new System.Text.StringBuilder();
    int pos = 0;
    while (pos < text.Length)
    {
        int len = Math.Min(maxChars, text.Length - pos);
        string chunk = text.Substring(pos, len);
        sb.Append(System.Web.HttpUtility.HtmlEncode(chunk));
        pos += len;
        if (pos < text.Length) sb.Append("<br />");
    }
    return sb.ToString();
}

Usage in the Repeater (ASPX):

<asp:Literal ID="litNewsContent" runat="server"
    Text='<%# WrapSafeHtml(Eval("News_Content").ToString(), 100) %>' />

If you prefer not to break words, find the last whitespace in each chunk and break there (fall back to forced split if none found).

TextBox (textarea) option — no HTML

  • If you must use a TextBox with TextMode="MultiLine", insert Environment.NewLine instead of <br />. To avoid scrollbars, calculate Rows from content length (e.g., Rows = Math.Max(1, (int)Math.Ceiling(content.Length / (double)charsPerLine))) or use a small autosize script and CSS (overflow:hidden; resize:none;) so the textarea grows to fit content.

Notes and cautions

  • Always HtmlEncode user/content text before outputting HTML fragments.
  • For precise visual “N characters per line” without changing data, using a monospace font + CSS width in ch (e.g., width:50ch) can approximate fixed-character wrapping, but it’s visual only and depends on font.
  • Choose server-side splitting for predictable, cross-browser results.

Recommended Answers

All 6 Replies

Hello you all,

I have used label in repeater to display news content.see the code below.

<asp:Label ID="lblNewsContent" Width="1000px" Height="500px" runat="server" Text='<%#Eval("News_Content")%>' ReadOnly="true" BorderStyle="None" BorderColor="#EFF3FB" BackColor="#EFF3FB" TextMode="MultiLine"></asp:Label >

Now problem is that how can i give new line in label after certain amount of characters?means i have say 500 characters record i want break line or give new line after 50 characters.how can i achieve this?i m fetching news content from DB.

Give me some guidence.
Thank you all.

hey are you sure Label has TextMode property ? I believe Lable doesn't have Multiple line functionality.
Instead you can use Text Box set it's Wrap and TextMode property and it will work for you.

commented: pl reply me soon +1

oh sorry first i have used textbox but later used label so forgot to remove textmode.but my question is same?

<asp:Label ID="lblNewsContent" Width="1000px" Height="500px" runat="server" Text='<%#Eval("News_Content")%>' ReadOnly="true" BorderStyle="None" BorderColor="#EFF3FB" BackColor="#EFF3FB" >

please reply me.

oh sorry first i have used textbox but later used label so forgot to remove textmode.but my question is same?

<asp:Label ID="lblNewsContent" Width="1000px" Height="500px" runat="server" Text='<%#Eval("News_Content")%>' ReadOnly="true" BorderStyle="None" BorderColor="#EFF3FB" BackColor="#EFF3FB" >

please reply me.

no dude- you can't do such thing with Label. Instead i say use Text Box and with the help of style set Text Box such that it will look like label. :-)

i have used textbox with textmode =multiline earlier but i got scroll at right hand side which is not good looking as i have to only show the data.
ok if i use textbox according to you then how can i achieve solution for the same problem without scrolling in textbox?

pl reply

i have used textbox with textmode =multiline earlier but i got scroll at right hand side which is not good looking as i have to only show the data.
ok if i use textbox according to you then how can i achieve solution for the same problem without scrolling in textbox?

pl reply

Sorry for late replay...
you need to set either overflow:hidden; or overflow:auto; in style and then use this style in your text box.

it will work...

hi rohand,

i had applied ur css code and working properly, thanks dude.

now i want to give new line after fixed amount of characters(say after every 100characters) in that textbox as i had told u that i m displaying news content n large amount.

pl guide me for that.thanks waiting for ur reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.