7

Is there ASP button that won't submit post form data.

Whenever I click an asp button it post form data and I want to change that behavior so I can post some other form data I will create it programmatically.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Pinchy
  • 1,506
  • 8
  • 28
  • 49

5 Answers5

14

Using the attribute UseSubmitBehaviour="false" solved my problem

<asp:Button ID="button1" runat="server" UseSubmitBehavior="false" Text="just a button" />

This attribute is available since .Net v2.0, for more information: Button.UseSubmitBehavior Property

Community
  • 1
  • 1
Imran Rafique
  • 329
  • 3
  • 10
  • 3
    This is incorrect. If UseSubmitBehavior is false, then the button type is changed to ```button```, but the form is still submitted via javascript. If its true, then the button type is ```submit```. Either way the form is submitted. – Sprintstar Feb 14 '18 at 14:05
5

You need to use JavaScript and intercept the onclick event, and there you make an Ajax call.

In the event handler, return false and your form won't be submitted. Something like this:

<script>
    function on_s() {
       /// do some AJAX with JQuery or any other library
       $.ajax({ url: "your_asp_entry.aspx" });
       // you can add some processing to the AJAX call
       return false;
    }
</script>
<body>
<form runat="server">
<asp:Button id="b1" Text="Submit" runat="server" OnClick="return on_s();" />
</form>
</body>

On your_asp_entry.aspx you do whatever you need to invoke your C# function/method.

Update: changed the answer after OP said on the comments he/she needs to invoke a C# function.

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
3

add onclientclick="Javascript:return false;" to aspbutton

SMK
  • 2,098
  • 2
  • 13
  • 21
  • This doesn't work, at least not in SP2013 and if used in a pop up dialog. This causes the dialog to close. The solution by @Imran Rafique works. –  Sep 13 '16 at 04:14
0

The root issue here is that a <button> will submit its parent form by default, unless otherwise specified (e.g. type="button").

Here's a related answer that discusses adding the UseSubmitBehavior="false" ASP attribute to set the correct type attribute on the button.

And here's documentation from MDN that explains the default type="submit" type when none is specified.

dmbaughman
  • 578
  • 2
  • 7
0

Here is an example button html button without form submit, just use html button with runat="server" and type="button".

<input id="butDelete" runat="server" class="btn btn-danger" type="button" 
value="Delete" OnServerClick="butDelete_ServerClick" onclick="if 
(!confirm('Sure?')) return;"/>
Max B
  • 21
  • 3