3

我有一个文本模式设置为多行的 ASP 文本框。当用户尝试在文本中插入换行符时,我在保留 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

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

4

2 回答 2

10

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

?txtComments.Text.Substring(4,1) = vbLf
True
于 2010-05-13T16:45:01.997 回答
2

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

于 2010-05-19T07:08:45.363 回答