Solved

Stop postback JavaScript

Posted on 2006-11-16
7
9,768 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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

18 Experts available now in Live!

Get 1:1 Help Now