Solved

Stop postback JavaScript

Posted on 2006-11-16
7
9,776 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

627 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