Thursday 4 August 2011

How To: Define Custom EventArgs for the ServerValidate event fired by a CustomValidator

Question
How can i either:

1) Put a string of my choice in the ServerValidateEventArgs.Value Property
OR
2) Define my own EventArgs class and pass that as an argument to the ServerValidate event of a CustomValidator control.

Context
Building a dynamic data driven Web Survey application.
I have a Repeater control, which uses custom template (implementing ITemplate) to render its contents.
Each row of the Repeater represents a Survey Question. Each Question can potentially have multiple answer options.
Hence, for each Row in the Repeater, there exists a bunch of dynamically generated Input controls (EG RadioButton, CheckBox, TextBox)

Requirement
For some specific Rows in the Repeater, at least one of the inputs needs to contain a value (IE that question is mandatory)

Solution So Far
I have placed a CustomValidator control in each Row of the Repeater and hooked up its ServerValidate event to a handler within the custom template class.

The Road Block
I now need to be able to access the all the Input controls within that Repeater row to Validate whether they have been Checked or not.

The handler of the ServerValidate event currently only gives me access to:
a. A reference to the CustomValidator
b. The ServerValidateEventArgs object - in turn providing the ServerValidateEventArgs.Value property.

However, ASP.Net sets ServerValidateEventArgs.Value by default to be the Value property of the control defined in the CustomValidator.ControlToValidate property. But i am not setting CustomValidator.ControlToValidate to anything, as there is not one particular control i am trying to Validate - i'm trying to validate a group of controls!

No comments:

Post a Comment