Solved

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

Posted on 2009-07-01
3
1,208 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now