Pinchy Pinchy - 4 months ago 45
ASP.NET (C#) Question

ASP.net button without submit

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.

thanks a lot
Any help will be much appreciated

Answer

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.