Monday, April 18, 2011

How to insert "new line" into "Multiple lines of text" programmatically

When insert some text into "Note" field programmatically, if there are more than one line, by default, all "line break" will be removed automatically.  The new line character " " is still there, but doesn't take effect in web form.

That's why we need to run the function below to formatting the string before assigning it to a "Note" field.

        public static string FormattingForNote(string strBody)
        {
            string strReturn = string.Empty;

            strReturn = strBody.Trim();
            if (strReturn.IndexOf(Environment.NewLine) > 0)
            {
                strReturn = strReturn.Replace(Environment.NewLine, @"</div><div>");
                strReturn = @"<div>" + strReturn + @"</div>";
            }

            return strReturn;
        }

Update (20/04/2011): the full version source code is available at CodePlex as workflow activity enableNewLineForRichText.wsp

1 comment:

  1. Thanks Eric.
    It works for me.
    Thanks to share this with us.

    -Kaustubh

    ReplyDelete