【问题标题】:CheckBoxList CustomValidator ClientValidationFunction Only Runs on Second and Subsequent SubmitCheckBoxList CustomValidator ClientValidationFunction 仅在第二次和后续提交时运行
【发布时间】:2014-02-24 21:10:51
【问题描述】:

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

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

效果很好。完美,事实上。 ...除了我第一次点击提交按钮。

CheckBoxList 在后面的 VB 代码中使用简单的 DataSource = x, DataBind... blah blah... 动态填充。 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

有什么想法吗?

【问题讨论】:

  • 你能显示用户控件的代码,checkboxlist吗?
  • 为 CheckBoxList 添加了代码。

标签: javascript asp.net checkboxlist customvalidator


【解决方案1】:

关于我的设置的一些事情是将活动扳手投入到工作中。因此,我放弃了客户端验证,转而使用服务器端验证。

只需在 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-17
    • 2018-05-25
    • 2015-09-28
    • 1970-01-01
    • 2021-04-29
    • 2014-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多