Page Template (Page Template)

xiaoxiao2021-03-14  202

I rarely saw Chinese information about introducing ASP.NET Page Templates (hereinafter referred to as Page Template), I have seen a lot of information in foreign sites. I don't know if you are not happy. Use page template. When I develop ASP.NET web applications, I prefer to use it, and the effect is not bad. Today, some information is compiled and sorted out, talk about ASP.NET Page Templates.

When you are developing a part or all pages having a common element, such as banner, copyright statement, navigation bar, etc .; even part or all pages have functions, such as authentication session judgment, error capture display, data Operation and help prompt function, you will encounter such a problem: How to make these ASP.NET pages have these common elements and features simple and convenient?

In previous ASP programming, use include Contains files to solve the above problem. Although the include file has solved the shared page element problem to a certain extent, it is tight between the containment file and the included page, which means that your page must contain and contain the corresponding header file in a certain order, otherwise the program will Do not run normally. At the same time, this tight coupling relationship allows you to modify each header file and each ASP page in order to modify a significant interface or important feature of the site in the future.

Now, in ASP.NET, many of the new features and object-oriented objects can be used to find better solutions to solve this problem.

Therefore, the main discussion will be: ASP.NET What channels can I solve the shared problem of page elements or functions? What is the effect of ASP.NET PAGE TEMPLATE?

First, what channels can ASP.NET to solve the shared problem of page elements or features?

1, User Controls

When I just contacted asp.net, I saw a book of Taiwanese, and they introduced something about Pagelet (they called "web accessories", which felt fresh. Later, the real name of Pagelet is called User. Control, we call user controls. User control is actually a small page module that encapsulates HTML code block and rear server-side code to make it easy to reuse it in many different ASP.NET pages. This section may also More familiar, there is not much to say, more specific

Http://chs.gotdotnet.com/quickstart/aspplus/doc/webpagelets.aspx. User controls still do not have a problem that the page elements and functions are shared. For example, you encapsulate a header.ascx to allow each page head to keep consistent, and that you have to pull the header.ascx in a certain location; when you need to change the Header.ascx location, When you put under each page, you must also modify each page.

2, Page Template

In the ASP.NET Web application project, all ASPX pages are inherited from the System.Web.ui.page class. Because of this, in the Page Template scheme, in order to solve the sharing of views and functions, we created a page base class inherited from the System.Web.ui.page class, and inherit all ASPX pages in the web application Page base class PageBase.

As can be seen from the above figure, a layer of PageBase will help us add some public views or features to PageBase to make The page has public characteristics. Below is PageBase class code: use

System;

Using

System.Web.ui;

public

Class Pagebase: System.Web.ui.page

{

Private string_pagetiple;

Public String PageTitle

{

Get {return _pagetility}

Set {_pagetitle = value;

}

Protected Override Void Render (HTMLTextWriter Writer)

{

//

First create HTML and Body nodes

Writer.write (@ "

" PageTitle @" </ Title></p> <p></ hEAD></p> <p><body> ");</p> <p>//</p> <p>Secondly allow the base class to present HTML code and ASP.NET controls in the ASPX page.</p> <p>Base.render (Writer);</p> <p>//</p> <p>Finally end Body and HTML nodes</p> <p>Writer.write (@ "</p> <p></ body></p> <p></ html> ");</p> <p>}</p> <p>}</p> <p>Leave an ASPX page WebForm1.aspx only left <% @ page ... ..%> instructions and <form> ... </ form> nodes. as follows:</p> <p><% @ Page language = "c #" codebehind = "Webform1.aspx.cs" autoeventwireup = "false" inherits = "WebApplication3.webform1"%></p> <p><form id = "form1" method = "post" runat = "server"></p> <p>THIS IS.</p> <p></ form></p> <p>In Webform1.aspx.cs:</p> <p>public</p> <p>Class Webform1: PageBase</p> <p>{</p> <p>Private Void Page_Load (Object Sender, System.EventArgs E)</p> <p>{</p> <p>//</p> <p>Place the user code here to initialize the page</p> <p>PageTitle = "this is a test";</p> <p>}</p> <p>}</p> <p>At this point, a simple page Template has been implemented, running, how do you feel?</p> <p>J</p> <p>Second, more Page Template Application</p> <p>When we do web applications, we often found that all pages in the web application are partially identical performance or feature, such as session processing, page error handling, etc. These all pages are the same performance or function, we can solve the Page Template technology, one to make up the program's finishing and maintenance, two to reduce the amount of code. Some commonality with most web applications is listed below, which can be easily implemented by Page Template technology:</p> <p>? ● Session processing</p> <p>Typically, we need to record some information from the user, such as user name, user number, etc., and then the page program further determines whether the user has been logged in by obtaining these user information, or further acquires user details. [Idea]: Using Page Template, just define whether a property already logged in is required to define a logged in, or set a protection method that determines if the login is logged in, if it is not logged in. Jump now to the login page.</p> <p>? ● Internationalization</p> <p>For a web application that requires a multi-language display, you need to read the resource file at each page so that the page is displayed in various languages.</p> <p>[Idea]: Use the CultureInfo class based on the language of the acquired user client. Then go to the corresponding language text information in the resource file according to a keyword key.</p> <p>CultureInfo CultureObject = CultureInfo.createspecificCulture (langcode);</p> <p>Thread.currentthread.currentuicultuiculture = CultureObject;</p> <p>Thread.currentthread.currentculture = CultureObject;</p> <p>? ● Error handling</p> <p>You can define consistency of all pages to handle errors - Record Errors -> Jump to Error Page -> Show errors with appropriate prompt information.</p> <p>[Idea]: Using Page Template, you can define an ErrorCode property in the page base class, then add a setErrorCode (int code) method to set the ERRORCODE property and jump to the error page feature (SeterrorCode calls in the Catch block of the rear code) ). When jumps to the error page, the error page will get ErrorCode:</p> <p>INT ErrorCode = (PageTemplate) Context.Handler). ErrorCode;</p> <p>The error page can then take the corresponding error message from the resource file according to ERRORCODE and display it.</p> <p>? ● Universal tool method</p> <p>All pages require some practical methods, such as logging operation logs, sending information operations, and the like.</p> <p>? ● Help system</p> <p>When the user clicks on the page Help button, the consistent help display mode - Open Help Window -> Display Help -> Jump to the help information page corresponding to the current page.</p> <p>[Idea]: Define the Helpurl property in the page base class and then use this property in render ().</p> <p>? ● Other</p> <p>Such as database operations, page style custom, or even all pages have all pages, picture style or function customization can be implemented using the Page Template implementation. Of course, it is specifically applied according to the specific situation.</p> <p>Three, more page template base class</p> <p>Usually, we have a web application consisting of different modules with different template bases, such as forums, background management, press release, online stores, etc. We can use the Page Template technology to further sort out:</p> <p>Four, when should I use Page Template?</p> <p>Finally, I hope everyone can understand that any technique will not apply and use anywhere. Therefore, for the applicable range of Page Template: In a web application that is not allowed, each page contains functions such as Headers, Footers, Menus, Session, etc., or even user information such as acquiring login. Method.</p> <p>All right. If you need more detailed information or you need to download sample code, you can refer to the information:</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-129437.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="129437" 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.045</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 = 'Ml8_2F4dYM19VqzQ2rzU4SVqwnmAqG4GHt6eGdHucQZ3H72kQJ_2Fl1iyDHqMaMWx0OoXSr667m_2FjrHksWaWMZVF2Q_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>