3

I have a button:

<asp:Button
    runat="server"
    ID="btNew"
    OnClick="ButtonNew_Click"
    Text="New" />

I have the OnClick method:

protected void ButtonNew_Click(object sender, EventArgs e)
{

}

On the same page (.aspx), I have a JavaScript function:

function MyCheck()
{
    // Return true or false
}

I'd like when I click on the button to execute the JavaScript function (MyCheck()). If true, execute ButtonNew_Click(). If false, don't execute ButtonNew_Click().

Any ideas?

4 Answers 4

5

Use:

function clientfunction()
{
    // Do the client side validations here.

    // Now call the server side button event explicitly
    __doPostBack('OnClick','button_ID');
}

In button onclientclick, call the function clientfunction():

<asp:Button runat="server" ID="btNew" OnClick="ButtonNew_Click" Text="New" onclientclick="clientfunction()"/>
Sign up to request clarification or add additional context in comments.

1 Comment

You can just do return true or false, no need to call __doPostBack
-1

USE OnClientClick

OnClientClick=" MyCheck"

if your method will return false then ButtonNew_Click will not get called.

Comments

-1

Try this:

btNew.Attributes.Add("onclick", "return MyCheck();");

3 Comments

That's call the javascript function but I'd like when the method return false NOT execute ButtonNew_Click
then u need to do something in javascript code ... after checking the conditions call method from javascript
it's btNew.Attributes.Add("onclick", "return MyCheck();"); edit your answer
-1

Server-side code:

using System.Web.UI;
using System.Web.UI.WebControls;

namespace count
{
    public partial class WebForm1 : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "button clicked";
        }
    }
}

Client side:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs"      Inherits="count.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script type="text/javascript">
            function aa() {
                return true;
            }            
        </script>
    </head>

    <body>
        <form id="form1" runat="server">            
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick="return aa()" Text="Button" />
            <p>
                <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </p>
        </form>
    </body>
</html>

If in function aa() JavaScript you will return false; it will not execute server-side code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.