ASP.net button without submit

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


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.


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

Tags:

Asp.Net