在调用HttpRequest.ValidateString()之前修改ASP文本框

时间:2018-08-15 16:23:38

标签: asp.net vb.net webforms

我已经构建了一个正则表达式函数,我想调用它来清理粘贴到asp文本框中的文本,但是它看起来像在执行按钮事件处理程序之前调用了HttpRequest.ValidateString()。在验证之前是否可以通过我的函数运行文本?

1 个答案:

答案 0 :(得分:0)

我尝试了ONCHANGED事件,您说对了,它起初并没有立即起作用,

快速搜索显示了THIS参考页...

所以尝试一下,一切正常。

Default.Aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head runat="server">
    <title></title>
  </head>
  <body>
    <form id="form1" runat="server">
      <div>
        The event TextBox1_TextChanged() is raised when the text changes in the textbox<br />
        The TextBox1_TextChanged event calls the CapitaliseText subroutine.<br />
        The Capitalisetext subroutine changes the text in textbox2 to the same as Textbox1 with the FIRST letter capitalised.<br />
        If the first letter in Textbox1 is NOT lower case then Textbox2 does not change<br />
        <br />
        All this is done without clicking the submit button.<br />
        The code runs on the server not the client.<br />
        <br />
        <br />
        Textbox1 -
        <asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" ViewStateMode="Enabled"></asp:TextBox>
        <br />
        <br />
        Textbox-2 - <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        <br />
        <br />
        Submit Form -
        <asp:Button ID="Button1" runat="server" Text="Submit Form" />
        <br />
        <br />
        <br />
        <br />
        Textbox2 Contains -
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <br />
      </div>
    </form>
  </body>
</html>

Default.Aspx.Vb

Partial Class _Default
  Inherits System.Web.UI.Page

  Dim FormWasSubmitted As Boolean

  Protected Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
    'run my regex
    CapitaliseText(sender, e)
  End Sub

  Sub CapitaliseText(sender As Object, e As EventArgs)
    '
    Dim XString As String

    'get textbox1.text
    XString = sender.Text
    If Trim(XString) <> "" Then
      'if first chracter is not caps
      If (Char.IsLower(XString.Chars(0))) Then
            TextBox2.Text = Char.ToUpper(XString.Chars(0)) + XString.Substring(1, XString.Length - 1)
        UpdateLabel()
      End If
    End If
  End Sub

  Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    FormWasSubmitted = True
    UpdateLabel()
  End Sub

  Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    UpdateLabel()
  End Sub

  Sub UpdateLabel()
    If FormWasSubmitted Then
      If Trim(TextBox2.Text) <> "" Then
        Label1.Text = TextBox2.Text & "[Submitted]"
      Else
        Label1.Text = "Nothing"
      End If
    Else
      If Trim(TextBox2.Text) <> "" Then
        Label1.Text = TextBox2.Text
      Else
        Label1.Text = "Nothing"
      End If
    End If
  End Sub
End Class

请注意,文本框的定义包括

  • AutoPostback = True
  • ViewStateMode =启用

由于我不知道提交表单后您正在执行什么处理,所以极有可能它不起作用(?)

虽然在该示例中工作正常。 看看有没有帮助,别忘了阅读引用的链接

Netframework-4.7.2#System_Web_UI_WebControls_TextBox_OnTextChanged_System_EventArgs_