Solved

How do I get my javacript (onclientclick) to stop the postback of my ASP.NET page?

Posted on 2009-07-01
3
1,211 Views
Last Modified: 2013-12-17
I have a javacript function associated with the OnClientClick of an ASP.Net button.  I also have the OnClick associated with a server-side function.  My OnClientClick code evaluates values on the page and returns true or false.  If I return false I don't want the page to postback. Even if I return false the page posts back.

Do I need to hand code the ASP.NET postback in the javacript and remove the OnClick of the ASP.NET button?
0
Comment
Question by:Bruce
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
j_s_kelley earned 500 total points
ID: 24754599
Make sure your return false in the actual control markup.  You can't do it in the javascript function.

<asp:Button OnClientClick="javascript:return CheckMyData();">< /asp:Button>

Send your code and I will look at it and try to tell where you need the return.
0
 
LVL 1

Author Closing Comment

by:Bruce
ID: 31598779
That was it!  Thanks for the quick response.
0
 
LVL 4

Expert Comment

by:j_s_kelley
ID: 24754757
Hey, got a better example for you.  In this example, we have a Server side button that closes a dialog window.  It is Server side so we can control how it is streamed down but don't care about the server responding to the click, we just want to close the window.  

<asp:Button ID="btnCancel" runat="server" class="button" Text="Cancel" OnClientClick="javascript:window.close();return false;" />

See how the return false; is in the actual markup of the button.  This will prevent a post back.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question