1

J'ai un contrôle utilisateur qui contient une CustomValidator qui est utilisé selon si une RadioButton est cochée ou non (il y a plusieurs RadioButtons, je ne montrant que celui concerné)Contrôle utilisateur avec Client + Server CustomValidation; Mauvais côté client validateur est choisi

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

Il y a un code de validation côté client + serveur (le code côté serveur fait exactement la même chose et est ignorée par souci de concision)

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

Il y a deux cas de ce contrôle dans la page. Lors de l'exécution de la version côté client, elle est incorrecte (la version du contrôle est désactivée). Vous pouvez voir à partir du HTML généré les deux sont correctement spécifiés. Je ne suis pas sûr comment .NET fonctionne sur la fonction client à appeler, car ils ont tous les deux le même nom.

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

Ai-je besoin d'ajouter quelque chose pour l'étendre? Quel est le meilleur moyen d'y parvenir? Si je désactive le chargement du second contrôle, tout fonctionne correctement.

Répondre

1

Votre fonction de validation client est générée avec le même nom pour vos deux contrôles utilisateur. Il y aura deux fonctionsValidateDateFields_Client() dans votre page, et bien sûr l'interprète n'appellera que l'un d'eux.

Une façon de contourner ce problème serait de générer des noms de fonction uniques:

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

Étrangement, c'est la même solution que je suis venu avec en parallèle. Merci :-) –

+0

Je déteste parfois .NET (lu la plupart du temps) –