Search code examples
asp.netstringpropertieswhitespacecustom-server-controls

Custom Server Control, Text Property Whitespace


I am creating a custom server control with a property that accepts a String as input. What is the correct way to handle whitespace/tabs & newlines? For example,

<prefix:MyControl runat="server"
    Property="This is a long text string which is manually
              wrapped in the VS text editor on multiple
              lines."
>
</prefix:MyControl>

This returns something similar to

"This is a long text string which is manually\r\n           wrapped in the VS text editor on multiple\r\n           lines."

Is there a special attribute I can apply to the propery to help with this? I have checked Metadata Attributes for Custom Server Controls Or do I need to manually strip the linebreaks and extra spacing?


Solution

  • Discovered a Regular Expression solution which finds multiple whitespace characters preceded by a line break and replaces matches with a single space.

    private string property;
    
    [Bindable(true)]
    public string Property {
        get { return property; }
        set {
            property = Regex.Replace(value, @"(\n|\r|\r\n)\s+", " ");
        }
    }