Dynamically load the components of the user control! (End MasterPages Technology)

xiaoxiao2021-03-06  58

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"%>

Information Management System </ Title></p> <p></ HEAD></p> <p><body leftmargin = "0" TopMargin = "0" marginwidth = "0" marginheight = "0"> <form runat = "server" id = "form1"></p> <p><table width = 800 align = center border = 0 cellpadding = 0 Cellspacing = 0> <tr> <td></p> <p>The same first part</p> <p></ td> </ tr> <tr> <td></p> <p><Region: Plugarea ID = "part1" runat = "server" /> </ td> </ tr> <tr> <td></p> <p>The same second part</p> <p></ td> </ tr> <tr> <td></p> <p><Region: Plugarea ID = "part2" runat = "server" /></p> <p></ td> </ tr> <tr> <td></p> <p>The same third part</p> <p></ td> </ tr> </ table></p> <p></ form></p> <p></ body></p> <p></ Html></p> <p>2, when the template page is quoted, marking the following we keep the space should be inserted into which page is inserted, and you can mark the following! The specific thing is determined by the formation of the reference template page. Inherit Placeholder</p> <p>Have a name: Signarea</p> <p>Two things: First, just set the ID into the ID we want to insert the space component.</p> <p>Second, when this is inserted into the space we leave, set the set path to the same way, avoiding the path differently caused abnormalities.</p> <p>Of course, we have to apply our page in this Signarea.</p> <p>Public Class Signarea: PlaceHolder {</p> <p>INTERNAL STRING DIRECTORY;</p> <p>Public override string templatesourcedirectory {</p> <p>Get {</p> <p>Return Directory;</p> <p>}</p> <p>}</p> <p>}</p> <p>We can use this:</p> <p><Region: signarea id = "part1" runat = "server"></p> <p>Here you can use the article in front to tell Loadsky to reference the user page, or write directly to the content.</p> <p></ Region: Signarea></p> <p><Region: signarea ID = "part2" runat = "server"> The first part of the same.</p> <p></ Region: Signarea></p> <p>3, apply our template page and handle the module insertion.</p> <p>Inherit Placeholder</p> <p>Name: loadingTemplate</p> <p>Do things: First, overload the AddParsedSubObject event, recorded the Signarea in Placeholder</p> <p>Second, load the Template page.</p> <p>Third, find Plugarea (FindPlugarea in Plugarea) according to SIGNAREA, is why it is inserted into Plugarea in Plugarea.</p> <p>These three things will be done after doing things.</p> <p>Look at the code:</p> <p>Public Class LoadTemplate: Placeholder {</p> <p>Private arraylist signarealist = new arraylist ();</p> <p>Protected Override Void AddParsedSubObject (Object Obj)</p> <p>{</p> <p>IF (obj is signala) {</p> <p>SignareAlist.Add (OBJ);</p> <p>}</p> <p>}</p> <p>Protected Override Void OnInit (Eventargs E) {</p> <p>THIS.GetTemplate ();</p> <p>Base.onit (E);</p> <p>}</p> <p>Private void getTemplate () {</p> <p>IF (TemplateFilePath == NULL) {</p> <p>Throw new Exception ("» óðä £ òò3 £ ");</p> <p>}</p> <p>Controls.add (page.loadControl (TemplateFilePath);</p> <p>SignareaInsertintoplugarea ();</p> <p>}</p> <p>Private void signaInsertintoplugarea () {</p> <p>Foreach (Signarea Signarea in SignareAute) {</p> <p>Plugarea plugarea = plugarea.findplugarea (Signarea.ID);</p> <p>IF (Plugarea == Null) {</p> <p>Throw new Exception ("õõ2» μ 1/222åèëμäõ1/4Ä '" signara.id " ");</p> <p>}</p> <p>SIGNAREA.DIRECTORY = TemplatesourceDirectory;</p> <p>Plugarea.Controls.clear ();</p> <p>Plugarea.Controls.add (Signarea);</p> <p>}</p> <p>}</p> <p>Public string templatefilepath {</p> <p>Get {</p> <p>Return (String) ViewState ["TemplateFilePath"];</p> <p>}</p> <p>SET {</p> <p>ViewState ["TemplateFilePath"] = Value;</p> <p>ChildControlscreated = false;</p> <p>}</p> <p>}</p> <p>}</p> <p>Well, let's see how to collect the template page! INDEX.ASPX page:</p> <p><% @PAGE%></p> <p><% @ Register tagprefix = "region" namespace = "region.controls" assembly = "region.controls"%></p> <p><Region: loadTemplate runat = "server" id = "loadTemplate" templatefilepath = "~ / template.ascx"> <region: signarea id = "part1" runat = "server"></p> <p><Region: loadsky id = "sky1" SkinPath = "~ / list.ascx" runat = "server" /></p> <p></ Region: Signarea></p> <p><Region: signarea id = "part2" runat = "server"></p> <p><Region: loadsky id = "sky2" SkinPath = "~ / myarchive.ascx" runat = "server" /></p> <p></ Region: Signarea></p> <p></ Region: loadingTemplate></p> <p>So, when you run a complete page, you will appear in front of us. Of course, you can generate all the same style of style you want.</p> <p>Writing from the beginning now, in the end, it becomes the technology of the legend of MasterPages.</p> <p>Just like the martial arts in the world</p> <p>related articles:</p> <p>Dynamically load the components of the user control! (four)</p> <p>Dynamically load the components of the user control! (three)</p> <p>Dynamically load the components of the user control! (two)</p> <p>Dynamically load the components of the user control!</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-119361.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="119361" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.055</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'KtNu9xXzHQfA9GBY6sUCRncfX6niCJSdOMRCuwekXyFrqG14rXZlbFTg58gfWSVH1dMwg3nJDUhpLKoFJvDcqQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>