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
Solved

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

Posted on 2009-03-30
22
1,165 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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
 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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 …
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

839 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