user1
user1

Reputation: 586

Stop Asp.net page execution with jQuery

I would like to know how can I stop execution of code behind in an aspx page when a jquery function fires:

$(document).ready(function () {
            $("#btnSaveProvvisorio").click(function jControllaProvvisorio () {
                var i;
                var jElemento;
                $('.MyTextBoxClass').each(function (i, v) {
                    jElemento = $(this).val();
                    if (jElemento) {
                        if (isNaN(jElemento)) {
                            alert("Warning: " + jElemento + " is not a number!");
                            return false;
                        }
                        else
                            if (parseInt(jElemento) <= 0){
                                alert("Warning: " + jElemento + " is negative!");
                        return false;
                    }
                    }
                });
            });
        });

How can I set the button:

<asp:Button ID="btnSaveProvvisorio" runat="server" style="text-align: center" Text="Salva provvisorio" OnClick="btnSaveProvvisorio_Click" BackColor="#FF6600" ClientIDMode="Static" OnClientClick="jControllaProvvisorio ()"/> ?

This version gives me an error; without the OnClientClick tag the page aspx.cs doesn't stop anyway. Where is the error? Thanks!

Upvotes: 1

Views: 768

Answers (2)

sredne
sredne

Reputation: 9

Make sure you're returning the function you call in "OnClientClick". So you're button should be like:

<asp:Button ID="btnSaveProvvisorio" runat="server" style="text-align: center" Text="Salva provvisorio" OnClick="btnSaveProvvisorio_Click" BackColor="#FF6600" ClientIDMode="Static" OnClientClick="return jControllaProvvisorio();"/> ?

Upvotes: 0

Code Maverick
Code Maverick

Reputation: 20425

For your button, you don't need to assign the OnClientClick since you can attach the click event via jQuery. That means your button would look like:

<asp:Button ID="btnSaveProvvisorio" runat="server" 
            style="text-align: center" 
            Text="Salva provvisorio"
            OnClick="btnSaveProvvisorio_Click" 
            BackColor="#FF6600" 
            ClientIDMode="Static" />

In your jQuery click event, you need to pass in the event parameter to your function and then use e.preventDefault() to stop the server-side execution. To accomplish that, your jQuery would then look like this:

$(document).ready(function () 
{
    $("#btnSaveProvvisorio").on("click", function (e) 
    {
        var i;
        var jElemento;

        $('.MyTextBoxClass').each(function (i, v) 
        {
            jElemento = $(this).val();
            if (jElemento) 
            {
                if (isNaN(jElemento)) 
                {
                    alert("Warning: " + jElemento + " is not a number!");

                    // prevent ASP.NET server-side click from occurring.
                    e.preventDefault();
                }
                else if (parseInt(jElemento) <= 0)
                {
                    alert("Warning: " + jElemento + " is negative!");

                    // prevent ASP.NET server-side click from occurring.
                    e.preventDefault();
                }
            }
        });
    });
});

Upvotes: 1

Related Questions