Dynamically loading content in asp.net (user controls and templates)

Source: Internet
Author: User
asp.net| Dynamic | load | controls | Templates dynamically load content in asp.net (user controls and templates)
Points:
1. Using Page.parsecontrol
2. Use base. LoadControl
Part One: Loading templates
The following is a template "<table width=100%><tr><td width=100% colspan=2 runat=server id=containertop></td> &LT;/TR&GT;&LT;TR&GT;&LT;TD width=30% runat=server id=containerleft></td><td width=70% runat=server Containerright></td></tr></table> "How to add it to the page.

First, you need to have a page (. aspx), a table in a row on the page, and a server-side TD we'll name it TemplateContainer. Okay, now go to the Code editing window and load the template in Page_Load.
The second step is to use Page.parsecontrol to parse the top HTML code into the System.web.ui.control of the Web Forms page or user control as follows:
protected System.Web.UI.HtmlControls.HtmlTableCell TemplateContainer;
System.Web.UI.Control Objcontainer;
private void Page_Load (object sender, System.EventArgs e)
{
Objcontainer =page.parsecontrol ("Top HTML code");
This. TEMPCONTAINER.CONTROLS.ADD (Objcontainer);
}
The template has already been loaded.
Part Two: Loading user controls
First, you want to determine where the user control will be loaded into the page. Now you will find the first part of the HTML code, where each TD is a container you can find the container that you want to load the control by Page.findcontrol. The following code:
System.Web.UI.Control Objcontrol=page.findcontrol ("Containertop");
Second, after you find the container, you can load your user control into the page. The following code:
OBJCONTROL.CONTROLS.ADD (base. LoadControl ("User Control virtual path"));
Now run the program to see if the user control has already been loaded.

These are some of the main parts of dynamic load templates and user controls, and to make a better page you need to add a lot of necessary things, such as saving the template and the virtual path of the user control to the database, and adjusting the position of the user control that the page has loaded. Control over permissions as those people can see what user controls those people can not look at user controls, and so on.
Here's an example: http://elt.nec.edu.cn, but you can't adjust the user parts location here. (Of course I can) in this example the navigation bar is also dynamically loaded, and different pages may use different templates to load different user controls.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.