ASP.NET template implementation (C #)

zhaozj2021-02-16  45

Foreword

In previous (perhaps now), when we are doing a web project, some common files are often used, such as the top of the entire page (header), left (navigation), bottom (foot), etc. HTML, ASP and other documents, even so, we also need to do a good frame for these files (usually with TABLE?! There are not a few Divs) to get a complete page with a combination (include). The combination of these shared documents, here assumes defining it into a template (although the true meaning of the template may be much more than this). Even so, when the entire framework changes in our project, we must not modify each of the tops that are originally used by some HTML tags that have passed Table and so on, and such a job can be used by Ctrl C and Ctrl V. Completed, but the workload is also huge, time consuming.

development of

In ASP.NET, use is an object-oriented development mode, which can be so understanding - each page is a Class from SystemWeb.ui.page, which provides us with some cache, indicating, response , Request and other services (saying that it is a way). Is it better to solve the object-oriented method? Of course, the answer is affirmative.

achieve

I remember that there is a famous saying "Any problem can be implemented by joining a middle layer", which is a very simple example, and we often use the Façade mode to reduce the system's coupling, and why should we use design mode? Mainly used to reduce coupling improvement multiplexing.

From all the ASPX pages inherited from systemweb.ui.page, we only need to join a layer between ASPX and SystemWeb.ui.page, write a layer of our own Class to make the problem simple In the .NET framework, allow the user to customize the HTML code (this can be referred to the web user controls) so that the prototype of the problem will come out, see the figure below.

The "Custom Class" in the figure is equivalent to one of the intermediate layers we join, the custom class inherits the base class of System.Web.ui.page, below the category code:

Pagebase.cs

Public class Pagebase: System.Web.ui.page

{

Public string pagetitle = "Test Template";

Protected Override Void Render (System.Web.ui.htmlTextWriter Writer)

{

Writer.write (@ "

" this.pageTitle " </ title> </ head> ");</p> <p>Writer.write (@ "<body></p> <p><table border = '0' width = '680'></p> <p><tr></p> <p><td width = '160' bgcolor = '# 006699' align = 'center'> <font color = '# fffff'> <b> <a href='index.aspx'> Home </A> </ b> </ font> </ td> <TD colspan = '2' width = '520'> advertunch </ td></p> <p></ TR></p> <p><tr></p> <p><TD Width = '160' Valign = 'TOP'></p> <p><p> navigation </ p></p> <p><p> <a href='newcontact.aspx 'Add contact </a> </ p></p> <p><p> Find contacts </ P></p> <p></ td></p> <p><td width = '10 '> </ td></p> <p><td width = '510'></p> <p>");</p> <p>Base.render (Writer);</p> <p>Writer.write (@ "</ td> </ tr> <tr> <td width = '100%' colspan = '3'> Foot foot </ td> </ tr> </ table> </ body> < / html> ");</p> <p>}</p> <p>}</p> <p>The above PageBase.cs is our customty, so we can inherit this class directly in other ASPX pages, not system.Web.ui.page, below the code of index.aspx and newcontact.aspx (I contain index.aspx.cs and newcontact.aspx.cs:</p> <p>INDEX.ASPX</p> <p><% @ Page language = "c #" codebehind = "index.aspx.cs" autoeventwireup = "false" inherits = "wab.index"%> <form id = "index" method = "post" runat = "server"> <asp: datagrid id = "contacts" runat = "server" width = "492px" height = "104px"> </ asp: datagrid> </ form> index.aspx.cs (inherited custom class PageBase)</p> <p>public class index: PageBase {protected System.Web.UI.WebControls.DataGrid contacts; private void Page_Load (object sender, System.EventArgs e) {// Put user to initialize the page code here} #region Web Form Designer generated code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is required for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} /// <summary> /// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// </ summary> private () {this.load = new system.eventhandler (this.page_load);} #ENDREGON} newcontact.aspx</p> <p><% @ Page language = "c #" codebehind = "newcontact.aspx.cs" autoeventwireup = "false" inherits = "wAb.newcontact"%> <form id = "newcontact" method = "post" runat = "server"> <P> <font face = "Song body"> Camera </ font> <ask: textbox id = "textbox1" runat = "server"> </ ask: textbox> </ p> <p> <font face = " Song Body "> last name </ font> <asp: textbox id =" textbox2 "runat =" server "> </ ask: textbox> </ p> <p> <ask: button id =" Button1 "runat =" server " Text = "Button"> </ asp: button> </ p> </ form> newcontact.aspx.cs (inherited Customized Pagebase) Public Class NewContact: PageBase {protected system.web.ui.webcontrols.textbox textBox1; Protected system.Web.ui.WebControls.Button button1; protected system.Web.ui.WebControls.textBox textBox2; private void page_load (Object sender, system.eventargs e) {}</p> <p>#Region Web Form Designer Generated Code Override Protected Void OnNit (Eventargs E) {// // Codegen: This call is necessary for the ASP.NET Web Form Designer. // InitializeComponent (); base.onit (e);} /// <summary> /// Designer Supports the required method - Do not use the code editor to modify the // / this method. /// </ summary> private () {this.load = new system.eventhandler (this.page_load);} #ENDREGON}</p> <p>The above is the basic implementation method of the template in the ASP.NET, but in the first declaration, this will make the system performance to decline a little, but this point does not affect the actual project, but I believe this point system performance change It is convenient for future maintenance, I believe it is worth it.</p> <p>Testing under Windows 2000 Server Visual Studio.net (.NET Framework 1.0).</p> <p>Supplement: If there is anything in the article, please correct it to instantly change.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-25366.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="25366" 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.035</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 = 'hBifNkeSu4pJYpqii8uOl9ihTtlRCJV19EAwZFgma8I2lHSwBQii6Wsgy3cvzYE51GO2VRX1ISEagdsYMOvL6A_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>