8

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.

5 Answers 5

15

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

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

4 Comments

Yes but then I cannot call any c# functions! I want to disable submit behavior and call a function. Even UseSubmitBehavior is set to false it still submits.
@Pinchy: if you need to invoke a C# function you need to do AJAX on your Event handler. That's your only choice. Use JQuery or any other JS library to do the AJAX call and invoke the C# function.
Is there any tutorial on that? Thanks
@Pinchy: yes. Lot of them. Google: "ASP.NET AJAX".
3

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

1 Comment

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.
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.

Comments

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;"/>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.