运行时生成的复选框验证

发布于 2024-11-05 13:27:29 字数 1937 浏览 1 评论 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 函数中,我返回这个:

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

请帮助...

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

           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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

给不了的爱 2024-11-12 13:27:29

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

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

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

    函数 GetChk(chkbox, args) {
        if (!isConfirmed) {
            var chkBoxList = document.getElementById('ctl00_ContentPlaceHolder1_tc_hospital_improvement_features_tp_Reflection_cbl_116');
            var chkBoxCount = chkBoxList.getElementsByTagName("输入");
            for (var i = 0; i < chkBoxCount.length; i++) {
                if (chkBoxCount[i].checked == true) {
                    返回真;
                }
            }
            返回假;
        }
        返回真;
    }
    

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;
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文