1. Use Page. parsecontrol
2. Use base. loadcontrol
Part 1: load a template
The following is a template "<Table width = 100%> <tr> <TD width = 100% colspan = 2 runat = server id = containertop> </TD> </tr> <tr> <TD width = 30% runat = server id = containerleft> </TD> <TD width = 70% runat = server id = containerright> </TD> </tr> </table> "How to add it to the page.
first, you need to have a page (. aspx). There is a table with a row and a column in the page, and the server-side TD is named templatecontainer. Now, in the Code editing window, load the template in page_load.
Step 2: Use Page. parsecontrol analyzes the preceding HTML code as a web form page or user control system. web. UI. control the following code:
protected system. web. UI. htmlcontrols. htmltablecell templatecontainer;
system. web. UI. control objcontainer;
private void page_load (Object sender, system. eventargs e)
{< br> objcontainer = page. parsecontrol ("the above HTML code);
This. tempcontainer. controls. add (objcontainer);
}< br> the template has been loaded.
Part 2: loading a user control
first, determine the location where the user control is loaded to the page. Now you will find the first part of the HTML code, where every TD is a container you can find the container to load the control through page. findcontrol. The following code:
system. web. UI. control objcontrol = page. findcontrol ("containertop");
second, after finding the container, you can load your user control to the page. The following code:
objcontrol. controls. add (base. loadcontrol ("user control virtual path");
RUN Program to check whether the user control has been loaded.
The above are some of the main parts of dynamic loading templates and user controls. To make a good page, you still need to add many necessary things, for example, save the virtual paths of the template and user control to the database, and adjust the positions of the user control that has been loaded on the page. Permission control, such as those who can see what user controls those who can not see user controls, and so on.
Here is an example: http://elt.nec.edu.cn, but here you cannot adjust the location of the user piece. (Of course I can.) In this example, the navigation bar is also dynamically loaded. Different pages may use different templates to load different user controls.