Asp button with different button type

2019-01-18 14:11发布

问题:

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?

回答1:

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


回答2:

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



回答3:

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



回答4:

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


回答5:

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

Check this