How to apply javascript on <asp: Buttonfield>?

I want to apply javascript on asp:buttonfield when I click a button and then displays a message. Like the confirmation message for the Delete button.

+6
source share
3 answers

In your page load you can add

 button.Attributes.Add("onclick", "confirm('are you sure? blah!')"); 
+1
source

asp: ButtonField> does not have OnClientClick.

Replace with the Templated button

  <script type="text/javascript"> function function() { return confirm("Are you sure you want to delete this?"); } </script> ... <asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="button" runat="server" OnClientClick="return function();" /> </ItemTemplate> </asp:TemplateField> 
+1
source

I found a pretty simple way to do this when working with jQuery, which meant that I could still use asp: boundbutton

Apply CssClass to 'btn-remove' to the associated button (in this case), then add the following jQuery script

  $(function () { $('.btn-remove a').attr('onclick', 'return confirm(\'Are you sure you want to delete this?\')'); }); 
0
source

Source: https://habr.com/ru/post/924515/


All Articles