Conditionally Required Validator Control

wayneph

Freshman
Joined
Jul 13, 2004
Messages
25
Location
Dallas, TX, USA
I was about to set up a new validation control, but I decided to ask and see if there is one out there, that would save me some time. Here is what I'm looking for.

I have a question with DropDownBox that has ListItems of "Yes" and "No" for answers. Next to the DropDownBox I have a Textbox that will be required depending on the answer of the associated question.

In my mind I see needing 3 properties: ControlToValidate, ConditionalControl, AnswerWhenRequired. In the associated Code, if the ConditionalControl = AnswerWhenRequired and ControlToValidate is blank, then the Validator is not valid.

Does anyone know where to find something like this, if it already exists?
 
Well, I have created something that works, but I'm not 100% happy with it. I know there should be a way to do it where I don't write out a seperate JavaScript Function for each one, but I can't think of it right now. My problem is finding a way to specify the "QuestionControl" and "QuestionAnswer" in such a way that they are available to the JavaScript function.

The other thing that would be nice is the ability to set up the "QuestionControl" so that when you are in the .NET designer, you have the dropdown list of controls similar to what is available for "ControlToValidate".

I've attached my class. I'd like to hear any suggestions if anyone has some...
 

Attachments

As soon as I hit post, I came up with a way to solve the first part. This new OnPreRender function takes care of the JavaScript. The other is really just cosmetic, if someone has an answer.

Visual Basic:
		Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
			If EnableClientScript Then
				'Add the Attribute to the HTML for the verify Function
				Me.Attributes("evaluationfunction") = "crv_verify"
				Me.Attributes.Add("QuestionControl", QuestionControl)
				Me.Attributes.Add("QuestionAnswer", QuestionAnswer)
				'Create the Javascript function to check the control
				Dim sb As New System.Text.StringBuilder

				sb.Append("<script language=""JavaScript"">" & vbCrLf)
				sb.Append("  function crv_verify(val)  {" & vbCrLf)
				sb.Append("    var tmp = document.getElementById(val.QuestionControl);" & vbCrLf)
				sb.Append("    var response = true;" & vbCrLf)
				sb.Append("    if (tmp.options[tmp.selectedIndex].value == val.QuestionAnswer)  {" & vbCrLf)
				sb.Append("      if (document.getElementById(val.controltovalidate).value == """")  {" & vbCrLf)
				sb.Append("        response = false;" & vbCrLf)
				sb.Append("      }" & vbCrLf)
				sb.Append("    }" & vbCrLf)
				sb.Append("    return response;" & vbCrLf)
				sb.Append("  }" & vbCrLf)
				sb.Append("</script>" & vbCrLf)

				'Register the Script Block.
				Me.Page.RegisterClientScriptBlock("CRV", sb.ToString())
			End If

			MyBase.OnPreRender(e)
		End Sub
 
Back
Top