1

我有一个用户控件,其中包含一个 CustomValidator,它根据是否选中 RadioButton 来使用(有几个 RadioButtons,我只显示相关的一个)

<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 如何确定调用哪个客户端函数,因为它们都具有相同的名称。

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

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

4

1 回答 1

2

为您的两个用户控件生成具有相同名称的客户端验证函数。您的页面中将有两个 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;
}
于 2010-11-23T20:51:43.523 回答