Introduction
User control are powerful, they can divide the code and helps us to reuse them. Normally we can use the UserControl’s in two ways:
- Placing the user control by dragging and dropping it to a page at the design time: If you drag and drop the user control, it adds following two important lines to your code: <%@ Register TagPrefix=”uc1″ TagName=”HeaderUserControl” Src=”example.ascx” %> This includes the example user control from the example.ascx. The example control is then positioned on the page using the following tag:
- <uc1:ExampleUserControl id="Example1" runat="server"></uc1:ExampleUserControl>
- Other is, loading them dynamically when ever needed:
Some times you may need to load the user control at runtime or dynamically. For this purpose there is a method called LoadControl. This function has a straightforward syntax - it takes a single argument - the virtual path to the user control page.In this tutorial I will try to explain in detail about loading user controls dynamically.
Loading the user controls is simple but, it is not easy to make the dynamically loaded controls working properly, if we missed some key points. So, in this tutorial, I will try to explain the things by dividing it into following sections:
- Loading the user Controls Dynamically
- Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need
- Accessing the properties of the dynamically loaded User Control
Loading the user Controls Dynamically
As I said, there is method called LoadControl(), which loads the usercontrols dynamically as follows:
- ExampleUserControl uc = LoadControl("ExampleUserControl.ascx");
LoadControl takes just single argument that is the virtual path of the user control. Some people asked me on forums like, why cannot we use new operator to create an instance of the user control? The answer is simple, if we create an instance of the user control only an instance of the control is created but, not all the child controls on the UserControl.
Once, we load the user control, it should be added to page control collection as follows:
- Controls.Add(uc);
Using the PlaceHolders and Panel Controls, to display the user controls, where ever you need
Above section, helped us to load the control and add it to the controls collectins. But, the drawback is that, we cannot control, where the UserControl actually appear on the page. So, I prefer, always using PlaceHolders and Panel controls. We can add them to the page by just dragging and dropping it on page. Adding the loaded control to them is simple as follows:
- PlaceHolderright.Controls.Add(uc);
- PanelRight.Controls.Add(uc);
Accessing the properties of the dynamically loaded User Control
I accept that, their will be multiple ways do to a same job. Here, I am accessing the properties of the UserControl by casting the UserControl to its class as follows:
- Control uc = LoadControl("ExampleUserControl.ascx");
- ExampleUserControl example1 = (ExampleUserControl)uc;
- example1.xyzproperty = true;
- PlaceHolderright.Controls.Add(example1);
Note that, the above code should be placed in the code-behind file of web form.
Above code snippets, first loads the usercontrol, casts the loaded usercontrol generic one to the example control and then, we are accessing the properties of controls. At last, we are adding the example1 to controls collection.
No comments:
Post a Comment