In-depth understanding of ASP. NET Dynamic controls (Part 2-compilation process)

Source: Internet
Author: User

 

Preface

In-depth understanding of ASP. NET Dynamic controls, first of all, we need to have a deep understanding of the entire ASP. NET page processing process, you write An ASPX file (may also have a code-behind file) to the HTML page you see in the browser, what happened in the middle. The first step is to explain and compile the aspx file, which is the content to be discussed in this article.

Since ASP. NET compilation is a big topic, I decided to divide this topic into several articles in this series. The first article briefly describes the steps involved in the compilation process, so that you can understand how the declarative code and C #/VB. NET code in aspx are combined and compiled into assembly. After this article, I will go into some details during the compilation process to see how a static control with declarative definitions in aspx runs.

Aerial view

Let's start with the compilation process. First, let's look at two figures. This is the compilation flowchart of ASP. NET 1.x:

Next, the compilation flowchart of ASP. NET 2.0:

The two images are from the official document ASP. NET 2.0 internal changes, you should note that the code-Beside (Inline) and code-hidden (Code-behind) Compilation modes are different: the code-embedded is only compiled once, declarative code and C #/VB.. Net code is compiled into a class together. If the code is hidden, the declarative code and C #/VB.. Net code is separated several times for translation/compilation. These codes are in the relationship between local and local (partial), or between the base class and the derived class.

Landing

Now, let's go down to a point on the graph to see how a compilation step is executed.

ASP. NET 1.x

What attracts attention in the figure is that the Code hides the "inherited from" relationship twice during compilation. The first inheritance is very understandable. People who have used vs2002/2003 remember to explicitly declare that the class of this page inherits from the page class in the Code. How does the second inheritance come from?

Let's put aside the above question first. Let's think about it in another way and rethink about what our C #/VB. NET code has. If we put a textbox in aspx, the Code on both sides will show its definition. The aspx code is <asp: textbox id = "mytextbox" runat = "server"/>, C # The code is textbox mytextbox = new Textbox (); mytextbox. id = "mytextbox ";. Then, we will write <div> Please write down something </div> in HTML behind the textbox. Therefore, this section of HTML only has definitions in aspx, not in C # code.

Next we compile the C # code and then use ASP. the net engine runs it (it can indeed run this way, but this is not what we are currently concerned about). What do you guess we can see? We should be able to see a textbox. As for the subsequent text, you should be smart and immediately think that it is not defined in the C # code, so it cannot be seen.

Now we understand that some logic is defined only in aspx, and we need to add them to the C # compilation result. How to add this part of logic? ASP. NET selects the Inheritance Mechanism, inherits from the class of the C # compilation result, and then adds the logic defined only in aspx to the derived class. How to compile aspx as a declarative language into msil is discussed in the next article.

It must be noted that the two compilations must be performed manually for the first time, such as executing the compilation in vs2002/2003 and the second compilation will be performed automatically at runtime. Therefore, you do not need to re-compile the aspx, but you need to manually compile the C #/VB. NET code.

ASP. Network 2.0

ASP. NET 1.1 code hiding and compilation also mentioned the problem. A Textbox Control should be declared on both sides at the same time, which obviously violates the dry (don't repeat yourself) principle. ASP. NET 2.0 introduces a new mechanism to solve this problem.

The so-called new mechanism is the partial keyword in C # code. Everyone may be used to its existence. But has anyone ever wondered where the other partial of the page inheritance class is? If you search for a project in vs2005, you will find that other partial of this class does not exist. In this case, you should check the official documentation (for example, the one I provided above ). The official document will tell you that the other partial is aspx, which will be merged and compiled like two common partial files, so in ASP. in NET 2.0, we only need to merge and compile once to solve all the problems. Then I want to tell you that what the official document says is wrong, Asp. NET 2.0 compilation is still like ASP. NET 1.1, but according to the control definition in aspx, the corresponding C # definition work is generated and transferred to ASP by IDE. net compiler. For details, refer to the two articles I wrote earlier: Asp. NET 2.0 solves the problem of Code-behind requiring control declaration synchronization. NET 2.0 compilation model is not exactly as the MS said.

In ASP. after the net compiler picks up the definition synchronization job, the entire compilation process will be within its scope of responsibility, no longer like ASP. net 1. in that case, C #/vB first. net compiler is responsible for hiding the compilation of code, and then ASP. net compiler is responsible for secondary compilation. Since the ASP. NET compiler is responsible for two compilations at the same time, it can save the trouble of manual compilation for the first time, and the compilation is done by it at runtime.

Next step

Now we have an understanding of the entire compilation process, and most of the compilation steps are easy to understand, just called C #/VB. net compiler, except for one: only how the logic declared in aspx is compiled into msil, because we take this as the goal for further understanding, and discussed in the next article.

Problems and labs

There are some simple questions or small experiments. They can help you better understand the article. You can write the answers directly in the article comments.

  1. I created a new user control myusercontrol in the root directory of the Web application. ascx: The definition class name in the hidden file is myusercontrol. Now I need to dynamically load this user control on the page. Which of the following methods is correct? Why? (Note: The compilation method of ascx is similar to that of Aspx)
    1. This. Page. Controls. Add (New myusercontrol ());
    2. This. Page. Controls. Add (this. Page. loadcontrol ("~ /Myusercontrol. ascx "));
  2. When discussing ASP. NET 1.1 compilation, I mentioned that classes compiled by hidden code can be directly run and a textbox can be seen. In fact, this textbox may not be visible, but I don't have vs2002/2003 on hand, so I can't verify it. If you are interested, you can perform an experiment to see if the textbox will appear. Before the experiment, let me talk about how to directly run the hidden code compilation result:
    1. Open msdn, find the ihttphandler entry, and then check its sample code, and how to configure a path in Web. config to use a specific ihttphandler.
    2. Because the page class implements ihttphandler, the page inheritance class after code compilation is also required to be ihttphandler. configure a path to use ihttphandler in config and point to the hidden code class you want to test.
    3. Access the path you configured in the browser, and you will be able to see the execution result after the pure hidden code is compiled.

Source: http://www.cnblogs.com/cathsfz/

Related Article

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.