 |
|
|
 |
|
Enable or Disable ASP.NET Validators From Within JavaScript
|
by Jeffrey Dale Starr, MCSD
If you've had any experience with .NET Validators, you know they can be tricky. One of the keys to becoming adept at using them is to understand the chronology of their implementation.
Each .NET web page has a boolean property named "IsValid" - if the page is not in a "valid" state, then no submission to the server will be made. Which is great - because who needs a PostBack and screen flicker just to find out that we're missing a value?
Where this becomes a challenge is when we want to make exceptions.
For example, let's say we have a DropDownList called "ddlMethodOfPayment". It contains the following items:
Cash
Check
Credit Card
Purchase Order
Invoice
We also have a RequiredFieldValidator on a TextBox called "txtTax".
But our client doesn't charge tax on Purchase Orders, so in that case having a value in txtTax isn't required.
Now, at this point, I'm sure some of you are thinking "Couldn't this logic be worked out with a CustomValidator?" and in this particular scenario, you're probably right. I'm just using this example for times when your situation is too complex to be handled by a CustomValidator. I've had a few cases where the page itself was being created so dynamically that you would have to write 2,000 lines of code to account for each possible permutation.
Back to our example. So obviously what we need to do is disable the RequiredFieldValidator, "rfTax", if, OnChange, the value of ddlMethodOfPayment = "Purchase Order".
So first we need to create our JavaScript function that will be called onchange of our dropdown:
Couple of things to note: On line 15, you'll see that we're setting a string variable that will populate the ClientID of the control we want to grab. This can be very helpful (as opposed to hard-coding a rendered ID) in situations like a UserControl, where the concatenated ASP.NET control id might change depending on where the UserControl is being used. On line 23, you'll see the ValidatorEnable method being called. This is generated by ASP.NET and if you put in a "debugger" command and F11 through your code, you can step into that method to check out all the little things it does.
Next, we need to attach the "OnChange" event to our dropdown. Since this is an ASP.NET control, best practice is to attach the event in our server-side code:
This is a good point to remind ourselves of the chronology set out in the beginning of this article. The "IsValid" property of the page being set to "false" will prevent the page from being sent to the webserver - thus, we'll never hit our code-behind C#. So our JavaScript function takes care of that little piece.
However, somewhat frustratingly, disabling the RequiredFieldValidator client-side does not disable the Validator server-side. For that, we have one little step left. We need to override the Validate method of the page and redo our logic in server-side code:
You might go months or years without ever having to do something like this, but it's another arrow for your quiver. Plus, the more you understand what's happening under the hood, the better programmer you'll become.
|
|
|
 |