代码之家  ›  专栏  ›  技术社区  ›  Brandon Montgomery

只有在调用.trim()时,多行文本框中的vbcrlf才会显示。

  •  3
  • Brandon Montgomery  · 技术社区  · 14 年前

    我有一个将textmode设置为multiline的ASP文本框。当用户试图在文本中插入换行符时,我在保留vbcrlf字符方面遇到了问题。当按下页面上的按钮时,我将从控件中提取文本,使用string.trim对其进行剪裁,并将该值赋给对象上的string属性(该属性又将其赋给对象上的私有内部字符串变量)。然后,该对象从私有内部变量中获取值,并使用存储过程调用(将其放入的sp参数是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>
    

    代码落后:

    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 回复  |  直到 14 年前
        1
  •  8
  •   Thomas    14 年前

    这里有两个问题。首先,VB中的即时窗口正在将不可打印的字符转换为一个空格,因此您无法看到它。在C中,它将使用替换转义码(例如 \n \r\n ,但vb没有。第二,vb只将换行符视为换行符。( vbLf )不是回车+换行( vbCrLf )因此,如果您在即时窗口的中断模式下执行以下操作,您将看到我的意思(假设您键入 test ,点击回车, 测试 在评论框中):

    ?txtComments.Text.Substring(4,1) = vbLf
    True
    
        2
  •  2
  •   VMAtm    14 年前

    可能,你应该使用 Environment.NewLine 用vbcrlf替换的常量