Can I dynamically add a UserControl to my WorkflowGen web form?

Background

In situations where you can’t insert a UserControl at design time in Visual Studio, since the UserControl is only needed depending on certain conditions that are only determined at runtime, it’s possible to dynamically add a UserControl to your WorkflowGen web form.

Solution

In Simple Mode, verify that:

  1. Each dynamically added UserControl object has a valid server-side ID assigned.

  2. Each dynamically added UserControl object is declared in the OnPreLoad() Page event.

For example, you need to insert a UserControl in a PlaceHolder (ID=PH1) control on your web form. In the OnPreLoad event:

protected override void OnPreLoad(EventArgs e)
 {
 UserControl myUC;
 myUC = (UserControl)Page.LoadControl("~/WebUserControl.ascx");
 myUC.ID = "UC1";
 PH1.Controls.Add(myUC);

 base.OnPreLoad(e);
 }

Once myUC is loaded with the UserControl ASCX file, we specify the object ID to be UC1. Then, we insert the UserControl to the PlaceHolder PH1.

Note that you need to call base.OnPreLoad(e); before the end of the override OnPreLoad event.

In Advanced Mode, the approach is very similar, except that you need to manually manage the WorkflowGen UserControl list, with the following requirements:

  • Each dynamically added UserControl object must have a valid server-side ID assigned.

  • Each dynamically added UserControl object must be declared at OnPreLoad() Page event.

  • Any UserControl (whether it is statically added at Design time or dynamically added at Runtime) must be registered in the WorkflowGen UserControl list.

  • Each column name in Table1 of WorkflowGen Page form data where any controls reside inside the UserControl must be prefixed with the UserControl ID (e.g. UC1.TextBox1 and not just TextBox1).

Similar to Simple Mode, you need to insert a UserControl in a PlaceHolder (ID=PH1) control in your web form. Under OnPreLoad event:

protected override void OnPreLoad(EventArgs e)
 {
 UserControl myUC;
 myUC = (UserControl)Page.LoadControl("~/WebUserControl.ascx");
 myUC.ID = "UC1";
 PH1.Controls.Add(myUC);

RegisterUserControlInPage("UC1");

 base.OnPreLoad(e);
 }

Note: After the declaration of the UserControl, you also need to register the UserControl by calling a custom method. This method has the following syntax:

private void RegisterUserControlInPage(string controlID)
 {
 List<string> myList;
 if (!IsSessionLess)
 { myList = Session["WFGEN_USER_CONTROLS_IN_PAGE"] as List<string>; }
 else
 { myList = ViewState["WFGEN_USER_CONTROLS_IN_PAGE"] as List<string>; }

 if (myList == null)
 {
 myList = new List<string>();
 myList.Add(controlID);
 }
 else
  {
 if (!myList.Contains(controlID))
 { myList.Add(controlID); }
 }
 if (!IsSessionLess)
 { Session["WFGEN_USER_CONTROLS_IN_PAGE"] = myList; }
 else
 { ViewState["WFGEN_USER_CONTROLS_IN_PAGE"] = myList; }
 }

WorkflowGen.My has an internal variable called WFGEN_USER_CONTROLS_IN_PAGE to keep track of all supported controls within the UserControl before it generates the FormData structure. This is why this registration must be performed during OnPreLoad and not during PageLoad. Also depending on IsSessionLess state, make sure your UserControl registration method covers both Session and ViewState for WFGEN_USER_CONTROLS_IN_PAGE.

Download the webforms_usercontrol_sample.zip file for a copy of this sample code.

Note: It is highly recommended to use Simple Mode to avoid any potential issues that could arise when using Advanced Mode, especially considering that the WorkflowGen.My class automatically handles these aspects for you.