Javascript CheckBoxList CustomValidator ClientValidationFunction仅在第二次提交和后续提交时运行
我有一个动态添加到DNN页面的用户控件 此用户控件基于CheckBoxList(另一个基于CheckBoxList的自定义控件,它在Pre_呈现中只有一点代码来获取有关当前用户的信息),并且可能需要使用验证器。在控件上,我有一个CustomValidator,它的ClientValidationFunction也在控件上设置为javascript块 它工作得很好。很好,事实上。。。除了第一次单击提交按钮 复选框列表是用一个简单的DataSource=x,DataBind。。。胡说八道。。。在VB代码后面。ASCX端的控件非常简单。但是,第一次单击包含此控件的页面上的submit按钮时,它不会启动ClientValidationFunction。第二次,成功了。第三次,工作 在下面,查找my ASCX的全部内容:Javascript CheckBoxList CustomValidator ClientValidationFunction仅在第二次提交和后续提交时运行,javascript,asp.net,checkboxlist,customvalidator,Javascript,Asp.net,Checkboxlist,Customvalidator,我有一个动态添加到DNN页面的用户控件 此用户控件基于CheckBoxList(另一个基于CheckBoxList的自定义控件,它在Pre_呈现中只有一点代码来获取有关当前用户的信息),并且可能需要使用验证器。在控件上,我有一个CustomValidator,它的ClientValidationFunction也在控件上设置为javascript块 它工作得很好。很好,事实上。。。除了第一次单击提交按钮 复选框列表是用一个简单的DataSource=x,DataBind。。。胡说八道。。。在VB
<%@ 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." />
函数ValidateCheckboxList(源,参数){
//window.alert(“启动”);
var chkListModules=document.getElementById(“”);
var chkListinputs=chkListModules.getElementsByTagName(“输入”);
对于(变量i=0;i
以及用于用户控件的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
导入系统
导入System.Web.UI
导入System.Web.UI.WebControl
导入MyCustom.Modules
命名空间MyCustom.Controls
公共类复选框列表
继承System.Web.UI.WebControl.CheckBoxList
Private _GlobalID为整数
公共属性GlobalID()为整数
得到
返回_GlobalID
结束
设置(ByVal值为整数)
_GlobalID=值
端集
端属性
Private Sub CheckBoxList_PreRender(ByVal sender作为对象,ByVal e作为System.EventArgs)处理Me.PreRender
将用户设置为MyCustom.Modules.Users.UserInfo=HttpContext.Current.Items(“LoggedInUser”)
如果用户什么都不是,那么
Dim objUserInfo As UserInfo=UserController.GetCurrentUserInfo
Dim UserID为整数=objUserInfo.UserID
如果UserID>-1,则
如果HttpContext.Current.Items(“LoggedInMyCustomUser”)为空,则
将myCustomUserController设置为新的MyCustom.Modules.Users.UserController
User=myCustomUserController.MyCustomGetUser(6,UserID,True)
HttpContext.Current.Items(“LoggedInMyCustomUser”)=用户
其他的
User=HttpContext.Current.Items(“LoggedInMyCustomUser”)
如果结束
如果结束
如果结束
如果用户为空,则退出Sub
将MyCache设置为新的GlobalTextCache(User.CorpID)
对于Me.Items中的每个li作为列表项
作为字符串的Dim NewVal
NewVal=MyCache.GetGlobalText(li.Text,User.PreferredLocale,User.CorpID)
如果NewVal不是什么也不是_
那么是“NewVal”吗
li.Text=NewVal
如果结束
下一个
端接头
末级
结束命名空间
有什么想法吗?关于我的设置,有人在工作中使用活动扳手。因此,我放弃了客户端验证,转而使用服务器端验证 只需在CustomValidator上设置ServerValidate的属性:
<asp:CustomValidator runat="server" ForeColor="Red" ID="cvCheckBoxList" OnServerValidate="cvCheckBoxList_ServerValidate" ErrorMessage="At least one item must be selected." />
在stackoverflow上找到了此服务器端解决方案。能否显示用户控件的代码,复选框列表。为复选框列表添加了代码。
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