vb.net 使用正则表达式验证器清除文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16731303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clear Textbox with Regular Expression Validator
提问by Sabilv
I Got a text box with a Regular Expression validator, to validate if my textbox is numeric.
我有一个带有正则表达式验证器的文本框,用于验证我的文本框是否为数字。
here's the code :
这是代码:
<asp:TextBox ID="txtAmount" runat="server" CssClass="TextBoxCls"></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ID="valNumbersOnly" ControlToValidate="txtAmount"
SetFocusOnError="true" Display="Dynamic" ErrorMessage="Please enter a numbers only in text box."
Font-Bold="true" ForeColor="Red" ValidationExpression="(^([0-9 ]*|\d*\d{1}?\d*)$)">
</asp:RegularExpressionValidator>
and , if the user accidentally input the wrong data , it'll show the Error Like This :
并且,如果用户不小心输入了错误的数据,它会显示这样的错误:


and i give the user, a clear function to clear all the textbox : with kind of this function :
我给用户一个清除所有文本框的明确功能:使用这种功能:
Public Sub ClearTextBox(ByVal root As Control)
For Each ctrl As Control In root.Controls
ClearTextBox(ctrl)
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
End If
Next ctrl
End Sub
Protected Sub btnClr_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClr.Click
ClearTextBox(Me)
dropResponse.SelectedIndex = 0
FillData()
End Sub
but , the amount is not cleared, it still show the error. why it still happen?
但是,金额没有清除,它仍然显示错误。为什么它仍然发生?
回答by Sean Airey
If you are providing this through another button on the same form it won't ever hit the code-behind.
如果您通过同一表单上的另一个按钮提供此信息,它将永远不会遇到代码隐藏。
If you want a button on a validated form that needs to fire regardless of whether the other controls have validated, put it in a separate validation group:
如果您想要一个经过验证的表单上的按钮,无论其他控件是否已验证,都需要触发,请将其放在单独的验证组中:
<asp:Button runat="server" ID="btnClr" Text="Clear form" OnClick="btnClr_Click" ValidationGroup="unvalidatedControls" />
Alternatively, put a validation group on your controls that need validating and also the button that submits the form:
或者,在需要验证的控件以及提交表单的按钮上放置一个验证组:
<asp:TextBox ID="txtAmount" runat="server" CssClass="TextBoxCls" ValidationGroup="validatedControls"></asp:TextBox>
<asp:RegularExpressionValidator runat="server" ID="valNumbersOnly" ControlToValidate="txtAmount"
SetFocusOnError="true" Display="Dynamic" ErrorMessage="Please enter a numbers only in text box."
Font-Bold="true" ForeColor="Red" ValidationExpression="(^([0-9 ]*|\d*\d{1}?\d*)$)"
ValidationGroup="validatedControls">
</asp:RegularExpressionValidator>
<asp:Button runat="server" ID="btnSubmit" Text="Submit Form" ValidationGroup="validatedControls" />
You could put a ValidationGroupattribute on both your clear button and the main form, as long as they're different, the clear button will work just fine.
您可以ValidationGroup在清除按钮和主窗体上都放置一个属性,只要它们不同,清除按钮就可以正常工作。
Some documentation on Validation Groups: http://msdn.microsoft.com/en-us/library/ms227424.aspx
关于验证组的一些文档:http: //msdn.microsoft.com/en-us/library/ms227424.aspx
回答by Raimond Kuipers
I suspect that the textbox is contained in a panel or another container control. Only the toplevel controls of a page are available in the controls collection of the page.
我怀疑文本框包含在面板或其他容器控件中。页面的控件集合中只有页面的顶级控件可用。
I would suggest to change the parameter of the call to ClearTextBox with the correct container control.
我建议使用正确的容器控件更改对 ClearTextBox 的调用参数。

