开发者

runtime generated Checkbox validation

I have panel in the asp.net webpage, and i m generating the checkbox at runtime.. i want to validate checkbox, required field validator when form submit.

here is my code:

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());

and in my javascript function i return this:

function GetChk(chkbox, args) {

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

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

   开发者_如何学JAVA        for (var i = 0; i < chkBoxCount.length; i++) {
               if (chkBoxCount[i].checked == true) {
                   return true;
               }
           }

           return false;
       }
       return true;
   }

but i m not getting the value of the checkbox...

required value:= ctl00_ContentPlaceHolder1_tc_hospital_improvement_features_tp_Reflection_cbl_116_0

Actual Value:= ctl00_ContentPlaceHolder1_tc_hospital_improvement_features_tp_complete_stage_chk_confirm

pls help...


first get the runtime generated control into the codebehind file from class file. and then secondly after getting the control property, we can validate the checbox list.

  1. Get the control into codebehind file from class file.

    CheckBoxList cbl = (CheckBoxList)pnlref.FindControl("cbl_116");
    
  2. provide the javascript validation to the runtime generated checkbox list.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜