15

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?

Radu
  • 8,561
  • 8
  • 55
  • 91

6 Answers6

41

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

Bakudan
  • 19,134
  • 9
  • 53
  • 73
Robert Ivanc
  • 1,402
  • 17
  • 32
  • 1
    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](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior(v=vs.110).aspx) – Ondrej Aug 21 '17 at 08:09
  • 1
    In order to prevent pressing the button to submit the form, you can put the button into an UpdatePanel – NineBerry Nov 23 '18 at 16:31
10

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

Ricky
  • 109
  • 1
  • 2
  • UseSubmitBehavior is property to chose which submit mechanism (browser or ASP.NET using javascript) to use. The property does not disable page submit. – Ondrej Aug 21 '17 at 08:08
  • In order to prevent pressing the button to submit the form, you can put the button into an UpdatePanel – NineBerry Nov 23 '18 at 16:31
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"/>
Chandu
  • 81,493
  • 19
  • 133
  • 134
  • In my case, UseSubmitBehavior="false" still refreshed the page. The second option of this answer is working instead. – Yusril Maulidan Raji May 03 '17 at 08:25
  • 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](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior(v=vs.110).aspx) – Ondrej Aug 21 '17 at 08:07
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;" />
driis
  • 161,458
  • 45
  • 265
  • 341
  • Not using an html button because of this: http://stackoverflow.com/questions/4608733/onserverclick-not-working. Is there a way to stop the submit server-side as well? – Radu Jan 05 '11 at 20:42
  • "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 ? – driis Jan 05 '11 at 20:44
  • 2
    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 Which renders OUT as type="button" instead of type="submit" perfect... – Tom Stickel Sep 08 '15 at 21:59
  • 1
    "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. – Kristaps Vilerts Aug 09 '19 at 07:44
1

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

Check this

Ken D
  • 5,880
  • 2
  • 36
  • 58
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.

M Komaei
  • 7,006
  • 2
  • 28
  • 34