Asp button with different button type

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

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

Method 4

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

Method 5

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

Check this


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x