Skip to content

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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

About

Create a DevExpress control or user control dynamically at runtime

Topics

Resources

License

Stars

Watchers

Forks