0
votes

Please help me. I have tried all solutions and it doesn't work. OnclientClick function works when I put return success(); but Onclick does not work.

My OnClientClick function:

function success() {

        var no = document.getElementById('<%= tbcNo.ClientID %>').value;
        var qty = document.getElementById('<%= tbquantity.ClientID %>').value;
        var rbVanilla = document.getElementById('<%= rbVanilla.ClientID %>').checked;
        var rbPandan = document.getElementById('<%= rbPandan.ClientID %>').checked;
        var rbMocha = document.getElementById('<%= rbMocha.ClientID %>').checked;
        var rbUbe = document.getElementById('<%= rbUbe.ClientID %>').checked;
        var rbS1 = document.getElementById('<%= rbS1.ClientID %>').checked;
        var rbS2 = document.getElementById('<%= rbS2.ClientID %>').checked;
        var rbS3 = document.getElementById('<%= rbS3.ClientID %>').checked;

        if (!no) {
            bootbox.dialog({
                message: "Please input your phone number!",
                title: "Warning",
                buttons: {
                    success: {
                        label: "OK",
                        className: "btn-success",
                        callback: function () {


                        }
                    }

                }
            });
        }
        else if (isNaN(no)) {
            bootbox.dialog({
                message: "Please input a valid number!",
                title: "Warning",
                buttons: {
                    success: {
                        label: "Ok",
                        className: "btn-success",
                        callback: function () {

                            return true;
                        }
                    }

                }
            });
        }
        else if (no.length < 11) {
            bootbox.dialog({
                message: "Phone number must be 11 digits!",
                title: "Warning",
                buttons: {
                    success: {
                        label: "OK",
                        className: "btn-success",
                        callback: function () {

                            return true;
                        }
                    }

                }
            });
        }
        else if (no) {
            if (!qty) {
                bootbox.dialog({
                    message: "Please enter a quantity for your order.",
                    title: "Warning",
                    buttons: {
                        success: {
                            label: "OK",
                            className: "btn-success",
                            callback: function () {

                                return true;
                            }
                        }

                    }
                });
            }

            else if (qty) {
                if (rbVanilla && rbS1) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Vanilla" +
                            '<br />'+
                        'Puto Cake Size: Small' +
                        '<br />'+
                        'Price: 250 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 250 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {

                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {


                                }
                            }

                        }
                    });

                    return false;

                }
                else if (rbMocha && rbS1) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Mocha" +
                            '<br />' +
                        'Puto Cake Size: Small' +
                        '<br />' +
                        'Price: 250 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 250 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbPandan && rbS1) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Pandan" +
                            '<br />' +
                        'Puto Cake Size: Small' +
                        '<br />' +
                        'Price: 250 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 250 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbUbe && rbS1) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Ube" +
                            '<br />' +
                        'Puto Cake Size: Small' +
                        '<br />' +
                        'Price: 250 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 250 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }

                //S2

                else if (rbVanilla && rbS2) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Vanilla" +
                            '<br />' +
                        'Puto Cake Size: Medium' +
                        '<br />' +
                        'Price: 350 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 350 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbMocha && rbS2) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Mocha" +
                            '<br />' +
                        'Puto Cake Size: Medium' +
                        '<br />' +
                        'Price: 350 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 350 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbPandan && rbS2) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Pandan" +
                            '<br />' +
                        'Puto Cake Size: Medium' +
                        '<br />' +
                        'Price: 350 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 350 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbUbe && rbS2) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Ube" +
                            '<br />' +
                        'Puto Cake Size: Medium' +
                        '<br />' +
                        'Price: 350 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 350 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }

                // S3

                else if (rbVanilla && rbS3) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Vanilla" +
                            '<br />' +
                        'Puto Cake Size: Large' +
                        '<br />' +
                        'Price: 450 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 450 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbMocha && rbS3) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Mocha" +
                            '<br />' +
                        'Puto Cake Size: Large' +
                        '<br />' +
                        'Price: 450 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 450 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbPandan && rbS2) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Pandan" +
                            '<br />' +
                        'Puto Cake Size: Large' +
                        '<br />' +
                        'Price: 450 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 450 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }
                else if (rbUbe && rbS2) {
                    bootbox.dialog({
                        message: '<div style="text-align:center;">' +
                            "Puto Cake Flavor: Ube" +
                            '<br />' +
                        'Puto Cake Size: Large' +
                        '<br />' +
                        'Price: 450 Pesos' +
                        '<br/>' +
                        'Quantity: ' + qty +
                        '<br />' +
                        'Total Amount: ' + qty * 450 +
                        '<br/>' +
                        'We will call you at ' + '<b>' + no + '</b>' +
                        '<div>',
                        title: "Order Summary",
                        buttons: {
                            success: {
                                label: "That's Correct",
                                className: "btn-success",
                                callback: function () {
                                    done();

                                }
                            },
                            danger: {
                                label: "That's Incorrect",
                                className: "btn-danger",
                                callback: function () {

                                    return true;
                                }
                            }

                        }
                    });


                    return false;
                }


            }
        }

        else{
                bootbox.dialog({
                    message: "The company will contact you shortly to further discuss the order that you made. Thank you for choosing Baby's Puto Cake!",
                    title: "Information",
                    buttons: {
                        success: {
                            label: "OK",
                            className: "btn-success",
                            callback: function () {

                                window.location = "Main.aspx";
                                document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC";

                                return true;

                            }
                        }


                    }
                });
            }
        return false;
        }



    function done() {
        bootbox.dialog({
            message: "Would you like to process another order?",
            title: "Order Verification",
            buttons: {
                success: {
                    label: "Yes",
                    className: "btn-success",
                    callback: function () {



                    }
                },
                danger: {
                    label: "No",
                    className: "btn-danger",
                    callback: function () {


                        done1();


                    }
                }

            }
        });



    }

    function done1() {
        bootbox.dialog({
            message: "The company will contact you shortly to further discuss the order that you made. Thank you for choosing Baby's Puto Cake!",
            title: "Information",
            buttons: {
                success: {
                    label: "OK",
                    className: "btn-success",
                    callback: function () {

                        window.location = "Main.aspx";
                        document.cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC";



                    }
                }

            }
        });

    }

My OnClick:

 SqlConnection conn = new SqlConnection(sConn);
                String query = "Insert into orders values (@name,@cNo, @flavor, @size)";

                    if (rbVanilla.Checked == true && rbS1.Checked == true)
                    {



                        SqlCommand commandUP = new SqlCommand(query, conn);

                        commandUP.Parameters.Add(new SqlParameter("name", Request.Cookies["username"].Value));
                        commandUP.Parameters.Add(new SqlParameter("cNo", tbcNo.Text));
                        commandUP.Parameters.Add(new SqlParameter("flavor", "Vanilla"));
                        commandUP.Parameters.Add(new SqlParameter("size", "12'dios"));

                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "successful", "success();", true);

                        conn.Open();
                        SqlDataReader readerProfile = commandUP.ExecuteReader();

                        conn.Close();

                        //ClientScript.RegisterStartupScript(GetType(), "success", "test();", true);

                    }

I tried this Asp .NET Button - OnClientClick="return function()" vs OnClientClick="function()" but when i removed return false. OnclientClick is not working. I also tried disabling the button OnclientClick and OnClick is not working at the same time? and it is still not working.

Can anyone help me. I'm really desperate. It's been days now without resolution.

1
have you tried to debug your onclick method? Does it fires? also you don't need a reader to insert data. Change SqlDataReader readerProfile = commandUP.ExecuteReader(); to commandUP.ExecuteNonQuery();Khazratbek
Thank you for your help. There is nothing wrong with my onclick method. when i disable my OnClientClick it works fine.vaughn ellis ramos
Not all of your javascript functions return any value. So if your value goes there, OnClientClick is null, and it does not work. However, you have to get true value in order to OnClick method be executedKhazratbek
my javascript validates if you want to continue. if yes, it will stay on the same page and execute the OnClick. if no, it will still execute the onclick and redirect to a different page.vaughn ellis ramos
Is it possible to return false and still execute the OnClick method? my goal here is just to validate if the user wants to continue. if the user does not wants to continue then redirect to a different page which is the return true. but if user wants to continue it will be return false because it will cancel the OnClientClick and still run the OnClickvaughn ellis ramos

1 Answers

-1
votes

OnClientClick = "return success();" should work because onclientclick it will execute the javascript function first if no error occurs then it will execute OnClick() method. and also try this code. you seem to have passing the wrong parameter name commandUP.Parameters.Add(new SqlParameter("@name", Request.Cookies["username"].Value)); commandUP.Parameters.Add(new SqlParameter("@cNo", tbcNo.Text)); commandUP.Parameters.Add(new SqlParameter("@flavor", "Vanilla")); commandUP.Parameters.Add(new SqlParameter("@size", "12'dios"));