1

i want to call the function located in the code behind from javascript

Button :

<button class = "btn btn-outline btn-danger dim" type = "button" onclick = "confirmDelete ()"> <i class = "fa fa-trash"> </i> Delete </button>

Javascript function :

<script>
     function confirmDelete () {
         if (confirm ('Are you sure you want to delete this?')) {
             // call function Delete_Click ();
         }
         return false; // cancel the click event always
     }
 </script>

the function I want to call:

protected void Delete_Click (object sender, EventArgs e)
     {
         try
         {
             Delete item
         }
         catch (Exception ex)
         {
             SetError (ex.Message);
         }
     }

if I use <asp: Button> or <asp: LinkButton>, there are some css don't show up. That's why I use

  • Check this https://stackoverflow.com/questions/5731224/calling-javascript-function-from-codebehind – Nagib Mahfuz Nov 19 '20 at 11:00
  • Could you provide an example in codepen or something similar so that we have a working example that we can take a closer look at? – Grant Nov 19 '20 at 11:12

1 Answers1

0

First change you button and add runat,Id,OnClientClick and OnClick event :

   <button Id="MyBtn" runat="server" 
    class = "btn btn-outline btn-danger dim" type ="button" 
    OnClientClick="return confirmDelete()" 
    OnClick="Delete_Click"> 
      <i class = "fa fa-trash"> </i> Delete 
    </button>

And change you js function :

 function confirmDelete () {
     return confirm ('Are you sure you want to delete this?');
 }
nAviD
  • 2,784
  • 1
  • 33
  • 54