使用WebMethod的警报框返回Message

问题描述 投票:0回答:1

我想在选定日期与日期时间选择器上的数据库日期匹配时显示警报消息框。

即使WebMethod工作正常,我的代码现在每次我选择时,警报消息总是在没有检查的情况下出现。

Test.aspx.cs

    [System.Web.Services.WebMethod]
     public static string GetDateFromDB(DateTime compareDate)  
    {
        string selectedDate = compareDate.ToString("yyyy/MM/dd");

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LoginDBConnectionString1"].ConnectionString);
        SqlCommand com = new SqlCommand("SELECT * from Holiday where Date='" + selectedDate + "'", conn);
        SqlDataAdapter sqlDa = new SqlDataAdapter(com);
        DataTable dt = new DataTable();
        sqlDa.Fill(dt);

        if (dt == null || dt.Rows.Count == 0)
             return "NG";               
        else
            return "OK";
    }

Test.aspx文件

      <link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
                <input type='text' class='date' id="datepicker" autocomplete="off">
                <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
                <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
                <script>
                    jQuery(function ($) {

                        $("#datepicker").datepicker({
                            onSelect: function (dateText) {                                    
                                alert("Selected date: " + dateText + "; input's current value: " + this.value);
                                $(this).change();
                                $.ajax({
                                    type: "POST",
                                    url: "Test.aspx/GetDateFromDB",
                                    data: '{ "compareDate" : "' + dateText + '"}',
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    //success: OnSuccess,
                                    //failure: function (response) {
                                    //    alert(response.d);
                                    //}
                                });
                            },
                          }
                        ).on("change", function () {
                            display("Got change event from field");
                        });

                    function display(msg) {
                        $("<p>").html(msg).appendTo(document.body);
                    }

                    });
                </script>
c# jquery asp.net datepicker webmethod
1个回答
1
投票

它始终发出警报,因为警报消息已进入onChange事件。您需要将此移至AJAX调用成功。以下是您的编辑代码。

编辑:根据后面代码的返回值检查条件(即“OK”和“NG”)。

 <link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
                <input type='text' class='date' id="datepicker" autocomplete="off">
                <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
                <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
                <script>
                    jQuery(function ($) {

                        $("#datepicker").datepicker({
                            onSelect: function (dateText) {                                    
                                $(this).change();
                                $.ajax({
                                    type: "POST",
                                    url: "Test.aspx/GetDateFromDB",
                                    data: '{ "compareDate" : "' + dateText + '"}',
                                    contentType: "application/json; charset=utf-8",
                                    dataType: "json",
                                    success: function(data)
                                             {
                                                 if(data == "OK")
                                                 {
                                                     alert("Selected date: " + dateText + "; input's current value: " + this.value);
                                                 }                                              
                                             },
                                    //failure: function (response) {
                                    //    alert(response.d);
                                    //}
                                });
                            },
                          }
                        ).on("change", function () {
                            display("Got change event from field");
                        });

                    function display(msg) {
                        $("<p>").html(msg).appendTo(document.body);
                    }

                    });
                </script>

注意:以上更改为非测试。如果不起作用,请写评论。

© www.soinside.com 2019 - 2024. All rights reserved.