CheckBoxList CustomValidator ClientValidationFunction仅在第二次和后续提交时运行

时间:2014-02-24 21:10:51

标签: javascript asp.net checkboxlist customvalidator

我有一个动态添加到DNN页面的用户控件。

此用户控件基于CheckBoxList(以及另一个基于CheckBoxList的自定义控件,它只在Pre_Render中有一些代码来获取有关当前用户的信息),并且可能需要使用验证器。在控件上,我有一个CustomValidator,它的ClientValidationFunction也设置为控件上的javascript块。

它很漂亮。事实上,完美无缺。 ...除了第一次点击“提交”按钮。

在后面的VB代码中,使用简单的DataSource = x,DataBind ...等等等等来快速填充CheckBoxList。 ASCX方面的控件非常简单。但是,第一次单击包含此控件的页面上的提交按钮时,它不会触发ClientValidationFunction。第二次,作品。第三次,工作。

在下面,找到我的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." />

用户控件的VB:

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

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

关于我的设置的一些事情是扔掉一把猴子扳手。因此,我放弃了客户端验证,转而使用服务器端验证。

只需在CustomValidator上设置 OnServerValidate 属性:

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

然后,在自定义控件上填写函数(其中cbValue是CheckBoxList控件):

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

在stackoverflow上找到了此服务器端解决方案here