【问题标题】:Server side is not working when i put return in onclientclick当我在 onclientclick 中返回时,服务器端无法正常工作
【发布时间】:2016-04-30 17:06:45
【问题描述】:

请帮助我。我已经尝试了所有解决方案,但它不起作用。 当我输入 return success(); 时,OnclientClick 函数起作用;但 Onclick 不起作用。

我的 OnClientClick 函数:

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";



                    }
                }

            }
        });

    }

我的点击:

 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);

                    }

我试过这个Asp .NET Button - OnClientClick="return function()" vs OnClientClick="function()" 但是当我删除时返回 false。 OnclientClick 不工作。我还尝试禁用按钮OnclientClick and OnClick is not working at the same time?,但它仍然无法正常工作。

谁能帮帮我。我真的很绝望。已经好几天没有解决问题了。

【问题讨论】:

  • 您是否尝试过调试您的 onclick 方法?它会火吗?您也不需要阅读器来插入数据。更改 SqlDataReader readerProfile = commandUP.ExecuteReader();到 commandUP.ExecuteNonQuery();
  • 感谢您的帮助。我的 onclick 方法没有任何问题。当我禁用我的 OnClientClick 它工作正常。
  • 并非所有 javascript 函数都返回任何值。因此,如果您的值在那里,则 OnClientClick 为空,并且它不起作用。但是,您必须获得 true 值才能执行 OnClick 方法
  • 我的 javascript 会验证您是否要继续。如果是,它将停留在同一页面上并执行 OnClick。如果不是,它仍然会执行 onclick 并重定向到不同的页面。
  • 是否可以返回false,仍然执行OnClick方法?我的目标只是验证用户是否想继续。如果用户不想继续,则重定向到返回 true 的不同页面。但如果用户想继续,它将返回 false,因为它将取消 OnClientClick 并仍然运行 OnClick

标签: javascript asp.net onclick onclientclick


【解决方案1】:

OnClientClick = "返回成功();"应该可以工作,因为 onclientclick 如果没有发生错误,它将首先执行 javascript 函数,然后它将执行 OnClick() 方法。 并尝试此代码。您似乎传递了错误的参数名称 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"));

【讨论】:

  • does 参数是否有 @ 符号:现在在这里没关系,它们都有效。还!!!在添加答案之前,您应该阅读问题的 cmets,因为它也很有意义。
猜你喜欢
  • 1970-01-01
  • 2021-11-29
  • 1970-01-01
  • 2015-07-02
  • 2017-03-15
  • 2012-01-20
  • 2023-01-30
  • 2023-03-12
  • 2017-06-02
相关资源
最近更新 更多