Problem to show alert box when click submit button

In the code behind, I have the code below when pressing the submit button to show the alert box.

ScriptManager.RegisterStartupScript(this, GetType(), “alertMessage”, “alert(‘Message to user!!!’);”, true);
this.SaveFormData(this.FormData, true);
SubmitToWorkflow();

The problem is the alert box will not be shown and continue finish the workflow when pressing the submit button.
But if I remove “SubmitToWorkflow()” from the code above, the alert box can be shown.

Is there any way to show alert box before it run “SubmitToWorkflow()” when pressing the submit button?

If you are using FormDesigner, the default page validation at submit button event is managed by WorkflowGen page and you cannot interrupt the submission.

You need to override WorkflowGen page validation by doing the following:

    //  turn off the HandleSubmitButton flag in the OnPreLoad event, this is the only place where it is allowed.
	protected override void OnPreLoad(EventArgs e)
	{
	    // Turn off before calling base.OnPreLoad(e)
	    HandleSubmitButton = false;
	    
	    base.OnPreLoad(e);
	    
	    // Add your custom events to the submit button
	    submitButton.Click += new EventHandler(MySubmitButton_Click);
	    
	    // Add the validation group constant (Done by WorkflowGen when HandleSubmitButton is true)
	    // It is required in order to have the correct WorkflowGen Behaviour with the required fields.
        submitButton.ValidationGroup = "WFGENPage";
	}

Then you can have your override MySubmitButton_Click event to show the alert and stop submission

protected void MySubmitButton_Click(object sender, EventArgs e)
    {

        //-- this happens after the submit button is clicked. The validate message and the javascript will appear in the next action
       if (some_condition_does_not_match)
       {
               ClientScript.RegisterClientScriptBlock(this.GetType(), "interrupt", "alert('your alert message');", true);
       }
       else
       {
           // Submit to workflowgen
	        SubmitToWorkflow();
       }
       

     }

Hi @kevin.lee,

Yeah, I’m using the FormDesigner.

I do have override the WorkflowGen page validation:

protected override void OnPreLoad(EventArgs e)
{
        IsSessionLess = true;
        
        HandleSubmitButton = false;

        submitButton.Click += new EventHandler(testSubmitButton_Click);
        submitButton.ValidationGroup = "WFGENPage";
        
        base.OnPreLoad(e);
}

But the problem is when I using the code below, the alert box did not show up at all after click the submit button.

protected void testSubmitButton_Click(object sender, EventArgs e)
{
	    if(condition == 1)
	    {
	        ScriptManager.RegisterStartupScript(this, GetType(), "a1", "alert('Condition 1!!!');", true);
	    }
	    else if(condition == 2)
	    {
	        ScriptManager.RegisterStartupScript(this, GetType(), "a1", "alert('Condition 2!!!');", true);
	    }
	    
	    this.SaveFormData(this.FormData, true);
	    SubmitToWorkflow();
}

If I remove the “SubmitToWorkflow();” code, the alert box is able to display after click the submit button.

protected void testSubmitButton_Click(object sender, EventArgs e)
{
	    if(condition == 1)
	    {
	        ScriptManager.RegisterStartupScript(this, GetType(), "a1", "alert('Condition 1!!!');", true);
	    }
	    else if(condition == 2)
	    {
	        ScriptManager.RegisterStartupScript(this, GetType(), "a1", "alert('Condition 2!!!');", true);
	    }
	    
	    this.SaveFormData(this.FormData, true);
	   // SubmitToWorkflow();
}

I would like to know if there any possible method to solve the problem above.

Hi,

You should have your

this.SaveFormData(this.FormData, true);
SubmitToWorkflow();

inside your if statement. In your code regardless of what situation it will run submittoWorkflow() meaning WorkflowGen page will not return back to you page and submit back to WFG engine.

The logic is if you interrupt at submission your if statement is to control when to call submittoworkflow().

So you should have

protected void testSubmitButton_Click(object sender, EventArgs e)
{
	    if(condition == 1)
	    {
	        ScriptManager.RegisterStartupScript(this, GetType(), "a1", "alert('Condition 1!!!');", true);
	    }
	    else if(condition == 2)
	    {
	        ScriptManager.RegisterStartupScript(this, GetType(), "a1", "alert('Condition 2!!!');", true);
	    }
	    else
        {
	       this.SaveFormData(this.FormData, true);
	       SubmitToWorkflow();
        }
}

Hi @kevin.lee,

Thanks for your explanations.

For my situation, SubmitToWorkflow() will always run regardless what condition it is.

I just want to know is there any other possible method so that it will pop up message base on condition, then it will straight complete that workflow.

Hi @wing_hou just want to be clear. You said

“For my situation, SubmitToWorkflow() will always run regardless what condition it is.”

Do you mean you need the SubmitToWorkflow() to always run, i.e. you want the javascript alert but still submit right after?

If this is the case, you have to do a client side trigger to your javascript alert (on client click on the submit button to run the javascript and show the prompt) and followed by the normal WFG submission.

Once you call SubmitToWorkflow() in code behind basically it means WFG page will not render the page on screen but redirect the page back to the workflow and leave/complete the eformASPX application.

Hi @kevin.lee ,

Thanks for your advise.
Your understanding is correct.

Client side trigger means using the code below to call the JavaScript alert, is it?

RegisterClientScriptBlock(Page, Type, String, String, Boolean)

Sorry @wing_hou I missed your reply.
Here is an example to add validation rule so that it will avoid WorkflowGen from submitting the form. It will be added to be part of the WorkflowGen validation summary

protected void Page_Load(object sender, EventArgs e)
     {
          base.Page_Load(sender, e);

        //***************** this is the section to create a client side custom validator with javascript function that will append to WFG validation **************
        
        //----- create custom validator in runtime for the checkboxlist 
        System.Web.UI.WebControls.CustomValidator cv = new System.Web.UI.WebControls.CustomValidator(); 
        cv.Display = ValidatorDisplay.None; 
        cv.ValidationGroup = "WFGENPage"; 
        cv.ErrorMessage = "One or more services must be selected in order to complete the form."; 
        cv.Enabled = true; 
        cv.SetFocusOnError = true; 
        cv.ID = "cvCheckBoxList"; 
        cv.ClientValidationFunction = "ValidateCheckBoxList"; 
        
        //--- compose client validation script for the CheckBoxList 
        System.Text.StringBuilder sbValidateCheckBoxList = new StringBuilder(); 
        
        
        //---- this is a validation javascript method to be used by the custom validator
        sbValidateCheckBoxList.Append( "function ValidateCheckBoxList(source, arguments) { \r " ); 
        sbValidateCheckBoxList.Append( "var ValidateResult = true; \r "); 
        sbValidateCheckBoxList.Append( "ValidateResult = check_checkboxes(); \r "); 
        sbValidateCheckBoxList.Append( "arguments.IsValid = ValidateResult; \r "); 
        sbValidateCheckBoxList.Append( "} \r "); 
        
        //---- this is a sample to loop through a checkboxlist control and check if at least 1 item is checked
        sbValidateCheckBoxList.Append( "function check_checkboxes() { \r " ); 
        sbValidateCheckBoxList.Append( "  var isChecked = false; \r " ); 
        sbValidateCheckBoxList.Append( "  var c = document.getElementById('" + REQUEST_SERVICES.ClientID + "'); \r " ); 
        sbValidateCheckBoxList.Append( "  var cArray = c.getElementsByTagName('input'); \r " ); 
        sbValidateCheckBoxList.Append( "  for (var i = 0; i < cArray.length; i++) { \r " ); 
        sbValidateCheckBoxList.Append( "      var cRef = cArray[i]; \r "); 
        sbValidateCheckBoxList.Append( "      if (cRef.checked) {  isChecked = true; \r break; \r      } \r " ); 
        sbValidateCheckBoxList.Append( "  } \r "); 
        
        sbValidateCheckBoxList.Append( "  return isChecked; \r " ); 
        sbValidateCheckBoxList.Append( "} \r "); 
        
        
        //---- add validator to the page form 
        Page.ClientScript.RegisterClientScriptBlock(typeof(string), "ValidateCheckBoxList", sbValidateCheckBoxList.ToString(), true); 
        Page.Form.Controls.Add(cv); 
        
    }

If you have AJAX enabled you can use ScriptManager.RegisterClientScriptBlock instead of Page.ClientScript.RegisterClientScriptBlock