【问题标题】:Custom Server Control, Text Property Whitespace自定义服务器控件,文本属性空白
【发布时间】:2011-09-13 19:58:34
【问题描述】:

我正在创建一个自定义服务器控件,其属性接受字符串作为输入。处理空格/制表符和换行符的正确方法是什么?例如,

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

这会返回类似于

的内容

"这是一个长文本字符串 手动\r\n 包裹在 VS 文本中 多个编辑器\r\n 行。”

是否有一个特殊的属性可以应用于属性来帮助解决这个问题?我已经检查了Metadata Attributes for Custom Server Controls 还是我需要手动去除换行符和额外的间距?

【问题讨论】:

    标签: asp.net string properties whitespace custom-server-controls


    【解决方案1】:

    发现了一种正则表达式解决方案,该解决方案可查找前面有换行符的多个空白字符并将匹配项替换为单个空格。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-09
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 2015-06-03
      • 2013-04-13
      相关资源
      最近更新 更多