This blog post by Zhao said about the content output method of the ASP. NET WebForm page. Although this topic is very old, it is very important to apply it in ASP. net mvc, and the application is increasing. I hope this article will help you better understand the details of ASP. NET WebForm page content output.
This time we will talk about "How to output content on the WebForm page ". This is actually a very old topic, because the content of this article can even be applied to ASP. NET 1.1. However, this topic is widely applied, because even the latest ASP. net mvc framework, its default view engine is still based on ASP. NET WebForm, such as Page, Control, MasterPage ). Even because of the features of the ASP. net mvc Framework, we may encounter more "direct output" content on the page. Therefore, this topic may be important in ASP. net mvc applications.
Take ASP. net mvc as an example. If we generate a Partial View on the page, we can do this:
- < % Html.RenderPartial("MyPartialView"); %>
However, in the previous article, we proposed a new method, Partial, which returns a string that can be used on the page as follows:
- < %= Html.Partial("MyPartialView") %>
An aspx Page is compiled into a subclass of the Page class. The main "function" of this subclass covers the Render method of the base class:
- public class MyPage : Page
- {
- protected override void Render(HtmlTextWriter writer)
- {
- ...
- }
- }
-
A large amount of content we usually write on the aspx page will actually become the code for operating writer. For example, the writer. Write method is used to output the content, or the writer is handed over to the subcontrol's Render method for generating the content. Then, how are the tags on the above two pages operated on writer respectively?
- < %= expression %>
First, it is marked as <% = %>. <% = %> The tag contains an "expression", so it cannot end with a semicolon. Data in the expression is directly written into writer. For example:
- < %= DateTime.Now %>
After compilation, it becomes:
- writer.Write(DateTime.Now)
Unlike the <% = %> mark, the <%> MARK actually contains the "statement ". The statement can naturally have multiple lines. Naturally, each line must end with a semicolon, as we usually write C # code. But in fact, the statement is not used to "output content", but to "control logic ". For example, you have written the following code on the page:
- < % Func< int, bool> odd = i => i % 2 != 0; %>
This is equivalent to declaring a local variable odd in the Render method. Its type is a Func <int, bool> delegate. If you write such code:
- < % for (int i = 0; i < 10; i++) { %>
- < span>
- < %= i + 1 %>
- < /span>
- < % } %>
The generated Render method will include:
- for (int i = 0; i < 10; i++)
- {
- writer.Write("< span>");
- writer.Write(i + 1);
- writer.Write("< /span>");
- }
If it is a common HTML Tag written on the page, it will be processed as a normal string after compilation. Some friends have been talking about "client controls" and so on. In fact, if an element does not contain the runat = "server" mark, ASP. NET only treats them as common strings and does not have any concept of "HTML elements. Of course, the above Code represents "intent". In fact, after compilation, characters such as spaces and line breaks on the aspx page will also be included in the output content 1.
So, since <%> contains statements used to control the logic, rather than output, why does the Html. RenderPartial method in the Code generate page content? This is because the RenderPartial method directly writes characters to the current HttpContext. Response. Output. Many friends often use Response. Write to Output content. In fact, in the Write method, the content is Output to the Output.
In fact, even if HtmlTextWriter is used in our page to Output content, it also encapsulates the TextWriter exposed by Output. For verification, you can set a breakpoint in the Code and observe the writer parameters of the Render method. Under normal circumstances, you can find writer. the InnerWriter attribute is an HttpWriter object, a subclass of TextWriter and ASP. the internal type defined in. NET.
This is the details of the content output on the ASP. NET WebForm page. What are the differences between the following two output modes?
- < %= "Hello World" %>
- < % Response.Write("Hello World"); %>
In terms of effect, there is no difference between the two. However, the former is actually Output using the HTML textwriter object of the page, while the latter directly outputs content to the Response. Output. This difference does not seem important, but it actually involves many practical methods available in our development process. In future articles, I will propose some guidelines for generating page content, explain the causes of these guidelines, and point out how ASP. NET MVC breaks these design guidelines.
Naturally, the modified version of ASP. net mvc will be published in the MvcPatch project.
- Causes and solutions for WebForm undefined
- ListBox component programming in WebForm
- Differences between ASP. net mvc and WebForm
- ASP. NET Routing
- Add custom routes for ASP. net mvc applications