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