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?
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;" />
-
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? – Radu Jan 5 '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 5 '11 at 20:44
-
2I 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... – Tom Stickel Sep 8 '15 at 21:59
-
"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 9 at 7:44
You can just set UseSubmitBehavior="false"
and it will render type="button"
instead of type="submit"
(.net 4 at least)
-
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
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
-
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 8: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
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"/>
-
In my case, UseSubmitBehavior="false" still refreshed the page. The second option of this answer is working instead. – Yusril Maulidan Raji May 3 '17 at 8:25
-