CustomValidatation control that fire on multiple control events
- by George
I am trying to create a UserControl that is a composite control for entering Dates. It involves 2 drop down controls (one for month and one for day) and a text box for year. It also has a CustomValidation control that I would like the user of the UserControl to be able to set the ClientValidationFunction property and have it triggered whenever the value in any of the 3 date part controls changes.
To hook up each of the 3 controls to the validation control, I use teh AJAX ValidatorHookupControlID method.
If I hard code the following code directly in my aspx page that houses the usercontrol, my Client Validation Function is successfulluy called when any of the 3 date part control values are changed:
<script type="text/javascript">
//<![CDATA[
debugger;
alert('hooking up');
ValidatorHookupControlID("ctl00_phPageContent_dtmPassportExpirationDate_txtYear", document.all("ctl00_phPageContent_dtmPassportExpirationDate_CustomValidator1"));
ValidatorHookupControlID("ctl00_phPageContent_dtmPassportExpirationDate_ddlDate", document.all("ctl00_phPageContent_dtmPassportExpirationDate_CustomValidator1"));
ValidatorHookupControlID("ctl00_phPageContent_dtmPassportExpirationDate_ddlMonth", document.all("ctl00_phPageContent_dtmPassportExpirationDate_CustomValidator1"));
//]]>
However, I would like my usercontrol to emit this Javascript, so I added the following Code.
Public Property ClientValidationFunction() As String
Get
Return CustomValidator1.ClientValidationFunction
End Get
Set(ByVal value As String)
CustomValidator1.ClientValidationFunction = value
End Set
End Property
Public Property EnableClientScript() As Boolean
Get
Return CustomValidator1.EnableClientScript
End Get
Set(ByVal value As Boolean)
CustomValidator1.EnableClientScript = value
End Set
End Property
Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
If Me.ClientValidationFunction.Trim <> "" AndAlso
Me.EnableClientScript Then
RegisterValidationScript()
End If
End Sub
Private Sub RegisterValidationScript()
Dim Key As String = Me.ClientID & "_ClientValidate"
If Not Page.ClientScript.IsClientScriptBlockRegistered(Key) Then
Page.ClientScript.RegisterClientScriptBlock(GetType(String), Key, GetJavascript, True)
End If
End Sub
Private Function GetJavascript() As String
Dim Out As String = ""
Const Quote As String = Chr(34)
Out &= "debugger;" & vbCrLf
Out &= "ValidatorHookupControlID(" & Quote & txtYear.ClientID & Quote & ", document.all(" & Quote & CustomValidator1.ClientID & Quote & "));" & vbCrLf
Out &= "ValidatorHookupControlID(" & Quote & ddlDate.ClientID & Quote & ", document.all(" & Quote & CustomValidator1.ClientID & Quote & "));" & vbCrLf
Out &= "ValidatorHookupControlID(" & Quote & ddlMonth.ClientID & Quote & ", document.all(" & Quote & CustomValidator1.ClientID & Quote & "));" & vbCrLf
Return Out
End Function
Unfortunately, when the ValidatorHookupControlID method is called, the second parameter (the one that locates the validation control) always evaluates to null and the method fails.
ValidatorHookupControlID("ctl00_phPageContent_DateSelector21_txtYear", document.all("ctl00_phPageContent_DateSelector21_CustomValidator1"));
Can you tell me how to correct this error?