我有一个搜索程序,它将从数据库中查看数据库。如果日期范围超过3周,我想提醒他们,数据库中的所有数据可能需要一段时间。我在JavaScript函数中有一个确认消息框。我想检查aspx.cs页面中的日期范围。如何根据该条件移植消息框?这是我在html上的一些代码的副本。我不知道如何接近检查站。
function warning() {
var answer = confirm("The date range you have selected will return a substantial amount of data and will take some time to process.\n\nAre you sure you want to continue?");
if (answer)
return true;
else
return false;
}
答案 0 :(得分:2)
确认框继续,单击按钮时会弹出
SearchButton.Attributes.Add("onclick", "javascript:return " + "confirm('" +
"The date range you have selected will return a substantial amount of data and will take some time to process.\n\nAre you sure you want to continue?')");
答案 1 :(得分:0)
为什么检查服务器端的日期范围?如果你在客户端有日期范围,那么它的工作就会少得多。
否则,您需要进行postpack(或部分发布),然后检查日期范围,然后将警告javascript渲染回页面...
是时候重新考虑你的方法。
答案 2 :(得分:0)
最好在客户端查看日期范围,并在调用回发之前通过JS触发消息框。
答案 3 :(得分:0)
如果按钮是提交按钮,则将其添加到onclick()。真正的返回值将允许页面继续提交,而false将停止回发(提交)。
编辑:在你说它不对之前试试吧。它看起来很复杂但很简单。获取第一个日期,获取第二个日期,比较它们。如果差异少于3周,则返回true并允许页面回发(提交),否则提醒用户并返回他们的答案。
功能......
function warning() {
var ele;
var startDate;
var endDate;
var threeWeeksInMilliseconds = 1814400000; //1000 ms * 60 sec * 60 min * 24 hr * 21 days
//get starting value
ele = document.getElementById('txtStartDate');
if (ele == 'undefined'){
return false; //no start element
}
else {
try{
startDate = new Date(ele.value);
}
catch (e) {
return false;
}
}
//get the ending value
ele = document.getElementById('txtEndDate');
if (ele == 'undefined'){
return false; //no start element
}
else {
try{
endDate = new Date(ele.value);
}
catch (e) {
return false;
}
}
//getTime() returns milliseconds
if ((endDate.getTime() - startDate.getTime()) < threeWeeksInMilliseconds) {
return true;
}
//else present the message for confirmation.
var msg = "The date range you have selected will return a substantial " + "" +
"amount of data and will take some time to process.\n\n" +
"Are you sure you want to continue?";
var answer;
answer = confirm(msg);
if (answer) {
return true;
}
else {
return false;
}
//default return condition - nothing should get here so this indicates an error.
//Use true if you want to allow this to process.
return false;
}