Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Scroll 5 news at a time. 4 27
How do I pull the base url for use in html links 7 23
Index on a Table 6 12
SQL Query Help Top 1 and Distinct? 6 23
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…

770 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