views:

759

answers:

1

I have a Custom Control that has multiple textbox fields and a checkbox contained within it.

If the checkbox is checked, and the user submits the form, I need to allow them to continue if the textbox's are empty (and the checkbox is checked).

I have to implement this without javascript.

The problem is that on the OnInit event of the Custom Control, where I wire up the textboxes and their validators, I cannot access the value of the checkbox.


I have been trying to override the uniqueid of the checkbox so that in the custom controls's OnInit event I can disable the required fieldvalidators if this.Context.Request.Form["CheckboxId"] = "On", but as soon as I override the uniqueID of the checkbox viewstate stopped working.

The other thing I thought of is having a custom requiredfieldvalidator that takes the Checkbox in the constructor and is disabled if the value is false.

+1  A: 

Have you tried to perform the control initialization logic during the Control.Load event by overriding the Control.OnLoad method instead?
At that stage in the control lifecycle you should be able to access the value of the CheckBox.

As a side note, if you want to disable validation you don't necessarily have to disable all Validator controls. Instead you can simply set the Button.CausesValidation property to False on the button that will submit the form.

UPDATE:
It sounds like you are building a User Control, that is a complex control made up of an aggregate of simple controls working together as a unit.
If you want to do that entirely in code, like you would with a Custom Control, you should inherit from the CompositeControl class. That gives you the best of both worlds. The child controls initialization is then made by overriding the CreateChildControls method.

Enrico Campidoglio
Do you have a link to an article that describes how to do that? All the examples I've seen use OnInit to add controls in a custom control.
scottschulthess
The Button is outside of the Custom Control that includes the validated controls - so that's not an option. Right now, I'm seeing the value of the Checkbox as being false no matter what in OnLoad
scottschulthess
I updated the answer based on your feedback
Enrico Campidoglio
I haven't had a chance to see if this is a solution yet, but I will and tell you if this is correct, thank you.
scottschulthess