views:

1319

answers:

5

I have an ASP.NET form with three text inputs, one each for "Work Phone", "Home Phone" and "Cell Phone". Each of these text inputs has a RequiredFieldValidator associated with it. I also have a DropDownList where the user can select the preferred phone type.

I want to only require the field that is selected in the DropDownList. For example, if the user selects "Work Phone" from the DropDownList, I want to disable the RequiredFieldValidator for "Home Phone" and "Cell Phone", thereby only making the "Work Phone" field required.

I have a method that enables and disables these validators based on the value of the DropDownList, but I cannot figure out when to call it. I want this method to run before the validation takes place on the page. How would I do that?

A: 

OnChange of the DropDownlist, you will need to register a server-side event handler that enables/disables the validator...

HTH

Sunny
Are you talking about the OnSelectedIndexChanged event of the DropDownList? I set up that event to call the method that enables/disables the validators, but the event doesn't fire until after validation. At that point, it is too late. I need it to fire before validation occurs.
Jeremy
No, he means the client event "onchange"
Claudio Redi
On second thoughts, I would first render the screen with only the dropdown for the preference selection. Then when the user has selected the preference, render a textbox with its relevant validator control instead of doing it all on the client.
Sunny
+2  A: 

You can do this with JavaScript like this:

ValidatorEnable(RequiredFieldValidatorId, false);

Then have your drop down list use the onchange event (I'd suggest using jQuery)

$("#<%=dropDownList.ClientID %>").change(function(){
    var val = $(this).val();
    var skip = null;
    if (val == 1)
       skip = "workPhoneValidator";
    else if (val == 2)
       skip = "cellPhoneValidator";
    ....

    if (skip != "workPhoneValidator") ValidatorEnable(workPhoneValidator, false);
    if (skip != "cellPhoneValidator") ValidatorEnable(cellPhoneValidator, false);
    ....
});
hunter
Have you tried this code? Looks like a nice client side solution. I was just about to post a server side solution, but this one looks like it toggles client side validators, right?
citronas
@citronas - yeah, this should toggle the validators client side without postbacks
hunter
A: 

OnChange event of the drop down you may have something like this

function EnableValidator(){
  if(validatorMustBeEnabled)
    ValidatorEnable(requiredFieldValidator, true);
  else
    ValidatorEnable(requiredFieldValidator, false);
} 

Check on this url. Section "Client-Side APIs"

http://msdn.microsoft.com/en-us/library/Aa479045#aspplusvalid_clientside

Claudio Redi
A: 

Possible way would be:

  • Set in your DropDownList AutoPostBack="true"
  • In the SelectedIndexChanged event handler of the DropDownList enable/disable your validators
  • Set in your DropDownList CausesValidation="false" to avoid that the validators block a postback when you change the DropDownList entry.
Slauma
+1  A: 

Why not use a CustomValidator in this case? Turning off/on a RequiredFieldValidator could lead to a design issue in the future - I'd stick to using them on fields that are going to be required.

Jason M
Thanks for the suggestion. This is what I ended up using, so I am marking it as my answer.
Jeremy