Prevent an Asp.Net Button from triggering a Postback

Sometimes, you want an Button to not trigger a postback.

One solution is, don't use an button, use an html input or link tag instead. However, you may want to use an asp:XXXButton for consistency with the rest of your page; or it may seem a simple way to make the text on the button localisable (although you can equally achieve that on a plain old html control if you give it an id, a runat="server" and probably a meta:resourcekey); or you may have other stuff you want to do with it serverside.

To stop an button causing a postback, do it like this:

<asp:LinkButton id="btnX" OnClientClick="return false;" runat="server" />

The more likely scenarios is, that you want to run clientside javascript. In that case, put ";return false;" after your javascript call:

<asp:LinkButton id="btnX" OnClientClick="functionToCall();return false;" runat="server"/>

2 thoughts on “Prevent an Asp.Net Button from triggering a Postback”

  1. Thanks for that. Looked at two previous sites only to be confused by the series of replies.

    You were unambiguous, gave an example and it worked first time,

    Best wishes, John M.

Comments are closed.

Prevent an Asp.Net Button from triggerin…

by Chris F Carroll read it in 1 min