How to toggle confirmation box from aspx.cs page

I have a search program that will search a database from a database. If the date range is more than 3 weeks, I want to warn them that it might take a while with all the data in the database. I have a confirmation box in a JavaScript function. I want to check the date range in the aspx.cs page. how can I disable the message box based on these criteria? here is a copy of my html code. I am not sure how to approach the checkpoint.

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


source to share


4 answers


If the button is a submit button, add it to onclick (). A true return value will allow the page to continue submitting, while a false will stop submitting (submit).

EDIT: try it before you say it isn't. It looks complicated, but quite simple. Get the first date, get the second date, compare them. If there is less than 3 weeks difference, return true and allow the page to be submitted (submit), otherwise alert the user and reply to the response.



Function...

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

      

0


source


A confirmation window will appear which opens when the button is pressed



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

      

+2


source


Why check the date range on the server side? If you have a client-side date range, it works significantly less.

Otherwise, you will need to postpay (or partially submit) and then check the date range and then issue a javascript warning to the page ...

Time to rethink your method.

0


source


You would be better off checking the date range on the client side and triggering the message box via JS before invoking the postback.

0


source







All Articles