Stop a PostBack using JavaScript and the OnClientClick event

If you need to be able to update the text displayed in an ASP.net control such as a LinkButton through code-behind before the page is rendered but will end up using that control to invoke client-side javascript and don’t want a PostBack to happen you can simply add “return false;” after your custom JavaScript calls in the OnClientClick event handler of the control. “return false;” actually stops the page from posting back to the server and allows your custom JavaScript to be run client-side.

Example:
<asp:LinkButton ID="lbtnVisit" runat="server" Text="» Default Text" OnClientClick="CustomScriptMethod(); return false;">

8 thoughts on “Stop a PostBack using JavaScript and the OnClientClick event

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s