Link to home
Start Free TrialLog in
Avatar of jpandviv
jpandviv

asked on

javascript Confirm dialog before going to next step in asp .net 2 wizard control

I'm trying to create a scenario where a user will have to answer a question before being able to continue to the next step on a asp .net 2 wizard control.  I have tried to hijack the __DoPostBack code which doesn't quite work.  The javascript confirm will appear, but no matter what they choose the wizard will continue to the next step.  What I need is to basically cancel the postback event when the user hits 'Cancel' in the box.  Is this possible?
This is code from my .js file.  It seems to hijack just fine, but I need to have it stop the submit:
 
 
var __oldDoPostBack;
var __formName='form1';
 
 
function hijackDoPostBack()
{
   try{
      __oldDoPostBack = __doPostBack;
      __doPostBack = runOnSubmit(); 
   }
   catch(ex){ 
        alert('hijackDoPostBack: error: ' + ex.toString());
      lastExceptionLocation = "hijackDoPostBack";
      lastException = ex;
   }
}
function runOnSubmit(eventTarget, eventArgument)
{
         
           
       if(CheckSessions()==true)
        {
            __oldDoPostBack(eventTarget, eventArgument);
        }
        else
        {
             //here is where it will end up if the user selects 'Cancel' from the confirm box
       }
 
}
 
function CheckSessions()
{      
    var bConfrim=true;
   //any onsubmit functions here
   if(!btnWhichButton || (btnWhichButton.value=='Add Sessions' || btnWhichButton.value==''))
    {
        bConfirm=false
    } 
    else 
    {
         bConfirm=confirm('Are these all of the session you want to have covered?  Click Ok to continue or Cancel to add more sessions.');
    }
   btnWhichButton='';
   CheckSesssions=bConfirm; 
}

Open in new window

Avatar of ChetOS82
ChetOS82
Flag of United States of America image

Why not, rather than hijacking the __doPostBack function, just define an onsubmit function for the form.  If that method returns false, then the submit is cancelled.

The ASP.net javascript chain supports what you are trying to do already.

<form ... onsubmit="return CheckSessions();">
ASKER CERTIFIED SOLUTION
Avatar of prairiedog
prairiedog
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jpandviv
jpandviv

ASKER

Thank you very much.  I guess I was making this issue much more difficult than it should have been.  Not too uncommon I suppose.  I ended up creating a hidden control that kept the value of the javascript confirm box and using a custom validator with a javascript procedure to test for the answer to the confirm.  Of course, I had to default the hidden control to 'true' so that it would only work for certain steps.  Thanks again.  I gave you an 'A' on this one.
Thank you for the points and the grade.