views:

1085

answers:

4

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>
A: 

EDIT: After author's comment.

You don't want to check the quantity after every keystroke.

  1. Get rid of your AutoPostBack="true". This is causing it to postback after every keystroke.
  2. Add a RegularExpressionValidator using the RexEx "^\n+$".
    
    
    

Go ahead and keep the RequiredFieldValidator. The RegEx validator only fires if there is something in the textbox. They are both needed in your case.

Rap
The idea is not that. The OnTextChanged is doing a database save, not on every keystroke but when the control loses focus. This is the intended behavior. The validator is also needed and seems to me it should be straightforward to stop the postback.
Dante
A: 

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.

terR0Q
The EnableClientScript property does not avoid the postback...
Dante
Updated answer with simple solution...
terR0Q
You are right, and I tried this. The thing is I have a RangeValidator which needs a Min and Max values, and I'm having problems sending those two values into the IsValid function. But your idea works.For that reason I'm going to accept your answer although the problem is not solved..
Dante
You still can back client side scenario up.For that purpose add page-level variable on server side, then in server mark-up right smth like:var MinValueStr = "<%= MinValue %>";and convert it into integer before use.
terR0Q
That wouldn't work because the textbox is in a List control, I need specific Min and Max values per row in the list, but I think I overcame it. I'll share when finished.
Dante
A: 

try it after change AutoPostBack="true" as AutoPostBack="false"..

Jaswant Agarwal
And how is my TextChanged event going to postback then? :s
Dante
Why you want to postback on textchanged event..is there business logic attached with it?
A: 
;;;;;;;;;;;;;;;;;;;;;;
gy