这是我的asp.net标记:
<asp:TextBox runat="server" Style="width: 100px;" ID="txtCon" OnTextChanged="txtCon_TextChanged" MaxLength="10" AutoPostBack="true" />
<asp:RegularExpressionValidator ValidationGroup="con" runat="server" ControlToValidate="txtCon"
Display="Dynamic" ErrorMessage="Invalid Format. Ex. 12 is valid format" ValidationExpression="(\d){1,10}" />
问题是如果我在文本框中输入abc并且在控件中输出tab,它仍会进行回发。我想阻止这种行为。我怎么能这样做?
答案 0 :(得分:0)
不要使用OnTextChanged事件而是使用javascript函数为例如keyup让我知道这是否有帮助
<asp:TextBox runat="server" Style="width: 100px;" ID="txtCon" MaxLength="10" AutoPostBack="false" /> <asp:RegularExpressionValidator ValidationGroup="con" runat="server" ControlToValidate="txtCon"
Display="Dynamic" ErrorMessage="Invalid Format. Ex. 12 is valid format" ValidationExpression="(\d){1,10}" />
js功能
$(document).ready(function () {
$("txtCon").each(function () {
$(this).keyup(function () {
//your function here
});
});
});
答案 1 :(得分:0)
在文本框中添加 ValidationGroup =“con” AND CausesValidation =“true”
用这个
替换你的文本框 <asp:TextBox runat="server" Style="width: 100px;" ID="txtCon" OnTextChanged="txtCon_TextChanged"
MaxLength="10" AutoPostBack="true" ValidationGroup="con" CausesValidation="true"/>