Stop postback on TextChanged

后端 未结 5 1627
鱼传尺愫
鱼传尺愫 2021-01-13 17:26

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

5条回答
  •  逝去的感伤
    2021-01-13 17:50

    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.

提交回复
热议问题