停止回传框TextChanged [英] Stop postback on TextChanged
问题描述
我在具有附加给它的TextChanged事件的aspx页面的文本框。
我也有附加到文本框的验证。
在文本改变时,会触发验证但如果有错误TextChanged事件仍称。你知道如果有可能停止对框TextChanged如果校验火灾回发?
< ASP:文本框ID =txtQuantity=服务器的AutoPostBack =真ontextchanged =txtQuantity_TextChanged>< / ASP:文本框>
< ASP:的RequiredFieldValidator ID =reqQuantity的ControlToValidate =txtQuantity=服务器的ErrorMessage =质量是强制性的。>< / ASP:&的RequiredFieldValidator GT;
您可以移动验证,以客户端加入 EnableClientScript =真正的
属性。支票将与JS执行回发不会发生。
以外,你可以检查TextChanged事件,使执行回调函数时,函数定义是否可以进入页面是否有效。您应该添加的ValidationGroup
属性为您的验证,并调用 Page.Validate
函数之前指定该组Page.IsValid
被选中。
UPD
下面的一角。
添加您自己的JS功能,例如:
函数的IsValid(参数){
如果(args.value.length == 0){
返回false;
}
其他{
返回true;
}
}
在的Page_Load
事件添加此code:
txtQuantity.Attributes [的onchange] =如果(的IsValid(本)==假)回报;;
这不会弄乱输入时是正确的,但否则将prevent回传的自动提交。
I have a textbox in an aspx page that has a TextChanged event attached to it. I also have a validator attached to the textbox.
When the text is changed, the validate triggers but in case there is an error the textchanged event is still called. Do you know if it's possible to stop the postback on textchanged if the validator fires?
<asp:TextBox ID="txtQuantity" runat="server" AutoPostBack="true" ontextchanged="txtQuantity_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqQuantity" ControlToValidate="txtQuantity" runat="server" ErrorMessage="The quantity is mandatory."></asp:RequiredFieldValidator>
You can move validation to client side adding EnableClientScript="true"
attribute. Postback won't occur as check will be performed with JS.
Other than that you can check whether page is valid when performing callback function for TextChanged event so that to define whether function can proceed. You should add ValidationGroup
attribute to your validator and call Page.Validate
function specifying that group before Page.IsValid
is checked.
Upd
Here's the tip.
Add your own JS function, e.g.:
function IsValid( args ) {
if( args.value.length == 0 ) {
return false;
}
else {
return true;
}
}
In Page_Load
event add this code:
txtQuantity.Attributes[ "onchange" ] = "if ( IsValid(this) == false ) return;";
This won't mess up auto postback when input is correct, but will prevent postback otherwise.
这篇关于停止回传框TextChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!