Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 9795
  • Last Modified:

Stop postback JavaScript


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
Rickyc1
Asked:
Rickyc1
1 Solution
 
TimCotteeCommented:
Hi Rickyc1,

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


Tim Cottee
0
 
TimCotteeCommented:
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
 
Rickyc1Author Commented:
Tim that did it!!!
Tell me again why?
Why does adding the return mean so much?
0
Independent Software Vendors: 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!

 
Roopesh_7Commented:
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
 
TimCotteeCommented:
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
 
sachin_chatekarCommented:
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
 
TimCotteeCommented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now