asp.netgridviewimagebuttonscriptmanagerconfirm

Return value of ScriptManager.RegisterStartupScript confirm box in code behind


There are two confirm boxes on an ImageButton - first one is regular one saying "Are you sure you want to delete it?" (that is from the OnClientClick event on Client side ). If 'Yes' is selected, then it goes into the server side code, where another check happens and if that check is true, then the second confirm box is displayed from the code behind using ScriptManager.RegisterStartupScript.

Page page = HttpContext.Current.CurrentHandler as Page;
string script = "alert('There are Agents associated to this group. Are you sure you want to delete this group?')";
ScriptManager.RegisterStartupScript(page, page.GetType(), "its working", script, true);

My question is, how can I get to know the value of the second confirm box (which is generated in code behind) because with respect to that answer I have a delete the record.

Any suggestions?


Solution

  • After the user confirms delete (the 2nd time), you need to make a second round-trip to the server to pass the value server-side.

    On the form, you can add a hidden field to hold the result of the second confirm. If the hidden field was named "confirmDelete", then you could change your startup script like this:

    1. On the page, add a javascript function to prompt the user, get the response, put it into the hidden field and submit again:

      <script language="javascript">
      function CheckConfirm() {
         var conf = window.prompt('There are Agents associated to this group. Are you sure you want to delete this group?');
         document.forms[0].confirmDelete.value = conf; //or equiv in jquery
         document.forms[0].submit();  //or equiv in jquery
      }
      </script>
      <!-- don't forget to have a hidden field inside of the form -->
      <input type="hidden" name="confirmDelete" />
      
    2. Then change your startup script to call the (above) JS function:

      Page page = HttpContext.Current.CurrentHandler as Page;
      string script = "CheckConfirm()";
      ScriptManager.RegisterStartupScript(page, page.GetType(), "its working", script, true);