[Getting started with ASP. NET] Pearl Milk Tea
-- Server Control Model
He is addicted to pearl milk tea. To be honest, he used to think that pearl milk tea was stupid. I really don't know if it was the idea of that genius. Actually, I mixed the pink circle and the foam black tea together, add a funny, ultra-high suck.
When my sister was just fascinated by pearl milk tea, he was very sick. But after that day, he almost went to buy a cup of pearl milk tea every day. The more he drank, the more he thought that pearl milk tea had a special flavor and it was indeed full of delicious milk tea. Yes
He fell in love with pearl milk tea, and his sister smiled at him: "I told you it was good. You don't believe it. Boys are dull ."
-- The temptation of pearl milk tea
■ Object-oriented pink circle-Server Control Model
Suixiang 9
We have realized that an important goal of XHTML is to separate the structure from the performance, such as: <div
Id = "author"> Lao Yan </div>. However, from the perspective of a dynamic website, we need to further separate the webpage structure from the content, namely: <div
Id = "author"> <! -- Dynamic Data --> </div>. In comparison, the webpage structure is static and the content is dynamic.
Charly's content publishing system development in the blog
As a result, the author discusses how to use dynamic data to generate static pages by replacing tags in the template with data. The extension is the dream of ASP programmers for a long time-generation
The code is separated from the page to avoid <div
Id = "author"> <% = author %> </div> and other HTML code are mixed with ASP program code.
The method described by charly is replaced by a regular expression.
If you analyze a. aspx file from the perspective of static/dynamic, you can divide it into two parts: a static continuous text, such as: Body>; the other part is a dynamic special label, such as: <asp: TextBox id = "txtName" runat = "server"
/>. The two determine whether they have the property runat = "server. ASP. NET calls the last partServer ControlThe programmer uses the server control as the object model to define the Web application user interface and control user interaction. The previous part will also be created as a special control-LiteralControl during runtime.
If ASP is used to process the so-called Server Control, the corresponding HTML code is directly generated according to the user's needs.. NET, programmers and HTML code are isolated from the abstract object-oriented server control concept. Since it is object-oriented, the server control should have Attribute(Property) to describe your status; Use Method(Method) describes your own actions; required Event(Event) to trigger the method, change the status, and then automatically generate the corresponding HTML code. Of course, we do not need to build this model from scratch. All server controls, including Page classes, are directly or indirectly inherited from System. web. UI. control class. Controls displayed as HTML form elements often inherit from System. web. UI. webControl class, called Web controls. The following is a simple custom control example. Visit the TestMyControls. aspx page of this example. view the source code and you will find that the html code of the control is "1 ".
// MyControls. cs custom control set
Using System;
Using System. Web. UI;
Namespace essay
{
Public class MyFirstControl: Control // the absolute value of the output Control attribute Number
{
Private int _ number;
Public int Number // define attributes
{
Get {return _ number ;}
Set {_ number = value ;}
}
// Override the Control. Render method to generate the HTML code of the Control.
Protected override void Render (HtmlTextWriter writer)
{
Writer. Write (Math. Abs (Number ));
}
}
}
// TestMyControls. aspx page file, <% Register %> Register custom control set
// <Mc:……> Add a custom control on the page and set the attribute Number to-1.
<% @ Register TagPrefix = "mc" Namespace = "essay" Assembly = "essay" %>
<HTML> <HEAD> </HEAD> <body>
<Form runat = "server">
<Mc: MyFirstControl id = "test1" Number = "-1" runat = "server"/>
</Form> </body> </HTML>
■ Who is Ge Ling? -Maintain the status of the server control
There was a ham ad several years ago. The conversation was as follows:
LV Liping: What is dongbao thinking?
Ge You: Xiang Ge Ling
LV Liping: Don't think about it. I will introduce you to a new friend DUDU brand ham sausage.
LV Liping: (after a while) Do you still want Ge Ling?
Ge You: Who is Ge Ling?
An important part of human-computer interaction design is interactive workflows. The prerequisite for implementing interactive workflows is state persistence. Otherwise, there will be humor like "who is Ge Ling. Controls can store state values using traditional methods such as cookies, sessions, and hidden controls. In suixiang 8, we have discussed the role and principle of ViewState. In essence, ASP. the stateful and continuous page state persistence mechanism created by NET is to hide data through pages. Next, we will further study the details of using view status to maintain the control status through transformation.
// MyControls. cs custom control set
......
Public class MyFirstControl: Control
{
Private int _ number;
Public int Number {......}
// Add the NumberInViewState attribute to access the view status value of the attribute Number.
Public int NumberInViewState
{
Get
{
Object o = ViewState ["NumberInViewState"];
Return (o = null )? 0 int) o;
}
Set {ViewState ["NumberInViewState"] = value ;}
}
Protected override void Render (HtmlTextWriter writer ){......}
}
......
// TestMyControls. aspx page file
<Html>
......
<% @ Page Language = "C #" %>
<Script runat = "server">
Protected override void OnLoad (EventArgs e)
{
Test1.NumberInViewState --; // The value of the view State minus 1.
Test1.Number = test1.NumberInViewState; // The custom control value must be consistent with the view value.
Base. OnLoad (e );
}
</Script>
<Form runat = "server">
<Mc: MyFirstControl id = "test1" runat = "server"/>
<Input type = "submit"/>
</Form> </body>
■ Change the cup or pink circle? -Control tree and Server Control Lifecycle
During the runtime, the page framework will put the specified server control instance in the cup. Of course, they are not accumulated in a random block, but combined intoControl treeFigure 10-2 is an example of the page Control tree model. You can use the control ID or the Position Control in the tree to add or delete the control.
In the hypothetical stateful and continuous page premise, it means that after the initial request, the page must save the status of each controlReturn(PostBack)
First, restore the original status of the control and then process new requests. That is to say, each client sends N consecutive requests to the same page, which is equivalent to asking for N cups of Milk Tea of the same type continuously to the coffee shop. The first cup
In pearl milk tea, the round state is the default. From the request of the customer for the second cup of milk tea, the guy must dial the round state of the cup to the same status as the last cup when the client is given to the customer, then, based on the customer's new events
Line adjustment. 10-2. For detailed procedures, see control execution lifecycle in MSDN.
As a programming model, programmers can easily implement the abstract concept of server controls, but for the performance of the entire system, ASP. NET has not performed much load balancing Optimization on the client and server. in the case of an alloy gun head, "if you even use a server control to submit the drop-down box of the local province and city, and refresh the page at the same time, that is really disgusting ".
A natural idea is: why should we replace the entire cup for every request? Changing a few circles and a small amount of milk tea will greatly improve the system performance. This is the starting point of Ajax technology.