Why does this markup ...
<asp:TextBox ID="TextBox1" runat="server" CausesValidation="False"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegExValidatorTextBox1" runat="server"
ControlToValidate="TextBox1"
Text="Invalid date format."
ValidationExpression="(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d">
</asp:RegularExpressionValidator>
... throw开发者_开发知识库 a validation error when I enter some nonsense in the TextBox like "a" and hit only the Enter or Tab key? I' like to have the validation not before I click on a submit button.
(I'm working with VS2008 SP1, .NET 3.5 SP1, if that matters.)
To answer the question (Why does Enter or Tab key cause a validation on a TextBox?), when you press Enter, the DefaultButton will cause the form to post back. Pressing enter is akin to clicking on the submit button.
If you want to disable the postback on enter, there are several solutions suggested here, also see here and here for a JQuery based solution.
Set the EnableClientScript property to false. This will completely prevent validation on the client side. Then on the server side, be sure to check the Page.IsValid property before proceeding.
精彩评论