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.
-
To create a new DevExpress control, call the control type constructor. To create a user control, call the LoadControl method.
-
Specify the control's
ID
property. -
Attach event handlers.
-
Insert the control into the control hierarchy.
-
Specify the control's properties.
-
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.
- LoadSingleWebControl.aspx.cs (VB: LoadSingleWebControl.aspx.vb)
- LoadWebUserControl.aspx.cs (VB: LoadWebUserControl.aspx.vb)
(you will be redirected to DevExpress.com to submit your response)