Solved

Stop postback JavaScript

Posted on 2006-11-16
7
9,773 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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