1

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!

0

2 Answers 2

1

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();
                }
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

0

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();"/> ?

2 Comments

I'm sorry sredne, but with this solution, I receive the error: 'jControllaProvvisorio' not defined
Oh sorry, I just saw that in your initial solution that 'jControllaProvvisorio' is declared inside the click handler, so it's only accessible when that event fires. If you wanted to do it with my solution you would need to extract the jControllaProvvisorio into document ready so it's accessible by the page.

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.