用户控制与客户端+服务器端CustomValidation;错误的客户端验证器拾取 [英] User Control with Client + Server Side CustomValidation; Wrong Client side validator is picked

查看:140
本文介绍了用户控制与客户端+服务器端CustomValidation;错误的客户端验证器拾取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含它是根据一个单选按钮是否被选中或不使用的CustomValidator的用户控件(有几个单选按钮,我只显示相关的一个)

I have a user control which contains a CustomValidator which is used according to whether a RadioButton is checked or not (there are several RadioButtons, I'm only showing the relevant one)

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

有一些客户端+服务器端验证code(服务器端code不完全一样,并跳过了简洁)

There is some client + server side validation code (the server side code does exactly the same thing and is skipped for brevity)

<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是如何工作了打电话给其客户方的功能它们都具有相同的名称。

There are two instances of this control in the page. When running the client side version it hits the wrong one (the version of the control which is disabled). You can see from the generated HTML both are correctly specified. I'm not sure how .NET works out which clientside function to call given they both have the same name.

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

我需要到的范围将它添加什么东西?什么是实现这一目标的最佳途径?如果我禁用第二个控制一切的负载正常工作。

Do i need to add something in to scope it? What's the best way to achieve this? If I disable the loading of the second control everything works fine.

推荐答案

您客户端验证功能与为您的用户控件同名的产生。会有的两个 ValidateDateFields_Client()在你的页面的功能,当然还有间preTER只会调用其中的一个。

Your client validation function is generated with the same name for both your user controls. There will be two ValidateDateFields_Client() functions in your page, and of course the interpreter will only call one of them.

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

One way to work around that problem would be to generate unique function names:

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

这篇关于用户控制与客户端+服务器端CustomValidation;错误的客户端验证器拾取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆