I am encountering an issue with a user control that is dynamically added to pages on DNN.
The user control is built on a customized version of CheckBoxList and includes a CustomValidator with ClientValidationFunction set to a javascript function. It works perfectly, except for the first time the Submit button is clicked.
The CheckBoxList is populated dynamically in the VB code behind, while the ASCX side remains simple. However, upon clicking the submit button for the first time, the ClientValidationFunction does not fire. Subsequent clicks work fine.
Below is the content of my ASCX:
<%@ Control Language="vb"
Inherits="MyCustom.Modules.WebApps.WebAppsFormBuilderControls.WebAppsFormBuilder_Controls_CustomCheckboxList"
CodeFile="WebAppsFormBuilder_Controls_CustomCheckboxList.ascx.vb"
AutoEventWireup="false" Explicit="True" %>
<script type="text/javascript">
function ValidateCheckboxList(source, args) {
//window.alert('starting');
var chkListModules = document.getElementById('<%= cbValue.ClientID %>');
var chkListinputs = chkListModules.getElementsByTagName("input");
for (var i = 0; i < chkListinputs.length; i++) {
if (chkListinputs[i].checked) {
args.IsValid = true;
}
}
args.IsValid = false;
//window.alert('IsValid = ' + args.IsValid);
}
</script>
<myCustom:CheckBoxList ID="cbValue" runat="server" />
<asp:Literal ID="Literal1" runat="server">
<font color="red" bold="true">*</font>
</asp:Literal>
<asp:CustomValidator runat="server"
ForeColor="Red" ID="cvCheckBoxList"
ClientValidationFunction="ValidateCheckboxList"
ErrorMessage="At least one item must be selected." />
VB code for the user control:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports MyCustom.Modules
Namespace MyCustom.Controls
Public Class CheckBoxList
Inherits System.Web.UI.WebControls.CheckBoxList
Private _GlobalID As Integer
Public Property GlobalID() As Integer
Get
Return _GlobalID
End Get
Set(ByVal value As Integer)
_GlobalID = value
End Set
End Property
Private Sub CheckBoxList_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
Dim User As MyCustom.Modules.Users.UserInfo = HttpContext.Current.Items("LoggedInUser")
If User Is Nothing Then
Dim objUserInfo As UserInfo = UserController.GetCurrentUserInfo
Dim UserID As Integer = objUserInfo.UserID
If UserID > -1 Then
If HttpContext.Current.Items("LoggedInMyCustomUser") Is Nothing Then
Dim myCustomUserController As New MyCustom.Modules.Users.UserController
User = myCustomUserController.MyCustomGetUser(6, UserID, True)
HttpContext.Current.Items("LoggedInMyCustomUser") = User
Else
User = HttpContext.Current.Items("LoggedInMyCustomUser")
End If
End If
End If
If User Is Nothing Then Exit Sub
Dim MyCache As New GlobalTextCache(User.CorpID)
For Each li As ListItem In Me.Items
Dim NewVal As String
NewVal = MyCache.GetGlobalText(li.Text, User.PreferredLocale, User.CorpID)
If NewVal IsNot Nothing AndAlso _
NewVal <> "" Then
li.Text = NewVal
End If
Next
End Sub
End Class
End Namespace
Any insights?