Solved

Stop postback JavaScript

Posted on 2006-11-16
7
9,772 Views
Last Modified: 2011-08-18

Doing some validation
function CheckText()
{
      var SleeveID = document.getElementById('txtD1').value; //SleeveID
      var PipeOD = document.getElementById('txtD2').value; //PipeOD
      var SmallSpace = document.getElementById('txtH').value; //Small Space
      if (SleeveID == "" || PipeOD == "" || SmallSpace == "" )
      {
            alert ('Please populate D1, D2 and H');
            return false;
            
      }
      }

ASP button
load event of the form
      Me.btnDtmSeal.Attributes.Add("onClick", "CheckText()")
The return false is not stopping postback, the programs afte going through the javascript and poping the alert still hits the click event of the button (code behind).
Why. why. why. cant I stop the postback?
 
0
Comment
Question by:Rickyc1
7 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 17957220
Hi Rickyc1,

      Me.btnDtmSeal.Attributes.Add("onClick", "return CheckText()")


Tim Cottee
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 17957228
Rickyc1,

The reason is that though you were returning false, you were not returning the false back to the onclick event handler itself.

Tim
0
 

Author Comment

by:Rickyc1
ID: 17957463
Tim that did it!!!
Tell me again why?
Why does adding the return mean so much?
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 6

Expert Comment

by:Roopesh_7
ID: 17957690
if u add return then only it dicide to postbsck or not....

return true means it will exicute javascript and then exicute the severside function....

return false means it will struck on javascript and not alowed to postback the page.....

0
 
LVL 43

Accepted Solution

by:
TimCottee earned 399 total points
ID: 17958174
If you return false to an event handler such as onclick, onkeypress etc. It has the effect of cancelling that event. In much the same way as setting Cancel=True in asp.net/vb.net event handler such as the query_closing event in windows forms. In your original script, you specified that the onclick event called a secondary function "CheckText()" and were effectively discarding the return value from this function. The return doesn't bubble up all the way to the calling event.

0
 

Expert Comment

by:sachin_chatekar
ID: 20505515
I'm doing the above said thing but from the ASPX page itself and not from the code behind.
I have a onclick handler in the code behind and in the ASPX page, to suppress the postback, I'm  adding OnClick = "rturn checkTest()"

But the run time gives error saying "return" is not part of ASPX.

Can anyone help me?

Suhas
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 20505908
Suhas: As you were not a contributor to this question, you really should have opened a new one however:

This depends on what version of asp.net you are using. If you are using asp.net 2 then you can use the onclientclick event in the declaration of the element:

<asp:button id="cmdDoSomething" OnClientClick="return checkTest();" text="Hit me" />

The OnClick event refers to the asp.net event handler not to the client event and cannot be used to add client-side functionality. All it can be used for is to define a server-side event that will be executed on postback.

If you are using asp.net 1.x then you cannot do this and have to add the attribute in code as shown above.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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