Question

I have a user control that is dynamically added to pages on DNN.

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.

It works beautifully. Perfectly, in fact. ... EXCEPT on the first time I click the Submit button.

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.

Below, find the entire contents 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">
    &nbsp;<font color="red" bold="true">*</font>&nbsp;
</asp:Literal>
<asp:CustomValidator runat="server"
    ForeColor="Red" ID="cvCheckBoxList"
    ClientValidationFunction="ValidateCheckboxList"
    ErrorMessage="At least one item must be selected." />

And the VB 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 ideas?

Was it helpful?

Solution

Something about my setup is throwing a monkey wrench into the works. Consequently, I have abandoned the Client Side validation in favor of a Server Side validation.

Simply set the OnServerValidate property on the CustomValidator:

<asp:CustomValidator runat="server" ForeColor="Red" ID="cvCheckBoxList"  OnServerValidate="cvCheckBoxList_ServerValidate" ErrorMessage="At least one item must be selected." />

Then, fill out the function on the custom control (where cbValue is a CheckBoxList control):

Protected Sub cvCheckBoxList_ServerValidate(source As Object, args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvCheckBoxList.ServerValidate
    Dim isValid = False

    For Each c As ListItem In cbValue.Items
        If c.Selected Then
            isValid = True
        End If
    Next

    args.IsValid = isValid
End Sub

This Server Side solution was found here, on stackoverflow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top