Solved

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

Posted on 2009-07-01
3
1,207 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
Comment Utility
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
Comment Utility
That was it!  Thanks for the quick response.
0
 
LVL 4

Expert Comment

by:j_s_kelley
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now