I. Inline code and code-behind
The advantages of the code-behind model relative to inline code:
1. The separation of logical code and HTML allows us to focus more on the processing of business logic.
2. Make business logic clearer.
3. You can compile the back code into a DLL to make the program more secure.
Two. The relationship and mechanism of post Code and page
Three classes associated with the code back model.
1. Base Pages Class Page class
The page class in the. NET class library, which defines the basic functionality of the page. Examples include allowing other controls to be hosted, rendering HTML, and providing access to asp.net objects (such as request, response, session, and so on).
2. Code Post Class
At compile time, ASP.net uses the magical features of the partial class to attach some additional code to the code back class.
In this additional part of the code, you define all the controls on the page as variables of type protected, making it possible to access the controls in the code-behind class.
3. Page class
The ASP.net compiler creates another class that represents the actual ASPX page at compile time, and this class inherits from the code-behind class.
This class contains the code that initializes the control and renders the HTML. When accepting this page request, ASP.net instantiates this class.
These three types of relationships are shown below:
Three. code-behind files and the connection to the page
<%@ Page language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default"%>
The connection between the code-behind file and the page is very simple, and a word at the top of the ASPX page completes their connection.
CODEFILE Specifies the file for the post code, inherits specifies the class name of the post code that has been compiled.
Four. Control and page variable connection
After understanding the relationship between the back code class and the page class, it is much simpler to understand how the control is connected to the page variable.
Like what:
<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
The ASP.net compiler will have the following variables declared in your code-behind class, and of course these declarations are not visible:
protected System.Web.UI.TextBox TextBox1;
This allows you to access the page control label in the post code class.
Five. Event and event handler connection
The event mechanism in. NET is a very important mechanism, and this part is to be slowly understood.