C# 使用WebMethod返回带有消息的警报框
我想在所选日期与日期时间选择器上的数据库日期匹配时显示一个警报消息框 尽管WebMethod运行良好,但我现在的代码是每次选择时都会显示的,警告消息总是在未经检查的情况下出现 Test.aspx.csC# 使用WebMethod返回带有消息的警报框,c#,jquery,asp.net,datepicker,webmethod,C#,Jquery,Asp.net,Datepicker,Webmethod,我想在所选日期与日期时间选择器上的数据库日期匹配时显示一个警报消息框 尽管WebMethod运行良好,但我现在的代码是每次选择时都会显示的,警告消息总是在未经检查的情况下出现 Test.aspx.cs [System.Web.Services.WebMethod] public static string GetDateFromDB(DateTime compareDate) { string selectedDate = compareDate.T
[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>
它总是发出警报,因为警报消息已进入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>
注:上述变更未经测试。如果警报不起作用,请写评论。警报应该在success函数中,而不是直接在change函数中。在success:function下,我确实更改了此代码,它工作得非常好。感谢您的帮助和专业意见。成功:函数响应{if response.d==OK{//alertSelected date:+dateText+;输入的当前值:+this.value;alertToday是公共假日!!!;}}如果有用且有效,则将其标记为答案。谢谢