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

**URL:** <https://discuss.workflowgen.com/t/can-i-dynamically-add-a-usercontrol-to-my-workflowgen-web-form/184>\
**Category:** Visual Studio Web Forms\
**Created:** [October 13, 2009, 4:51am UTC](https://discuss.workflowgen.com/t/can-i-dynamically-add-a-usercontrol-to-my-workflowgen-web-form/184 "2009-10-13T04:51:00Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![wfg-admin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discuss.workflowgen.com/wfg-admin/32/714_2.png) [@wfg-admin](https://discuss.workflowgen.com/u/wfg-admin)\
**Post date:** [October 13, 2009, 4:51am UTC](https://discuss.workflowgen.com/t/can-i-dynamically-add-a-usercontrol-to-my-workflowgen-web-form/184/1 "2009-10-13T04:51:00Z")

</div>

**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](https://docs.advantys.com/workflowgen-web-forms-for-visual-studio-developer/web-form-development-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:

```auto
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](https://docs.advantys.com/workflowgen-web-forms-for-visual-studio-developer/web-form-development-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:

```auto
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:

```auto
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](https://advantys.box.com/s/fv2r48zcpe3clibej5tir5uygqo1hjje) 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.
