Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to bind multiple lines of text from backend in repeater..
i have used this..
Text='<%#Limit(Eval("feedback_details"),40)%>'


aspx.cs file...
C#
protected string Limit(object Desc, int length)
    {
    StringBuilder strDesc = new StringBuilder();
    strDesc.Insert(0, Desc.ToString());

    if (strDesc.Length > length)
    {
        return strDesc.ToString().Substring(0, length) + "...";
    }
    else return strDesc.ToString();
    }


it displays the text in same line like this
this is a sample.......

i want to implement like this
this is a sample paragraph.
this is a sample paragraph.
this is a....


kindly help me..
Posted

1 solution

I have considered 'Desc' as string input

C#
string Desc = "Hell\r\noWorld\r\nHow\r\nAre\r\n";
        protected string Limit(int length)
        {
            string strDesc = Convert.ToString(Desc);
            string[] lines = Regex.Split(strDesc, Environment.NewLine);
            string s = "";
            int TotalLength = 0;
            int cnt = 0;
            //Finding total length of all strings
            for (int i = 0; i < lines.Length; i++)
            {
                TotalLength += lines[i].Length;
            }
            if (TotalLength > length)
            {
                //Making of String with "<br/>
                s = lines[0];
                length = length - lines[0].Length;
                cnt = 1;
                while (length > 0)
                {
                    if (lines[cnt].Length < length)
                    {
                        s += "<br/>" + lines[cnt];
                        length = length - lines[cnt].Length;
                    }
                    else
                    {
                        s += "<br/>" + lines[cnt].Substring(0, length);
                        length = 0;
                    }
                    cnt++;
                }
                return s + "...";
            }
            else
            {
                return strDesc.Replace(Environment.NewLine, "<br/>");
            }

        }
 
Share this answer
 
v6
Comments
priya dharshan 18-Apr-14 8:26am    
This will bind the entire text from backend and displays it in multiple line...
i need to limit the full text in second or third line..
how can i able to do that..

thanks in advance..
DotNet WeblineIndia 19-Apr-14 1:44am    
If you are using Label for binding text then your code will work,but you need to check for the newline character in the string that comes from the database because Label control will not accept "\r\n" so you need to replace it with "<br/>"
And if you are using TextBox then set the Textmode to multiline.
Hope this will help you
priya dharshan 19-Apr-14 2:53am    
I have used label only..
i need to implement like this

this is a sample paragraph.
this is a sample paragraph.
this is........


i have tried your coding but it displays all the text from backend in multiple lines..
i need to limit the text in second or third line.
eg: like in google search..

for that what we can do sir...
DotNet WeblineIndia 19-Apr-14 5:15am    
I have updated the solution.Pls check it
Hope this will help you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900