views:

389

answers:

6

Hi All, is it possible to disable the submit behaviour of a server side button using Javascript? Note: I dont want to disable the button, the user will click the button but it will not submit/get any value from server.

scenario:

 <asp:Button ID="Button1" runat="server" Text="Submit"  onclick="Button1_Click"  OnClientClick="Check()" />

javascript:

enter code here  function Check()

{

                           var res = confirm(" Are u sure whatYou have selected ")
                if (res) 
                {
                    alert("yes");

                }
                else {
                    //Here I need to disable the submit behavoiur                        
                }

            }
        }

    }
+3  A: 

sure: return false from the click handler of the submit button.

Here Be Wolves
+7  A: 

You need to make sure that UseSubmitBehavior is set to false in the markup, then you need to make sure you return false before the postback javascript is called.


Edit:

You should return the value of your function, then in the function retrun true if you want a postback, and return false if you don't.

<asp:Button ID="Button1" runat="server" Text="Submit"  onclick="Button1_Click"  
  OnClientClick="return Check();" UseSubmitBehavior="false" />

Javascript:

function Check()
{
     return confirm(" Are u sure whatYou have selected ");
}
ck
With the updated code posted, your answer fully addresses the question +1
ichiban
+1  A: 
<input type='submit' onclick='return false;' />

Something like this (if it's not done in a different way in asp.net.

Ólafur Waage
A: 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script>
    function dostuff() {
    alert('hello');
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <% =DateTime.Now.ToString() %>
        <asp:Button ID="Mybutton" runat="server" UseSubmitBehavior="false" Text="Click"
        OnClientClick="dostuff(); return false;" />
    </div>
    </form>
</body>
</html>
SillyMonkey
A: 

I wouldn't recommend the other approaches as there are two problems with them:

  1. Javascript should not be embedded inside HTML. This is just bad form and it's an arcane practise by now. There's loads of discussion on this.
  2. Clicking the submit button is not the only way to submit a form. If the user hits the enter button you still have a problem because the onclick handler will not be triggered at all.

In your case, you just need to include the following in your else block:

document.getElementById("YourFormId").onsubmit = function() { return false; }

So the event is on your form not your button and it's an onsubmit event.

aleemb
The answers were getting upvoted because they work. The question was related to asp.net buttons performing a postback onclick. In ASP.NET, every button declared on the server side behaves like an HTML form submit button.
ichiban
The question clearly asks the user to verify his information before submitting. This should be handled in onsubmit rather than onclick. Markup should not include Javascript or CSS, wether you're using ASP.NET or not--these methods are arcane as I mentioned.
aleemb
A: 

just say return false, do it inside the client-click event, which will not proceed to submit (or __doPostBack() in asp.net)

Emre