【ASP.NET】动态绑定验证控件

一个CheckBoxList可以如此验证
1.在aspx文件中添加javascript

<script language="javascript" type="text/javascript"> 
        
<!--
        
function  ClientValidate(sender, args)
        {
            
var flag = false;
            
var  inarr=form1.all.tags("input");
            
for (var i=0; i<inarr.length; i++)
            {
                
if(inarr[i].type=="checkbox")
                 {
                    
if(inarr[i].checked==true)    
                     {
                         flag 
= true;
                     }
                  }
             }
             
if (flag)
             {
                args.IsValid 
= true;
             }
             
else
             {
                args.IsValid 
= false;
             }
        }
        
-->
</script>
2.添加自定义验证控件(CustomValidater),并将其ClientValidationFunction 属性设为
Dim customValidatorVer As New CustomValidator      
customValidatorVer.ClientValidationFunction 
= "ClientValidate"

以上是验证表单(form)中所有的checkbox是有一个被选中,引自金色約定之家
如果我想验证多个CheckBoxList呢?
方案一:动态输出JavaScript
1.建立生成JavaScript的函数

    '''<summary>
    '''指定CheckBoxList的验证函数
    '''</summary>
    '''<param name="inCheckboxList">需要验证的控件</param>
    '''<returns>完整的JavaScrip字符串</returns>
    Private Function GetCheckScript(ByVal inCheckboxList As CheckBoxList) As String
        
Dim newLine As String = Chr(13& Chr(10)
        
Dim strScript As New StringBuilder
        strScript.Append(
"<script language='javascript' type='text/javascript'>")
        strScript.Append(newLine)
        strScript.Append(
"function CheckSearch_" & inCheckboxList.ClientID & "(sender, args)")
        strScript.Append(newLine)
        strScript.Append(
"{" & newLine & "with(document.forms[0])")
        strScript.Append(newLine 
& "{" & newLine)
        strScript.Append(newLine 
& "var flag=false;")
        
Dim i As Integer
        
For i = 0 To inCheckboxList.Items.Count - 1
            strScript.Append(newLine)
            strScript.Append(
"if(" + inCheckboxList.ClientID + "_" + i.ToString() + ".checked) {flag=true;}")
        
Next
        strScript.Append(newLine)
        strScript.Append(
"if(!flag) {args.IsValid = false;")
        strScript.Append(newLine)
        strScript.Append(
"}")
        strScript.Append(newLine)
        strScript.Append(
"else")
        strScript.Append(newLine)
        strScript.Append(
"{args.IsValid = true;")
        strScript.Append(newLine)
        strScript.Append(
"}")
        strScript.Append(newLine 
& "}" & newLine & "}" & newLine)
        strScript.Append(
"</script>")
        
Return strScript.ToString
    
End Function
2.引用如上函数

Dim aLiteral As New LiteralControl
aLiteral.Text 
= GetCheckScript(CheckBoxList1)
Panel1.Controls.Add(aLiteral)
LiteralControl类
表示 HTML 元素、文本和 ASP.NET 页中不需要在服务器上处理的任何其他字符串。
JavaScript同样属于HTML元素。可以输出到页面。
3.添加自定义验证控件,并绑定验证函数(ClientValidationFunction )

Dim aValidator As New CustomValidator
aValidator.ClientValidationFunction 
= "CheckSearch_" & CheckBoxList1.ClientID
aValidator.Text 
= "Please Check the Checkbox!"
Panel1.Controls.Add(aValidato )

以下是生成的JavaScript代码:
<script language="javascript" type="text/javascript">
function CheckSearch_CheckBoxList1()
{
    
with(document.forms[0])
    {
    
var flg=false;
    
if(CheckBoxList1_0.checked) {flg=true;}
    
if(CheckBoxList1_1.checked) {flg=true;}
    
if(CheckBoxList1_2.checked) {flg=true;}
    
if(CheckBoxList1_3.checked) {flg=true;}
    
if(CheckBoxList1_4.checked) {flg=true;}
    
if(CheckBoxList1_5.checked) {flg=true;}
    
if(CheckBoxList1_6.checked) {flg=true;}
    
if(CheckBoxList1_7.checked) {flg=true;}
    
if(!flg) 
        {
            args.IsValid 
= false;
        }
    
else
        {
            args.IsValid 
= true;
        }
    }
}
</script>
这样就可以验证动态生成的CheckBoxList1,CheckBoxList2,CheckBoxList3。。。。。了

方案二:判断验证控件的Id
1.在aspx文件中加入javascript
    <script language="javascript" type="text/javascript"> 
        
<!--
        
function  ClientValidate(sender, args)
        {
            
var flag = false;
            
var e = document.form1.elements;   
            
for (var i=0; i<e.length; i++)
            { 
                
if(e[i].type=="checkbox" && e[i].name.substr(0,sender.id.length-2== sender.id.substr(2,sender.id.length-2))
                {
                    
if(e[i].checked==true && e[i].name.substr(0,3=="chk")    
                    {
                        flag 
= true;
                    }
                } 
            }
             
             
if (flag)
             {
                args.IsValid 
= true;
             }
             
else
             {
                args.IsValid 
= false;
             }
        }
        
-->
    
</script>
e[i].name.substr(0,sender.id.length-2) == sender.id.substr(2,sender.id.length-2)) 判断了验证控件的id是否主要部分和
要验证的CheckBoxList相同,指定的验证控件只验证和自己ID主要部分相同的CheckBoxList.
2.添加自定义验证控件(CustomValidater),并将其ClientValidationFunction 属性设为ClientValidate,并指定其Id
Dim customValidatorVer As New CustomValidator 
customValidatorVer.ClientValidationFunction 
= "ClientValidate"
customValidatorVer.Id
=“V_” & CheckBoxList1.ClientID

要是把一些控件及其相关的验证都写到一个用户控件(ascx)里,是不是就不会互相影响了?




原文地址:https://www.cnblogs.com/sekihin/p/808713.html
Creative Commons License 本作品采用 知识共享署名-非商业性使用 2.5 中国大陆许可协议进行许可。