Code Behind Technology in ASP.NET (Reposted)

xiaoxiao2021-03-06  79

Code Behind Technology in ASP.NET. Introduction to Code Behind is the so-called code separation. Since Microsoft has launched ASP.NET, Code Behind is a hot topic. In a general ASP.NET file, Code Behind is mainly used to create an ASP.NET page in two files, one is a design file, which is generally made as an extension in .aspx or .ascx, and the other is the program code Files, generally in .vb or .cs as an extension, the programming language is mainly VB.NET or C #. two. Code Behind Benefits Users who have used ASP compiled programs that the ASP program is mixed with interface design and program design. Therefore, when the program designer wants to modify the application interface layout, it is often necessary to change the code that is not related to the interface. For a small program, the workload is not very large. If the program is large for the code, it is a workload. Not small. Code Behind separates the interface design code and program design code, separated by different files, and reuses the code, the debugging and maintenance of the program is revolutionary. Another point is that when you post your website, you can use this technology to effectively protect your code. This is an improvement for the security of the program. The appearance of Code Behind technology is a bit like VB or Delphi appears. This is why he has become a hot topic. Below we use a complete example to specifically explain the characteristics of Code Behind technology. three. Example Introduction This example is the operating environment of Windows 2000 Preferenceional and .NET Framework SDK BEAT 2. The programming language is C #, and its main function is to send an email. The program is divided into two parts, saving the interface code of the sending email in the mail.aspx file, and the transmitted program is saved in Send.cs file.

four. Specific implementation Step 1). New HTML page You can use Microsoft's FontPage to design your page's entire layout. Design your option to this page. Name this HTML file Send.htm.

The specific code is as follows: new page 1 </ title> </ head> <body> <h3> Welcome to the code Behind design interface </ h3> <form> <table Border = "1" width = "100%"> <tr> <td width = "100%" colspan = "2"> Please fill in the following items: </ td> </ tr> <tr> <TD Width = "35%"> Name </ td> <TD width = "65%"> <input type = "text" name = "name" size = "20"> </ td> </ tr> <TR> < TD width = "35%"> email address </ td> <TD width = "65%"> <input type = "text" name = "email" size = "20"> </ td> </ tr> <Tr> <td width = "35%"> content </ td> <td width = "65%"> <textarea rows = "7" name = "message" cols = "26"> </ textarea> </ TD> </ tr> <tr> <td width = "100%" colSpan = "2"> <input type = "submit" value = "submit"</p> <p>Name = "b1"> </ td> </ tr> </ table> </ form> <p> </ p> </ body> </ html> 2). Creating a virtual directory first in "c: inetpubwwwroot" Create a directory to "Mail" below, then run "Start> Control Panel> Management Tools> Internet Service Manager>" Create a virtual directory name is "Mail" 3). Create an inline code ASP.NET file if you The work of creating a virtual directory has been completed, and copy the send.htm file to this directory, please rename the send.htm file to Send.aspx. Then open this file with the NOTEPAD or other text editor that comes with Windows. Next we first created an ASP.NET file embedded in the code, then convert the converted into a Code Behind file. Although the ASP.NET file created in the code is an extra work in this discussion, it is convenient for debugging, easy to understand, and once your code embedded in the file, turn him into a Code BEHIND files are relatively easy.</p> <p>The specific steps are as follows: a). Add to page Indicates the first line of the file with the text editor, add it to the representation: <% @ page language = "c #"%> b). Put the HTML component used in mail.aspx Convert to HTMLControls, can be converted with the following table: HTML HTML Control <form> <form runat = "server"> <input type = "text" name = "name" size = "20"> <input type = "text" ID = "name" name = "name" size = "20" runat = "server" /> <input type = "text" name = "email" size = "20"> <input type = "Text" ID = " Email "name =" email "size =" 20 "runat =" server "/> <textarea rows =" 7 "name =" message "cols =" 26 "> </ textarea> <textarea rows =" 7 "id = "Message" name = "message" cols = "26" runat = "server"> </ textarea> <input type = "submit" value = "submit" name = "b1"> <input type = "submit" id = "B1" value = "submit" name = "b1" ONSERVERCLICK = "post_form" runat = "server"> c) After the embedded code is completed, the inline delivery code sent by the message will be written next. Sending an email is a SMTPMAIL class in the namespace - System.Web.mail. A basic method in the SMTPMAIL class --send. The successful call of this method requires the following four parameters: 1). Source address 2 of the email). Delivery address 3 of the email 3). The subject of the email is only available in the content of the email, the SEND method It is possible to successfully call. For email, you can refer to my previous article "to make your own mail sending system with ASP.NET". There is a specific elaboration in this article.</p> <p>The code is the complete embedded code: <% @ page language = "c #"%> <html> <head> <meta http-equiv = "content-type" content = "text / html; charSet = GB2312> < Meta name = "generator" content = "Microsoft FrontPage 4.0"> <meta name = "progid" content = "frontpage.editor.document"> <title> new page 1 </ title> <script runat = "server"> protected Void Post_form (Object sender, eventargs e) {file: // check if the name and email fields are filled in if (name.value! = "&& email.value! =") {file: // send the mail system .Web.mail.smtpmail.send (Email.Value, "saurabh@mastercsharp.com", "Mail from:" name.value, message.value);}} </ script> </ head> <body> < H3> Welcome to Code Behind Design Interface </ h3> <form runat = "server"> <table border = "1" width = "100%> <Tr> <TD width =" 100% "colspan =" 2 " > Please fill in the following </ td> </ tr> <tr> <td width = "35%> Name </ td> <TD width =" 65% "> <input type =" text "id = "Name" name = "name" size = "20" runat = "server" /> </ td> </ td width = "35%> email address </ td> <TD Width = "65%"> <input type = "text" ID = "email" name = "email"</p> <p>Size = "20" runat = "server" /> </ td width = "35%"> content </ td> <td width = "65%> <textarea rows = "7" id = "message" name = "message" cols = "26" runat = "server"> </ textarea> <td> </ tr> <tr> <td width = "100%" colSpan = " 2> <input type = "submit" value = "submit" id = "b1" name = "b1" onServerClick = "post_form" runat = "server" /> </ td> </ tr> </ table> < / form> <p> </ p> </ body> </ html> 4). Start Creating a Code Behind file If the above page can run successfully, then the program code is separated from the interface design code. This is done by the following steps. A). Create a .cs source program file creates a file name "Send.cs" in the virtual directory of your application host, and open this file with a text editor. b). Copy the script to the Send.cs program file to cut all the contents in the "Script Runat =" Server "> </ script> in the" "representative of the script) in the </ script>, cut into the send.cs file .</p> <p>C). Modify the .cs file must ensure that the send.cs file is a correct C # file, so you must make the necessary modifications at this time. The following list will be listed below, and the two differences are different: no The modified C # file, as follows: <script runat = "server"> protected void post_form (Object sender, eventargs e) {file: // determines whether the name and email address fill in IF (Name.Value! = "&& email. Value! = "") {File: // Send email system.web.mail.smtpmail.send (email.value, "majinhu@yesky.com", "Mail from:" name.value, message.value) }}} </ Script> Complete the modified file, as follows: using system; using system.web.ui; using system.web.ui.htmlcontrols; public class firstage {file: // Declarative Component Protected HTMLINPUTCONTROL NAME, Email; Protected HTMLTextArea Message; Public Void Post_form (Object Sender, Eventargs E) {file: // Decision Name and email address to fill in if (Name.Value! = "&& email.value! =" ") {File: // Send an email system.Web.mail.smtpmail.send (email.value, "majinhu@yesky.com", "Mail from:" name.value, message.value);}}} Note: First: This modification must introduce the necessary namespaces, and the namespaces used in this segment are "System", "System.Web.ui" and "System.Web.ui.htmlControls". Second: Use the "Public Class Firstage {" logo to replace the identifier </ script> ended by <script runat = "server">. Third: Finally, we must also declare the components used in the program. D). Modify the interface design file (mail.aspx) After the above work is completed, you must modify the interface design file, let the ASP.NET's running environment know where to load the Code Behind file.</p> <p>So the following modifications, the modified code is as follows: mail.aspx <% @ page language = "c #" inherits = "first" src = "send.cs"%> <HTML> <head> <meta http-equiv = "Content-Type" Content = "text / html; charSet = GB2312"> <meta name = "generator" content = "microsoft frontpage 4.0"> <meta name = "progid" content = "frontpage.editor.document"> < Title> New Page 1 </ Title> </ head> <body> <h3> Welcome to Code Behind Design Interface </ h3> <form runat = "server"> <table border = "1" width = "100%" > <Tr> <td width = "100%" colSpan = "2"> Please fill in the following items </ td> </ tr> <tr> <td width = "35%> Name </ td> < Td width = "65%"> <input type = "text" id = "name" name = "name" size = "20" runat = "server" /> </ td> </ tr> <TR> <TD Width = "35%"> email address </ td> <TD width = "65%"> <input type = "text" ID = "email" name = "email" size = "20" runat = "server" /> </ Td> </ tr> <tr> <td width = "35%> Content </ td> <TD width =" 65% "> <textarea rows =" 7 "id =" message "name = "Message" cols = "26" runat = "</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-106223.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="106223" 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.040</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 = 'Xt5yuG47oSCSkNT7GgWESOBI5tzZn7eiXH_2BSfDhyyh28sOaH2uJPf96UKCCZZDs6foZd_2FBK_2F_2FUrMHIA8zgYqGQ_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>