Asp. NET entry Random server control model

Source: Internet
Author: User
Tags abstract define html form mixed net object model regular expression client
Asp.net| Server | Control He was fascinated with pearl milk tea. To tell the truth, he had always thought that pearl milk tea is very stupid, I really do not know is that a genius idea, incredibly the powder and foam black tea mixed together, but also with a so funny oversized straw; the younger sister just got hooked on the pearl milk tea, he still quite sneer at it. But after this day, he almost every day to buy a cup of pearl milk tea, the more the more they think the flavor of pearl milk tea is very special, indeed full of good drink. Know that he is fascinated with pearl milk tea, the younger sister also smiled at him: "Good morning with you agree to drink you do not believe, boys are slow." "


--Takia "The Temptation of pearl milk tea"





-Object-oriented powder-server control model




An important goal of
XHTML is to separate the structure from the performance, such as: <div id= "author" and Old Yan </div>; but from a dynamic website point of view, we need to further separate the structure of the Web page from the content, namely: <div id= "Author" ><!-- Dynamic Data--> </div>, in contrast, the Web page structure is static, content is dynamic.





Charly's "Development of content publishing system" triggered my recollection of the past, the author discusses the proposition is how to use Dynamic Data to generate static pages, the method is to replace the template with the data specified tags. Extended is the ASP programmer very remote dream-code and page separation, that is, to avoid the <div id= "author" and HTML code such as the ASP program code mixed in the case, and the solution, Using the method described by Charly--Regular expression substitution.











If you analyze an. aspx file using the static/dynamic angle, you can divide it into two parts: a static, contiguous text, such as:




if the ASP process-oriented approach to handling the so-called server control, is based on the needs of users directly generate the corresponding HTML code, and in ASP.net, the programmer and HTML code is abstract object-oriented server control concept isolation. Since object-oriented, server controls should have properties to describe their state, use methods to describe their actions, require events to trigger methods, change states, and finally automatically generate the corresponding HTML code.





Of course we don't have to build this model from scratch, all the server controls, including the Page class, are directly or indirectly inherited from the System.Web.UI.Control class, and controls that appear as HTML form elements are often inherited from the System.Web.UI.WebControl class, called Web controls. The following example is a simple example of a custom control that accesses the testmycontrols.aspx page of the example to see that the source code finds that the control's corresponding hhtml code is "1".





//MyControls.cs custom Control set


using System;


using System.Web.UI;


namespace essay{


public class myfirstcontrol:control{//Output attribute number absolute Value control


private int _number;


public int number{//number define Properties


get {return _number;}


set {_number=value}


  }


//Rewrite the Control.render method to generate the corresponding HTML code for the control


protected override void Render (HtmlTextWriter writer) {


writer. Write (Math.Abs (number));


  }


 }


}


//testmycontrols.aspx paging file, <%Register%> registering a custom control set


//<MC: ... > increase the custom control on the page and set the property number value 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> Green who?-the state of the server control remains





a few years ago there was a ham ad, dialogue as follows:





Lü: Dong Bao, what are you thinking?





ge: Want to Green





Lü: Forget it, I'd like to introduce you to a new friend--dudu ham sausage





Lü: (after a while) still want to Green?





GE: Who is Green?




An important content of
human-computer Interaction design is interactive workflow, and the precondition of realizing interactive workflow is state keeping, otherwise, the humor of "Green" will appear.





controls can use traditional cookies, sessions, hidden controls and other methods to store the state value, in the random eight "we have discussed the view state (ViewState) the role and principle, in essence, ASP." NET to create a stateful, continuous page state-keeping mechanism is to hide data through the page. Next, we further study the use of view state to complete the details of control state maintenance by revamping the above example.





//MyControls.cs custom Control set


......


public class myfirstcontrol:control{//Output attribute number absolute Value control


private int _number;


public int number{...}


//Add property numberinviewstate to access the view state 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 paging file




......


<%@ Page language= "C #"%>


<script runat= "Server" >


protected override void OnLoad (EventArgs e)


{


test1. numberinviewstate--; View state corresponding value self minus 1


test1. Number = Test1. Numberinviewstate; Custom control values remain consistent with view values


base. OnLoad (e);


}


</script>


<form runat= "Server" >


<mc:myfirstcontrol id= "test1" runat= "Server"/>


<input type= "Submit"/>


</form> </body>

Change the cup or change the powder circle?-The life cycle of the control tree and the server control





at runtime, the page frame is placed in the cup with the specified server control class instance. Of course they are not stacked together in a single, but grouped into a control tree, figure 10-2 is the example 2 page of the control tree model, we can control the control by ID or the location in the tree controls, can also add or remove controls.





in the hypothetical stateful, continuous page premise means that after the initial request, the page must save the state of each control, after the return (postback), first is to restore the original state of the control, and then process the new request. In other words, each client on the same page of the continuous n-times request, the equivalent of a continuous to tea shop N Cup of the same variety of milk tea, the first cup of pearl milk tea in the state of powder is the default, from the customer asked for a second cup of tea, the man must first of the cup in the state of the powder to the customer when the last cup of the same status Then adjust according to the customer's new events. As shown in Figure 10-2. For detailed procedures, check out the MSDN Control execution lifecycle.








This idea of an interactive workflow implementation comes at a cost, and as a programming model, programmers can easily implement the abstract concepts of server controls, but for the performance of the entire system, ASP. NET does not have to the client and server to do how much load performance optimization, with the alloy gun head (one of my netizens), said, "If you choose the place of origin and provinces of the Drop-down box to use server control to submit, while refreshing the page, it is really disgusting."





A natural idea is why do we have to replace the whole cup for every request? If just replace a few powder and a small part of milk tea, will greatly improve the system performance, this is the starting point of Ajax technology.





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.