多行文本框中的 vbCrLf 仅在调用 .Trim() 时显示
我有一个 ASP 文本框,其 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))
这是最奇怪的部分。当我调试代码时,如果我输入
“test
test”
(不带引号)到注释文本框中,然后单击“保存”按钮并使用即时窗口在我逐步执行时查看变量值,这就是我得到的:
?txtComments.Text
"test test"
?txtComments.Text.Trim
"test
test"
?txtComments.Text(4)
"
"c
?txtComments.Text.Trim()(4)
"
"c
有人知道这里发生了什么吗?
I have an ASP TextBox with TextMode set to MultiLine. I'm having problems with preserving the vbCrLf characters when a user tries to put line breaks into the text. When a button on the page is pressed, I'm taking the text from the control, trimming it using String.Trim, and assigning that value to a String property on an object (which, in turn assigns it to a private internal String variable on the object). The object then takes the value from the private internal variable and throws it into the database using a stored procedure call (the SP parameter it is put into is an nvarchar(4000)).
ASPX Page:
<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>
code behind:
ProjectRequest.StatusComments = txtComments.Text.Trim
object property:
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
stored proc call:
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))
Here's the strangest part. When I debug the code, if I type
"test
test"
(without the quotes) into the comments text box, then click the save button and use the immediate window to view the variable values as I step through, here is what I get:
?txtComments.Text
"test test"
?txtComments.Text.Trim
"test
test"
?txtComments.Text(4)
"
"c
?txtComments.Text.Trim()(4)
"
"c
Anyone have a clue as to what's going on here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里有两个问题。首先,VB 中的直接窗口将不可打印字符转换为空格,因此您看不到它。在 C# 中,它将使用其替换转义码(例如
\n
或\r\n
)显示字符,但 VB 不会。其次,VB 将换行符视为仅换行 (vbLf
),而不是回车+换行 (vbCrLf
)。因此,如果您在立即窗口中以中断模式执行以下操作,您将明白我的意思(假设您输入test
,按 Enter 键,在注释框中test
):There are two problems at bay here. First, the immediate window in VB is converting the non-printable character to a space so you cannot see it. In C#, it will show the character using its replacement escape code (e.g.
\n
or\r\n
), but VB does not. Second, VB sees the break as a line-feed only (vbLf
) not a carriage-return+line-feed (vbCrLf
). Thus, if you do the following in break mode in the immediate window you will see what I mean (assuming you typetest
, hit Enter,test
in the comments box):也许,您应该使用
Environment.NewLine
常量来替换为 vbCrLfMay be, you should use
Environment.NewLine
constant to get replaced with the vbCrLf