title: CheckBoxList CustomValidator ClientValidationFunction Only Runs on Second and Subsequent Submit
body: <p>I have a user control that is dynamically added to pages on DNN.</p>
  
  <p>This user control is based on the CheckBoxList (well, another custom control based on the CheckBoxList which only has a bit of code in the Pre_Render to get information about the current user) and may need the use of a validator. On the control, I have a CustomValidator that has its ClientValidationFunction set to a javascript chunk also on the control.</p>
  
  <p>It works beautifully. Perfectly, in fact. ... EXCEPT on the first time I click the Submit button.</p>
  
  <p>The CheckBoxList is populated on the fly with a simple DataSource = x, DataBind... blah blah... in the VB code behind. The control, on the ASCX side, is quite simple. However, the first time I click the submit button on the page in which this control is contained, it does not fire the ClientValidationFunction. Second time, works. Third time, works.</p>
  
  <p>Below, find the entire contents of my ASCX:</p>
  
  <pre><code>&lt;%@ Control Language="vb"
      Inherits="MyCustom.Modules.WebApps.WebAppsFormBuilderControls.WebAppsFormBuilder_Controls_CustomCheckboxList"
      CodeFile="WebAppsFormBuilder_Controls_CustomCheckboxList.ascx.vb"
      AutoEventWireup="false" Explicit="True" %&gt;
  
  &lt;script type="text/javascript"&gt;
      function ValidateCheckboxList(source, args) {
          //window.alert('starting');
  
          var chkListModules = document.getElementById('&lt;%= cbValue.ClientID %&gt;');
          var chkListinputs = chkListModules.getElementsByTagName("input");
  
          for (var i = 0; i &lt; chkListinputs.length; i++) {
              if (chkListinputs[i].checked) {
                  args.IsValid = true;
              }
          }
          args.IsValid = false;
  
          //window.alert('IsValid = ' + args.IsValid);
      }
  &lt;/script&gt;
  
  &lt;myCustom:CheckBoxList ID="cbValue" runat="server" /&gt;
  &lt;asp:Literal ID="Literal1" runat="server"&gt;
      &amp;nbsp;&lt;font color="red" bold="true"&gt;*&lt;/font&gt;&amp;nbsp;
  &lt;/asp:Literal&gt;
  &lt;asp:CustomValidator runat="server"
      ForeColor="Red" ID="cvCheckBoxList"
      ClientValidationFunction="ValidateCheckboxList"
      ErrorMessage="At least one item must be selected." /&gt;
  </code></pre>
  
  <p>And the VB for the user control:</p>
  
  <pre><code>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 &gt; -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 &lt;&gt; "" Then
                      li.Text = NewVal
                  End If
              Next
          End Sub
      End Class
  End Namespace
  </code></pre>
  
  <p>Any ideas?</p>