How can I cancel a client side server control event?

I have an asp.net image button and I want to cancel the click event if it fails client side validation ... how to do this?

0


source to share


3 answers


<asp:ImageButton OnClientClick="return ValidatorOnSubmit()" />

      

The ValidatorOnSubmit () function should already be enabled by the ASP.NET framework if you have validators on your form. The standard onsubmit WebForm function looks like this:



function WebForm_OnSubmit() {
   if (   typeof(ValidatorOnSubmit) == "function" 
       && ValidatorOnSubmit() == false) return false;

   return true;
}

      

+1


source


There is an OnClientClick event that you can set for your javascript function. If you return it will continue until the post office. If you return false, no message appears.



<asp:Button ID="NavigateAway" runat="server" OnClientClick="javascript:return PromptToNavigateOff();" OnClick="NavigateAwayButton_Click" Text="Go Away" />

  <script type="text/javascript">
    function PromptToNavigateOff()
    {
        return confirm("Are you sure you want to continue  and loose all your changes?");
    }

  </script>

      

0


source


I would just reverse the logic and not let the user click the button until they fill in this information. Place the required bullets and if it is filled, the button will be enabled.

0


source







All Articles