簡體   English   中英

如何從aspx.cs頁面切換確認消息框

[英]How to Toggle confirm message box from aspx.cs page

我有一個搜索程序,它將從數據庫中查看數據庫。 如果日期范圍超過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;
}

單擊該按鈕時,彈出確認對話框。

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?')");

為什么要在服務器端檢查日期范圍? 如果您在客戶端使用日期范圍,則其工作量會大大減少。

否則,您將需要后包裝(或部分發布),然后檢查日期范圍,然后將警告javascript渲染回頁面...

是時候重新考慮您的方法了。

在調用回發之前,最好在客戶端檢查日期范圍並通過JS觸發消息框。

如果該按鈕是一個提交按鈕,則將其添加到onclick()中。 返回值為true的值將允許頁面繼續提交,為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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM