【发布时间】: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">
<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." />
以及用于用户控件的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