0

我在 asp.net 网页中有面板,并在运行时生成复选框.. 我想验证复选框,表单提交时需要字段验证器。

这是我的代码:

cv = new CustomValidator();

                        cv.ID = "cv" + "_" + dt.Rows[0]["RefQueID"].ToString(); 

                        cv.ValidationGroup = "grp";
                        cv.Display = ValidatorDisplay.None;

                        cv.ErrorMessage = "- Question " + intQuestionNo.ToString();
                        cv.ClientValidationFunction = "chkCount";
                        cv.Attributes.Add("rfvid", cv.ID.ToString());
                        //this portion of code is for custom validation javascript function
                        StringBuilder sb = new StringBuilder();
                        sb.Append("<script type='text/javascript'> function chkCount(sender,args) { ");
                        sb.Append(" args.IsValid = GetChk(document.getElementById('ctl00_ContentPlaceHolder1_" + cbl.ID.ToString() + "'))");
                        sb.Append(" } </script>");
                        Page page = HttpContext.Current.Handler as Page;
                        page.RegisterStartupScript("_Check", sb.ToString());

在我的javascript函数中我返回这个:

函数GetChk(chkbox,args){

       if (!isConfirmed) {
           alert('hi');

           var chkBoxList = document.getElementById(chkbox.ClientID);
           var chkBoxCount = chkBoxList.getElementsByTagName("input");

           for (var i = 0; i < chkBoxCount.length; i++) {
               if (chkBoxCount[i].checked == true) {
                   return true;
               }
           }

           return false;
       }
       return true;
   }

但我没有得到复选框的价值......

所需值:= ctl00_ContentPlaceHolder1_tc_hospital_improvement_features_tp_Reflection_cbl_116_0

实际值:= ctl00_ContentPlaceHolder1_tc_hospital_improvement_features_tp_complete_stage_chk_confirm

请帮助...

4

1 回答 1

0

首先将运行时生成的控件从类文件中获取到代码隐藏文件中。然后在获得控件属性之后,我们可以验证复选框列表。

  1. 将控件从类文件中获取到代码隐藏文件中。

    CheckBoxList cbl = (CheckBoxList)pnlref.FindControl("cbl_116");
    
  2. 向运行时生成的复选框列表提供 javascript 验证。

    function GetChk(chkbox, args) {
        if (!isConfirmed) {
            var chkBoxList = document.getElementById('ctl00_ContentPlaceHolder1_tc_hospital_improvement_features_tp_Reflection_cbl_116');
            var chkBoxCount = chkBoxList.getElementsByTagName("input");
            for (var i = 0; i < chkBoxCount.length; i++) {
                if (chkBoxCount[i].checked == true) {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
    
于 2011-05-09T09:21:35.593 回答