Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
3475
asp.net validator does not set focus on error with WebEditors
posted

Using the WebCurrencyEdit control with a validator does not SetFocusOnError when an error is encountered.  Is there some trick to get this to work?

igtxt:WebCurrencyEdit ID="curPayoffAmount" runat="server"                 SkinID ="Popup"                 Style="z-index: 101;left: 124px; position: absolute; top: 20px"                 MaxLength ="10"                MaxValue="9999999.99"                 MinValue="0"                 Width="80px">            </igtxt:WebCurrencyEdit>             <asp:CompareValidator ID="valComPayoffAmount" runat="server"                 ControlToValidate="curPayoffAmount"                 Display="Dynamic"                EnableClientScript="true"                ErrorMessage="Payoff Amount must be greater than 0."                Operator="GreaterThan"                SetFocusOnError ="true"                 Style="z-index: 100; left: 112px; position: absolute;top: 20px"                 Text="*"                 ToolTip="Payoff Amount must be greater than 0."                 ValueToCompare="0.00"></asp:CompareValidator>             <asp:RequiredFieldValidator ID="valReqPayoffAmount" runat="server"                 ControlToValidate = "curPayoffAmount"                 ErrorMessage="To save, the Payoff Amount must be entered."                SetFocusOnError ="true"                 Style="z-index: 100; left: 112px; position: absolute;top: 20px"                 Text = "*"                    ToolTip = "To save, the Payoff Amount must be entered."></asp:RequiredFieldValidator>

 

  • 24497
    posted

    Hi,

    Focus fails because WebTextEdit uses hidden field with id equal to its ClientID. Actual <input> has different id which depends on properties of control. If control does not have buttons (spin, custom), then its id has prefix "igtxt".

    So, to trick validators you may override its "focus" function like below

    <script type="text/javascript">
    if(typeof ValidatorSetFocus == 'function')
    {
     window[
    '_oldValidatorSetFocus'] = ValidatorSetFocus;
     window[
    'ValidatorSetFocus'] = function(val, evt)
     {
      
    var editor = null, old = val ? val.controltovalidate : null;
     
     
    if(old == 'curPayoffAmount')
       {
        editor = igedit_getById(old);
       
    if(editor)
          val.controltovalidate = editor.elem.id;
         
    // or if editor does not have buttons, then line above
         
    // you may replace by
         
    // val.controltovalidate = 'igtxt' + old;
      
    }
       window[
    '_oldValidatorSetFocus'](val, evt);
      
    if(editor)
         val.controltovalidate = old;
     
    }
    }
    </script>