OnClientClick does not works on asp.net LinkButton

Abbas picture Abbas · Apr 30, 2013 · Viewed 33k times · Source

I have an asp.net linkbutton, which contains the OnClientClick property, however the function within the OnClientClick never gets called, it directly jumps to OnClick function.
Below are the 2 ways I am using LinkButton as:

<asp:LinkButton ID="btn" CssClass="button" Text="Browse Thumbnail" runat="server" 
     OnClientClick="return confirm('Are you sure you want to delete?');">
</asp:LinkButton>

and:

<asp:LinkButton ID="lnkDelete" runat="server" 
     OnClientClick="return confirm('Are you sure you want to delete this slide?');" 
     CommandName="DeleteThumbnail" CommandArgument='<%# Container.DataItemIndex %>'>
   <asp:Image ImageUrl="~/images/delete.gif" ID="imgDelete" runat="server"></asp:Image>  
</asp:LinkButton>

Both of the approaches does not works.

Can anyone please provide some solution for the same.

Answer

Shafqat Masood picture Shafqat Masood · Apr 30, 2013
  OnClientClick="javascript:return confirmAction();"  

  function confirmAction() {  
      if(confirm('Are you sure you want to delete?')) {  
        // you clicked the OK button.  
        // you can allow the form to post the data.  
        return true;  
    }  
    else {  
        return false;  
        }  
    }  

implement the Onclick on the server side

 protected void lnkdelete_Click(object sender, EventArgs e)  
 {  
 }  

and if you dnt want to call server method use this

   OnClientClick="javascript:confirmAction(); return false;"