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.
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
button, but the form is still submitted via javascript. If its true, then the button type is submit. Either way the form is submitted.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.
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.