asp.net - 在调用 HttpRequest.ValidateString() 之前修改 asp 文本框
问题描述
我已经构建了一个正则表达式函数,我想调用它来清理粘贴到 asp 文本框中的文本,但看起来 HttpRequest.ValidateString() 在执行按钮事件处理程序之前被调用。是否可以在验证之前通过我的函数运行文本?
解决方案
我尝试了 ONCHANGED 事件,你是对的,它最初并没有直接起作用,
快速搜索显示此参考页...
所以尝试一下,一切正常。
默认.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>
默认.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
注意文本框的定义包括
- 自动回发 = 真
ViewStateMode = 启用
由于我不知道您在提交表单后正在执行什么处理,因此它可能无法正常工作(?)
不过在那个例子中它工作得很好。看看这是否有帮助,不要忘记阅读引用的链接
Netframework-4.7.2#System_Web_UI_WebControls_TextBox_OnTextChanged_System_EventArgs_
推荐阅读
- android - 类不返回 MutableMap 对象
- macos - 我的 Apache2 .conf 文件有什么问题
- python - 强制 Y 轴从 0 开始
- python - 在 Python 中,如何计算包含固定格式项的格式说明符所隐含的宽度
- sql-server - 通过 JDBC 连接将本地 SQL Server 数据库连接到数据块时出错
- x86 - 即使 VGA 内存已初始化,QEMU 也会提供空白输出
- python - Python如何从实体关系结构派生完整的层次结构
- html - 将站点从 IE 迁移到 Chrome 和 Edge 支持时要研究的场景
- php - 创建 Laravel 路由以将 Product 模型附加到 Order 模型
- java - 如何事先获取将存储在 Android/Java 缓存中的未来 Glide 图像大小?