views:

1260

answers:

4

I have a search program that will be looking at a database from a database. If the date range is more than 3 weeks I want to alert them that it might take a while with all the data in the database. I have a confirm message box in a javascript funtion. I want to check the date range in the aspx.cs page. how do I totrigger the message box based on that criteria? here is a copy of some of my code on html. 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;
}
A: 

Why check the date range on the server side? If you have the date range on the client side, its considerably less work.

Otherwise, you will need to postpack (or partially post), and then check the date range, then render the warning javascript back out to the page...

Time to rethink your method.

FlySwat
Thanks, but how do you check the range on the html side?
A: 

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

Victor
the thing is I want to have the confirm button show up on a certain condition (that being if the date is more than 3 weeks), not everytime the search button is clicked. So what do i do? Sorry about the confusion
+2  A: 

A Confirmation Box to go ahead, pops up when the button is clicked

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

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

EDIT: Just try it before you say that its not right. It looks complicated but its pretty simple. Get the first date, Get the second date, compare them. If there is less than 3 weeks diff, return true and allow the page to post back (submit), else alert the user and return thier answer.

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