代码之家  ›  专栏  ›  技术社区  ›  Gordon Thompson

客户端+服务器端自定义验证的用户控件;选择了错误的客户端验证程序

  •  1
  • Gordon Thompson  · 技术社区  · 14 年前

    我有一个用户控件,它包含一个自定义验证器,根据是否选中一个单选按钮来使用(有几个单选按钮,我只显示相关的一个)。

    <asp:RadioButton runat="Server" ID="RadioBetween" GroupName="DateGroup" CssClass="date_group_options_control_radio" />
    <asp:TextBox ID="FromDate" runat="server" Columns="8"></asp:TextBox>
    <asp:TextBox ID="ToDate" runat="server" Columns="8"></asp:TextBox>
    
    <asp:CustomValidator ID="DateValidator" runat="server" Display="Dynamic" ClientValidationFunction="ValidateDateFields_Client" OnServerValidate="ValidateDateFields"></asp:CustomValidator>
    

    有一些客户机+服务器端验证代码(服务器端代码执行完全相同的操作,为了简洁起见跳过了这些代码)

    <script type="text/javascript">
    function ValidateDateFields_Client(source, args)
    {
        if ("<%=EnableValidation%>" == "True")
        {
            var bRadioBetweenSelected = false;
    
            var oRadio = document.getElementById('<%=RadioBetween.ClientID%>');
            if (oRadio != null && (oRadio.checked == true || oRadio["checked"] == true))
            {
                bRadioBetweenSelected = true;
            }
    
            if (bRadioBetweenSelected)
            {
                var oFromDate = document.getElementById('<%=FromDate.ClientID%>');
                var oToDate = document.getElementById('<%=ToDate.ClientID%>');
    
                if (oFromDate != null && oToDate != null)
                {
                    var sFromDate = oFromDate.value;
                    var sToDate = oToDate.value;
    
                    source.innerHTML = ValidateFromToDate(sFromDate, sToDate, args);
    
                    if (!args.IsValid)
                    {
                        return;
                    }
                }
                else
                {
                    args.IsValid = true;
                }
            }
            else
            {
                args.IsValid = true;
            }
        }
    }
    </script>
    

    此控件在页面中有两个实例。在运行客户端版本时,它碰到了错误的版本(禁用的控件版本)。从生成的HTML中可以看出,两者都是正确指定的。我不知道.NET是如何计算出调用哪个clientside函数的,因为它们都有相同的名称。

    <script type="text/javascript">
    //<![CDATA[
    var ctl00_MCPH1_QueryTextValidator = document.all ? document.all["ctl00_MCPH1_QueryTextValidator"] : document.getElementById("ctl00_MCPH1_QueryTextValidator");
    ctl00_MCPH1_QueryTextValidator.controltovalidate = "ctl00_MCPH1_SearchTextBox";
    ctl00_MCPH1_QueryTextValidator.focusOnError = "t";
    ctl00_MCPH1_QueryTextValidator.display = "Dynamic";
    ctl00_MCPH1_QueryTextValidator.evaluationfunction = "CustomValidatorEvaluateIsValid";
    ctl00_MCPH1_QueryTextValidator.clientvalidationfunction = "ValidateQueryText_Client";
    ctl00_MCPH1_QueryTextValidator.validateemptytext = "true";
    var ctl00_MCPH1_DisplayOptionsControl1_DateValidator = document.all ? document.all["ctl00_MCPH1_DisplayOptionsControl1_DateValidator"] : document.getElementById("ctl00_MCPH1_DisplayOptionsControl1_DateValidator");
    ctl00_MCPH1_DisplayOptionsControl1_DateValidator.display = "Dynamic";
    ctl00_MCPH1_DisplayOptionsControl1_DateValidator.evaluationfunction = "CustomValidatorEvaluateIsValid";
    ctl00_MCPH1_DisplayOptionsControl1_DateValidator.clientvalidationfunction = "ValidateDateFields_Client";
    var ctl00_MCPH1_PreferencesControl1_PreferencesTabContainer_DisplayOptionsTab_DisplayOptionsControl_DateValidator = document.all ? document.all["ctl00_MCPH1_PreferencesControl1_PreferencesTabContainer_DisplayOptionsTab_DisplayOptionsControl_DateValidator"] : document.getElementById("ctl00_MCPH1_PreferencesControl1_PreferencesTabContainer_DisplayOptionsTab_DisplayOptionsControl_DateValidator");
    ctl00_MCPH1_PreferencesControl1_PreferencesTabContainer_DisplayOptionsTab_DisplayOptionsControl_DateValidator.display = "Dynamic";
    ctl00_MCPH1_PreferencesControl1_PreferencesTabContainer_DisplayOptionsTab_DisplayOptionsControl_DateValidator.evaluationfunction = "CustomValidatorEvaluateIsValid";
    ctl00_MCPH1_PreferencesControl1_PreferencesTabContainer_DisplayOptionsTab_DisplayOptionsControl_DateValidator.clientvalidationfunction = "ValidateDateFields_Client";
    //]]>
    </script>
    

    我需要添加一些东西来确定范围吗?实现这一目标的最佳方法是什么?如果我禁用第二个控件的加载,一切正常。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Frédéric Hamidi    14 年前

    为两个用户控件生成的客户端验证函数的名称相同。会有 ValidateDateFields_Client() 函数,当然,解释器只调用其中一个函数。

    解决这个问题的一种方法是生成唯一的函数名:

    <script type="text/javascript">
    function ValidateDateFields_Client_<%=RadioBetween.ClientID%>(source, args)
    {
        // ...
    }
    </script>
    
    
    <asp:CustomValidator ID="DateValidator" runat="server" Display="Dynamic"
        ClientValidationFunction="ValidateDateFields_Client_"
        OnServerValidate="ValidateDateFields"></asp:CustomValidator>
    
    
    protected void Page_PreRender(object sender, EventArgs e)
    {
        DateValidator.ClientValidationFunction += RadioBetween.ClientID;
    }