It is more interesting from MSDN today.
Create an instance of user controls in programming
Just as you can create an instance of any ASP.NET server control on the web form page, you can also do this by using the LoadControl method containing the page. But you must first use the ClassName property of the @ Control instruction to associate strong types with the user control. The reason is to be because the loadControl method returns the type of the Control class, and you need to convert the user control to a suitable stronger type to set the various properties of the control.
The following code converts the user control saved in the MyUserControl.ascx file into a strong type using the classname property.
<% @ Control classname = "myuserControl"%> Note: This instruction can only be used in the user control. The user control is defined in a file with a .ascx extension. Each.ascx file can only contain an @ control instruction
Use the @ REFERENCE instruction to register the user control at the top of the web form page to which the user control is required. When creating user controls in programming, only the web form page can be used to use the user control to use the user control to the web form page after you create a reference to the control. For example, the following code creates a reference to the user control to the MyUserControl.ascx file. <% @Reference Control = "MyuserControl.ascx"%>
Note When an instance of a user control is created in a declaration method, use the @register directive. In the code hidden class file, or in the code declaration block containing the .aspx file, create an instance of the user control. Assign the property value as needed and add the control to the ControlCollection object containing the page using the Add method. This allows the control available for inherited Control.Controls properties of this page. In the following example, create the instance of MyUserControl.ascx and set its backcolor property to geige. [Visual Basic] DIM C1 AS UserControl = LoadControl ("MyuserControl.ascx") CType (C1, MyuserControl) .BackColor = "beige" Page.Controls.Add (C1)
[C #] Control C1 = LoadControl ("MyuserControl.ascx"); (MyuserControl) C1) .BackColor = "beige"; page.controls.add (c1);
Note These controls are placed in the collection when you add controls to the ControlCollection object using the Add method. If you want to add controls to a specific location in a collection, use the AddAT method and specify the index position you want to store the control.