将模块/控件添加到ASP.NET CMS中的网页时出现问题,该网页具有表单验证,它将验证页面上的所有表单,因为ValidationGroup具有相同的名称。如何用函数解决这个问题?
答案 0 :(得分:0)
我提出了以下解决方案:
迭代某个占位符的函数,为每个表单分配唯一的ValidationGroup名称。
Public Shared Sub SetValGroupControls(PageModule As Control, valGroupName As String)
For Each ctrl As Control In PageModule.Controls
If TypeOf ctrl Is RequiredFieldValidator Then
DirectCast(ctrl, RequiredFieldValidator).ValidationGroup = valGroupName
ElseIf TypeOf ctrl Is RegularExpressionValidator Then
DirectCast(ctrl, RegularExpressionValidator).ValidationGroup = valGroupName
ElseIf TypeOf ctrl Is CompareValidator Then
DirectCast(ctrl, CompareValidator).ValidationGroup = valGroupName
ElseIf TypeOf ctrl Is CustomValidator Then
DirectCast(ctrl, CustomValidator).ValidationGroup = valGroupName
ElseIf TypeOf ctrl Is RangeValidator Then
DirectCast(ctrl, RangeValidator).ValidationGroup = valGroupName
ElseIf TypeOf ctrl Is Button Then
DirectCast(ctrl, Button).ValidationGroup = valGroupName
End If
If ctrl.Controls.Count > 0 Then
SetValGroupControls(ctrl, valGroupName)
End If
Next
End Sub
其中PageModule表示包含验证控件的占位符(以避免遍历页面上的所有控件),ValGroupName表示将被指定为名称的唯一字符串。
假设我的验证控件包含在名为“plhMyForm”的占位符中,您可以像这样调用函数:
Dim nGuid = String.format("{0}-validation", Guid.NewGuid())
SetValGroupControls(plhMyForm, nGuid)
上面会创建一个唯一的字符串,如: 24ad5ae3-FEAD-4a6f-98F3-dffcd2e815ba验证
当然,您可以使用自己独特的字符串生成。
现在您可以将ValidationGroup名称留空,并且该函数会为您分配一个。这样,同一页面上需要验证的每个表单都将单独验证自身。