【问题标题】:runtime generated Checkbox validation运行时生成的复选框验证
【发布时间】:2012-01-05 04:05:42
【问题描述】:

我在 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 函数中我返回这个:

function 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

请帮忙...

【问题讨论】:

    标签: asp.net checkbox runtime generated


    【解决方案1】:

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

    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;
      }
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-07
      • 2017-11-23
      • 2018-09-28
      • 2020-12-30
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      相关资源
      最近更新 更多