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

Published by

Tim Clark

Experienced Business Owner, Chief Information Officer, Vice President, Chief Software Architect, Application Architect, Project Manager, Software Developer, Senior Web Developer, Graphic Designer & 3D Modeler, University Instructor, University Program Chair, Academic Director. Specialties: Ruby, Ruby on Rails, JavaScript, JQuery, AJAX, Node.js, React.js, Angular.js, MySQL, PostgreSQL, MongoDB, SQL Server, Responsive Design, HTML5, XHTML, CSS3, C#, ASP.net, Project Management, System Design/Architecture, Web Design, Web Development, Adobe CS6 (Photoshop, Illustrator)

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

  1. nice tip. Helped me a lot. I was trying to stop postback from linkbutton if a field entry was not valid. This gave me the idea –

    OnClientClick=”return (ValidateInput())”
    // ValidateInput must return true/false. Of course true will do the postback.

    Like

  2. This would not work… bcoz this renders as … so whether u return true or false it would always return and would never go to __doPostBack. I faced this for long and realized that the correct way was onClientClick= “if (!ValidateInput()) return false”
    This would return only when ValidateInput returns false else it would go to the next line i.e __doPostBack…

    Here check this http://deeps-space.blogspot.com/2010/05/stopping-postback-on-client-side-with.html

    Like

Leave a comment