Skip to content

Create a DevExpress control or user control dynamically at runtime

License

Notifications You must be signed in to change notification settings

DevExpress-Examples/asp-net-web-forms-create-controls-dynamically

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
DevExpressExampleBot
Oct 14, 2024
5770f5d · Oct 14, 2024

History

18 Commits
Oct 14, 2024
Jan 24, 2023
Jan 24, 2023
Apr 23, 2018
Apr 23, 2018
Apr 23, 2018
Jun 26, 2024
Apr 23, 2018

Repository files navigation

How to create ASP.NET Web Forms controls dynamically

This example shows how to create a DevExpress control or user control dynamically at runtime.

Follow the steps below to create a control in code.

  1. To create a new DevExpress control, call the control type constructor. To create a user control, call the LoadControl method.

  2. Specify the control's ID property.

  3. Attach event handlers.

  4. Insert the control into the control hierarchy.

  5. Specify the control's properties.

  6. Bind the control (for data-aware controls).

// Creates the DevExpress ASPxButton control
private void CreateControlProcedure(Control container) {
    ASPxButton btn = new ASPxButton();
    btn.ID = "btnRunTime";
    container.Controls.Add(btn);
    btn.Text = string.Format("This ASPxButton is created at RunTime once. ID = {0}", btn.ID);
}

// Creates a user control
private void LoadUserControl(string ucName) {
    Control control = LoadControl(ucName);
    control.ID = ucName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries)[0];
    control.EnableViewState = false;
    ASPxRoundPanel1.Controls.Clear();
    ASPxRoundPanel1.Controls.Add(control);
}

Note that once you have modified the control hierarchy (for instance, added a control to the control collection), it is necessary to restore this control with the same settings during the Page_Init stage.

Files to Review

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)