【问题标题】:The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>) error not working无法修改 Controls 集合,因为该控件包含代码块(即 <% ... %>)错误不起作用
【发布时间】:2015-07-29 10:53:42
【问题描述】:

我正在使用一些 javascript 代码进行验证,如下所示:-

var ErrArr = [];
    $(document).ready(function () {
        $('#btnSave').click(function (e) {
            e.preventDefault();
            validateTitle();
            validateddl();
            validateTextBoxes();
            checkBoxChecking();
            if (ErrArr.length > 0) {
                alert(ErrArr.join("\n"));
                ErrArr = [];
                return false;
            }
        });
        function checkBoxChecking() {
            if ($("#chkDeclaration").is(":checked")) {
                // alert("first button checked");
                return false;
            }
            else {
                ErrArr.push('Check the declaration');
                return true;
            }
        }
        function validateTitle() {
            if ($("#ddlTitle").val() > "0") {
                if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
                    ErrArr.push("Please enter the text in other title");
                }
            } else {
                ErrArr.push('Please select the title');
            }
        }
        function validateTextBoxes() {
            if ($("#txt_emp_fname").val() === "") {
                ErrArr.push('First name is required');
            }
            if ($("#txt_emp_mname").val() === "") {
                ErrArr.push('Middle name is required');
            }
            if ($("#txt_emp_lname").val() === "") {
                ErrArr.push('Last name is required');
            }
            if ($("#txtFatherName").val() === "") {
                ErrArr.push('Father name is required');
            }

            if ($("#txtDateofJoin").val() === "") {
                ErrArr.push('Date of joining is required');
            }

            if ($("#txtReligion").val() === "") {
                ErrArr.push('Religion is required');
            }
            if ($("#txtPersonalEmail").val() === "") {
                ErrArr.push('Email Id is required');
            }
            if ($("#txtDtOfBirth").val() === "") {
                ErrArr.push('Date of birth is required');
            }
            if ($("#txtAt").val() === "") {
                ErrArr.push('Place of birth is required');
            }
            if ($("#txtTaluka").val() === "") {
                ErrArr.push('Taluka is required');
            }
            if ($("#txtPostOffice").val() === "") {
                ErrArr.push('Post office is required');
            }
            if ($("#txtDistrict").val() === "") {
                ErrArr.push('District is required');
            }
            if ($("#txtStatePersonal").val() === "") {
                ErrArr.push('State is required');
            }
            if ($("#txtDtMarriage").val() === "") {
                ErrArr.push('Date of Marriage is required');
            }
            if ($("#TxtPassportNo").val() === "") {
                ErrArr.push('Passport no is required');
            }
            if ($("#txtIdMark").val() === "") {
                ErrArr.push('Identification mark is required');
            }
        }
        function validateddl() {
            if ($("#ddlGender").val === "" || $("#ddlGender").val() == "0") {
                ErrArr.push('Please select the gender');
            }
            if ($("#ddlMaritalStatus").val === "" || $("#ddlMaritalStatus").val() == "0") {
                ErrArr.push('Please select the Marital Status');
            }
            if ($("#ddlNationality").val === "" || $("#ddlNationality").val() == "0") {
                ErrArr.push('Please select the Nationality');
            }
        }
    });

这里发生的情况是,当我使用此 javascript 代码时,我的按钮单击事件不会触发。当我删除 JS 代码时,我得到错误

无法修改 Controls 集合,因为该控件包含代码块(即 )。

这是我的 aspx 代码:-

<asp:Button ID="btnSave" CssClass="button" Text="Save" runat="server" CausesValidation="true"
                    OnClick="btnSave_Click" />

请注意,我已经从 stack 和 google 提供的其他链接中搜索并尝试过,但我无法成功。

请帮忙

【问题讨论】:

    标签: javascript c# jquery asp.net


    【解决方案1】:

    您应该看到这个并使用&lt;asp:Button OnClickClick='..' /&gt; 属性:

    请参阅thisthis SO 帖子了解更多信息。

    <asp:Button 
     ID="btnSave" 
     CssClass="button" 
     Text="Save" 
     runat="server" 
     CausesValidation="true" 
     OnClick="btnSave_Click" 
     OnClientClick='return performValidation();'
    />
    

    performValidation 将是一个要调用的主函数,它将首先运行,您可以在其中调用您的验证函数。如果您不想发布页面,则应返回 false

    例如:

    先修改各个验证函数,验证失败时返回false:

    $(document).ready(function () {
    
    function checkBoxChecking() {
        if ($("#chkDeclaration").is(":checked")) {
           return false;
        }
        else {
           ErrArr.push('Check the declaration');
           return false; //return false when any validation fails.
        }
    }
    function validateTitle() {
        if ($("#ddlTitle").val() > "0") {
            if ($("#ddlTitle").val() == "1104" && $("#txtTitle").val() === "") {
                ErrArr.push("Please enter the text in other title");
            }
        } else {
            ErrArr.push('Please select the title');
            return false;//same here. return false when fail.
        }
    }
     //this is your main validation function..
     function performValidation()
     {
        var ret = true;
        ret = checkBoxChecking();
        ret = validateTitle();
        //etc 
        //then ..
        return ret;
     }
    });
    

    【讨论】:

    • performValidation() 我应该调用哪些验证??
    • 将所有验证代码放入其中以检查 "x field is required" ...(您也可以调用您已经编写的验证函数,例如checkBoxChecking(),但它们是如果验证失败,应修改为返回 false,您应该通过 return checkBoxChecking(); 调用它们
    • 你能告诉我怎么做吗,因为我有点困惑。请看这里jsfiddle.net/022tdvm3还有如何调用这个函数
    • 您链接的内容应该可以工作。到底发生了什么。您是否看到错误。等等?我已经用更多代码更新了我的答案。
    • 使用我的代码,页面在没有任何验证消息的情况下提交。让我试试你更新的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 2013-08-08
    • 2011-06-27
    相关资源
    最近更新 更多