首页 文章

Multiline TextBox中的vbCrLf仅在调用.Trim()时显示

提问于
浏览
3

我有一个ASP TextBox,TextMode设置为MultiLine . 当用户试图将换行符放入文本时,我遇到了保留vbCrLf字符的问题 . 当按下页面上的按钮时,我从控件中获取文本,使用String.Trim修剪它,并将该值赋给对象的String属性(该属性又将其赋值给私有内部String变量)在对象上) . 然后,该对象从私有内部变量获取值,并使用存储过程调用将其抛出到数据库中(它放入的SP参数是nvarchar(4000)) .

ASPX页面:

<asp:UpdatePanel ID="UpdatePanel2" runat="server" RenderMode="Inline" UpdateMode="Conditional"
                ChildrenAsTriggers="true">
  <ContentTemplate>
    <!-- some other controls and things -->
    <asp:TextBox TextMode="MultiLine" runat="server" ID="txtComments" Width="100%" Height="60px" CssClass="TDTextArea" Style="border: 0px;" MaxLength="2000" />
    <!-- some other controls and things -->
  </ContentTemplate>
</asp:UpdatePanel>

代码背后:

ProjectRequest.StatusComments = txtComments.Text.Trim

对象属性:

Protected mStatusComments As String = String.Empty
Property StatusComments() As String
  Get
    Return mStatusComments.Trim
  End Get
  Set(ByVal Value As String)
    mStatusComments = Value
  End Set
End Property

存储过程调用:

Common.RunSP(mDBConnStr, "ProjectStatusUpdate", _
    Common.MP("@UID", SqlDbType.NVarChar, 40, mUID), _
    Common.MP("@ProjID", SqlDbType.VarChar, 40, mID), _
    Common.MP("@StatusID", SqlDbType.Int, 8, mStatusID), _
    Common.MP("@Comments", SqlDbType.NVarChar, 4000, mStatusComments), _
    Common.MP("@PCTComp", SqlDbType.Int, 4, 0), _
    Common.MP("@Type", Common.TDSqlDbType.TinyInt, 1, EntryType))

这是最奇怪的部分 . 当我调试代码时,如果我输入

“测试
测试”

(没有引号)进入注释文本框,然后单击保存按钮并使用立即窗口在我单步执行时查看变量值,这是我得到的:

?txtComments.Text
"test test"
?txtComments.Text.Trim
"test
test"
?txtComments.Text(4)
"
"c
?txtComments.Text.Trim()(4)
"
"c

任何人都知道这里发生了什么?

2 回答

  • 8

    这里有两个问题 . 首先,VB中的即时窗口将不可打印的字符转换为空格,因此您无法看到它 . 在C#中,它将使用其替换转义码(例如 \n\r\n )显示该字符,但VB不会 . 其次,VB将中断视为仅换行( vbLf ),而不是回车换行( vbCrLf ) . 因此,如果您在立即窗口中以中断模式执行以下操作,您将看到我的意思(假设您在注释框中键入 test ,按Enter键, test ):

    ?txtComments.Text.Substring(4,1) = vbLf
    True
    
  • 2

    可能是,您应该使用 Environment.NewLine 常量替换为vbCrLf

相关问题