Dynamically load the components of the user control! (End MasterPages Technology)
Let's make a page template! Is there a template to unify it! Sure!
For example: Template page Template.ascx we leave a middle part, or you want to leave a space, let the content you want later.
All right! Then we can reference this template.ascx on our page INDEX.ASPX, then put our special things in the empty part, of course, it is best to insert another page in the empty place, such as: list.ascx,
Of course we are doing a page myarchive.aspx. Similarly we quote this template.ascx, then in the empty big place we insert another page Archive.ascx.
very good. What is the two index.aspx. Is it the same? And myarchive.aspx page? Only one of us staying is different.
Of course, such applications can be a variety of perfect effects without the frameset set. And your page can be completely available. Because our page is completely assembled!
So, after this wonderful idea, we start to achieve it.
Technology points: 1, how to leave a space, then let the reference page find this space.
2, how to quote the template and find out the space left.
3, how to quote another page inserted into the space left in the template page.
In fact, all of this requires three components, one is the space used to reserve, one is used to reference the page to the space we have retained, and the two components are actually just the role of tagging. Another is also the primary one is to collect the template, then process the reference page and insert it into the template.
1. Let's solve a technical problem, it is easy to leave a space is PlaceHolder, then we have to consider the ID problem, because we will find this place, of course, we may have to leave a few different places, so It is necessary to handle the ID problem. Then expand the PlaceHolder simultaneously inherits the inamingContainer.
We named: Plugarea
Then there are two things to do this, save itself, and then take it out when used in the third component.
Public Class Plugarea: PlaceHolder, InamingContainer {
Public override string id {
Get {
Return Base.ID;
}
SET {
Base.id = value;
AddTocontext ();
}
}
Private static readonly string contextkey = "region.masterpages.region";
Private void addtocontext () {
IF (httpContext.current! = null) {
String mykey = contextKey this.id;
IF (httpContext.current.Items.contains (mykey)) {
Throw new invalidOperationException ("This ID '" this.id "has been used!");
} else {
HttpContext.current.Items [mykey] = this;
}
}
}
INTERNAL Static Plugarea FindPlugarea (String ID) {
IF (httpContext.current == null) {
Return NULL;
Return httpcontext.current.items [contextkey id] as plugarea;
}
}
So our template.ascx can be written
<% @ Control%>
<% @ Register tagprefix = "region" namespace = "region.controls" assembly = "region.controls"%>
HEAD>