asp.net code-behind coding model _ practical Tips

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.