Asp.net|asp.net one. Introduction to Code behind
Code behind is what is called coding separation, and since Microsoft launched ASP.net, code behind is a hot topic. In a generic asp.net file, the Code Behind mainly uses two files to create a asp.net page, one is the design file, generally with. aspx or. ascx as the extension, and the other is the program code file, generally with. vb or. cs as extension, its programming language is mainly vb.net or C #。
Two Code behind advantages
The user who has programmed the ASP has known that the ASP program is a mix of interface design and programming. Therefore, when the program designer to modify the application interface layout, it is often necessary to change a large number of interface-independent code, for a small program, the workload is not very large, if the code for a large number of programs, is a workload is not small things. and code behind the interface design and programming code to separate files, for the re-use of code, program debugging and maintenance are revolutionary. Another point is that when you publish your site, you can use this technology to effectively protect your code. This is an improvement on the security of the program. The Code behind technology appears a bit like the past VB or Delphi appears the same. That's why he became a hot topic as soon as he appeared. Below we use a complete example to specify the characteristics of the code behind technology.
Three Introduction to Examples
The operating environment for this example is the Windows Prefessional version and the. Net FrameWork SDK Beat 2. The programming language is C #, whose primary function is to send e-mail. The program is divided into two parts, the interface code to send e-mail to the mail.aspx file to save, and send the program is Send.cs file to save.
Four Concrete implementation Steps
1). Create a new HTML page
You can use Microsoft's fontpage to design the entire layout of your page. All of your options are designed into this page.
Name the HTML file send.htm. The specific code is as follows:
<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>
<body>
<form>
<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" name= "name" size= ">"
</td>
</tr>
<tr>
<TD width= "35%" > E-mail address </td>
<TD width= "65%" ><input type= "text" name= "Email" size= ">"
</td>
</tr>
<tr>
<TD width= "35%" > Content </td>
<TD width= "65%" >
<textarea rows= "7" name= "message" cols= ">"
</textarea>
</td>
</tr>
<tr>
<TD width= "100%" colspan= "2" >
<input type= "Submit" value= "Submit" Name= "B1" ></td>
</tr>
</table>
</form>
<p></p>
</body>
2). Create virtual Directory
First create a directory of "Mail" under the "C:inetpubwwwroot" directory, and then run the Start > Control Panel > Administrative Tools >internet Service Manager > Create a virtual directory with the name "Mail"
3. Create asp.net files with inline code
If you have completed the task of creating a virtual directory and have copied the send.htm file to this directory, please rename the send.htm file to send.aspx. Then open the file with Notepad or another text editor with Windows.
The next step is to create a asp.net file that is embedded in the code, and then convert it into a behind file. Although creating code-embedded asp.net files is an unnecessary task in this article, it is easy to debug and easy to understand, and once your code has a built-in file to run successfully, it's much easier to convert it into a code behind file. The specific steps are as follows:
A). Join page representation
When you open the first line of a file with a text editor, add the expression:
<%@ Page language= "C #"%>
b). Converting the HTML components used in mail.aspx to HtmlControls can be converted using the following table:
HTML HTML control
<form> <form runat= "Server" >
<input type= "text" name= "name" size= "> <input type=" text "id=" name "name=" name "size=" "runat=" Server "/&G T
<input type= "text" name= "email" size= "> <input type=" text "id=" email "name=" email "size=" "runat=" Server " />
<textarea rows= "7" name= "message" cols= ">"
</textarea> <textarea rows= "7" id= "message" name= "message" cols= "num" 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). Add inline code
When the above conversion is complete, the next step is to write the embedded code that the mail sends. The message is sent using a SmtpMail class in the namespace--system.web.mail. A basic method of--send in the SmtpMail class. The successful invocation of this method requires the following four parameters:
1). The source address of the email
2). The destination address of the email
3). The subject of the email
4. Content of Email
The Send method can be invoked successfully only if you have these four parameters. I can refer to my previous article "using ASP.net to make my own mail delivery system" for e-mail messages. In this article there are specific elaboration. The code is a complete inline code:
<% @Page language= "C #"%>
<meta http-equiv=&q