18

Is it possible to have an asp button that isn't rendered with a type="submit" tag. I don't want the button to submit the form, so I'd like to have it as type="button" instead. Perhaps a better way to phrase my question would be: How do I prevent an asp button from submitting?

6 Answers 6

45

You can just set UseSubmitBehavior="false" and it will render type="button" instead of type="submit" (.net 4 at least)

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

2 Comments

UseSubmitBehavior is property to chose which submit mechanism (browser or ASP.NET using javascript) to use. The property does not disable page submit. For more info see MS docs
In order to prevent pressing the button to submit the form, you can put the button into an UpdatePanel
11

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

2 Comments

UseSubmitBehavior is property to chose which submit mechanism (browser or ASP.NET using javascript) to use. The property does not disable page submit.
In order to prevent pressing the button to submit the form, you can put the button into an UpdatePanel
3

Two ways:

1) Add a OnClientClick property which returns false.

 <asp:Button runat="Server" ONClientClick="return true"/>

2) Or use a HTML Control i.e:

 <input type="button" id="testButton" value="Regular Button" runat="server"/>

2 Comments

In my case, UseSubmitBehavior="false" still refreshed the page. The second option of this answer is working instead.
UseSubmitBehavior is property to chose which submit mechanism (browser or ASP.NET using javascript) to use. The property does not disable page submit. For more info see MS docs
2

If you don't want the button to submit, do you need a button at all ? After all, it doesn't do anything before it posts back to the server. You can might as well just use an <input type="button".

That being said, you can use javascript to prevent a postback:

<asp:Button runat="server" OnClientClick="return false;" />

4 Comments

Not using an html button because of this: stackoverflow.com/questions/4608733/onserverclick-not-working. Is there a way to stop the submit server-side as well?
"stop the submit server-side" doesn't really make sense. Either you submit or you don't, it's the client (browser) that ultimately decides this. Perhaps give some more context, what are you trying to accomplish ?
I just had to update a webforms application and I stumbled across this. The Question is confusing for sure. I'm not sure why anyone would want to use OnClientClick ... per the other answers This works for ME , because I have jquery to interact with a button <asp:Button UseSubmitBehavior="false" ID="Button1" runat="server" Text="Update" /> Which renders OUT as type="button" instead of type="submit" perfect...
"If you don't want the button to submit, do you need a button at all ?" - to do other things like open modal popups/dialogs , change values, transform DOM etc.
1

You need to prevent postback when pressing on an <asp:Button>

Check this

Comments

0
  1. btnGrades.UseSubmitBehavior = false;

  2. btnGrades.OnClientClick = "btnGradesClick(this)";

  3. js function :

    function btnGradesClick(btn) { console.log(btn); }

  4. in chrome : press f12 and see the console tab then click on the button you can see the button tag.

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.