Solved

asp.net 2.0 button with return false javascript STILL POSTING BACK!!!

Posted on 2009-03-30
22
1,136 Views
Last Modified: 2012-05-06
I have 4 hours in this and I don't get it. I have tried every example. Is there not a way to stop post the form by returning false from javascript?  even this doesn't work...
function thisFunc()
{  return false; }
It is executing but does not stop the post. Code below
Server side: 

 Button1.OnClientClick = "return doPost();"; 
 

Client side:

 <script type="text/javascript">

     	 function doPost()

	   {

           	         return false      

                          }

Open in new window

0
Comment
Question by:ecsmoore
  • 11
  • 11
22 Comments
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24024453
Have you set the asp button postback property to false?

<asp:button postback="false"/>
0
 

Author Comment

by:ecsmoore
ID: 24024673
Not sure what you mean? There is not Postback property. In some .NET controls there is Autopostback but not on the button.  ???
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24024729
Sorry, Try this.

Its using an ASP button and should work better for you.


<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <script language=javascript type="text/javascript">

        function myOnClick() {

            //perform some other actions...

            return false;

        }

    </script>

    <title>Untitled Page</title>

</head>

<body bgcolor="buttonface" bottommargin="0" topmargin="0">

    <form id="form1" runat="server">

    <div>

<asp:Button ID="btnyours" runat="server" Text="Work Darnit" OnClientClick="return myOnClick()" />

    </div>

    </form>

</body>

</html>

Open in new window

0
 

Author Comment

by:ecsmoore
ID: 24024759
This is what is so strange. It is exactly how I started this out and it did not work. I have tried this and many other implementations and nothing stops the postback.  Could it have someting to do with Masterpages, which is another piece to this environment?
I tried just as you have demonstrated with no code in javascript other than return = false; and it still posts back.  Any idea what I have wrong?
0
 

Author Comment

by:ecsmoore
ID: 24024799
I just made a new page, fresh out of the box with your suggestion, and added this code behind and it posts back:
Web page (your code)
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <script language=javascript type="text/javascript">
        function myOnClick() {
            //perform some other actions...
            return false;
        }
    </script>
    <title>Untitled Page</title>
</head>
<body bgcolor="buttonface" bottommargin="0" topmargin="0">
    <form id="form1" runat="server">
    <div>
<asp:Button ID="btnyours" runat="server" Text="Work Darnit" OnClientClick="return myOnClick()" />
    </div>
    </form>
</body>
</html>

Code behind:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class NoPostback : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
            Response.Write("not fixed yet");
        }
       
    }
}
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24024811
Im not sure. What is the code behind the button?

Have you tried to also add onclick="return false" along with what I posted above?
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24024873
It works for me...

One minor thing.

Change script language=javascript  to script language="javascript"
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24024913
Here is the working code in C#
<%@ Page Language="C#" %>
 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<script runat="server">
 

    protected void btnyours_Click(object sender, EventArgs e)

    {

        if (Page.IsPostBack)

        {

            Response.Write("not fixed yet");

        }

    }

</script>

    <script language="javascript" type="text/javascript">

        function myOnClick() {

            //perform some other actions...

            return false;

        }

    </script>
 

<html xmlns="http://www.w3.org/1999/xhtml" >

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:Button ID="btnyours" runat="server" Text="Work Darnit" OnClientClick="return myOnClick()" OnClick="btnyours_Click"/>
 

    </div>

    </form>

</body>

</html>

Open in new window

0
 

Author Comment

by:ecsmoore
ID: 24024931
this is crazy... the code above... copied directly in to my VS 2008.  It posts pack and displays
"not working yet".

You are saying it does not do this on your machine?
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24024946
thats correct....i removed the onclick function from the button and tested it again, sure enough i saw "not fixed yet", i added the function back and tried it again. No text....

that is strange.

what browser are you testing in.
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24024958
Ive tried this in both VWD 2005 and 2008 and it works perfectly
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:ecsmoore
ID: 24024977
IE 7.. GOING TO TRY FF now
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24025003
Just as an FTI you may need to enable javascript for IE 7...I read its turned off by default
0
 

Author Comment

by:ecsmoore
ID: 24025017
I will check that but I see it is working and stepping thru just fine. It is just ignoring the return = false:

Your code works fine in FF
0
 

Author Comment

by:ecsmoore
ID: 24025052
javascript is enabled in my IE7 environment .... don't get it.
0
 

Author Comment

by:ecsmoore
ID: 24025061
well I just did a search on IE7 and this problem... Haven't read it yet but here is the first item in the list
http://forums.asp.net/t/1161858.aspx
0
 
LVL 6

Accepted Solution

by:
ITHelper80 earned 500 total points
ID: 24025073
Someone said here was the fix.

<asp:Button ID="btnDELETE" runat="server" OnClick="btnDELETE_Click" Text="Delete" OnClientClick="if(confirm('Selected records will be deleted?')==false){event.returnValue=false;return false;}else{return true;}" />

Please let us know if you get it fixed...Im sure I will see this again in the future.
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24025092
Oh just to make it even more strange I am running IE7 too....=)
0
 

Author Comment

by:ecsmoore
ID: 24025165
Yes... I just read a few blogs and big argument cuz it works on about 50% of situations so the ones it works on, of course, say to the others, "Yes it does work"  blah blah....
Going to try that fix now.
0
 

Author Comment

by:ecsmoore
ID: 24025181
Honestly, I have been on this since 1PM with a break for dinner. This solution works.
0
 

Author Comment

by:ecsmoore
ID: 24025186
Thanks alot for hanging in there with me. I was getting pretty frustrated.
0
 
LVL 6

Expert Comment

by:ITHelper80
ID: 24025197
NP, glad we could get it going. I know how frustrating this stuff can get.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

12 Experts available now in Live!

Get 1:1 Help Now