Every time I write some complicated controls from the original method, isn't it depressing?
How can I simply use existing controls to create composite controls?
Let's take a look at a simple example.
1 [toolboxdata ("<{0}: webcustomcontrol1 runat = server ></ {0}: webcustomcontrol1>")]
2 public class webcustomcontrol1: compositecontrol
3 {
4 Public textbox txt1 = new Textbox ();
5 Public label lab = new label ();
6 Public button B = new button ();
7 public webcustomcontrol1 ()
8 {
9 B. Text = "Click me ";
10 txt1.id = "KKK ";
11 B. Click + = new eventhandler (B _click );
12}
13
14 void B _click (Object sender, eventargs E)
15 {
16 lab. Text = txt1.text;
17}
18
19 protected override void oninit (eventargs E)
20 {
21
22
23 base. oninit (E );
24}
25
26 protected override void createchildcontrols ()
27 {
28 This. Controls. Add (txt1 );
29 This. Controls. Add (LAB );
30 this. Controls. Add (B );
31
32}
33 protected override void renderchildren (htmltextwriter writer)
34 {
35 txt1.rendercontrol (writer );
36 B. rendercontrol (writer );
37 lab. rendercontrol (writer );
38
39}
40 protected override htmltextwritertag tagkey
41 {
42 get
43 {
44 return htmltextwritertag. div;
45}
46}
Control Lifecycle
(1) initialization -- in this phase, two tasks are completed: 1. initialization of the settings required during the Web Request lifecycle; 2. Tracking the view status. First, the page framework triggers the init event by default and calls the oninit () method. The control developer can override this method to provide the initialization logic for the control. Then, the page Framework calls the trackviewstate method to track the view status. Note that the trackviewstate method provided by the control base class is sufficient in most cases. Developers can override the trackviewstate method only when the control defines complex attributes.
(2) Load view status-the main task of this phase is to check whether the server control exists and whether it needs to restore its status to the end state of the request before processing. Therefore, this process occurs in the page return process, rather than the request initialization process. In this phase, the page framework automatically restores the viewstate dictionary. If the server control does not maintain its state, or it has the ability to save all its States by default and uses the viewstate dictionary, developers do not have to implement any logic. For data types that cannot be stored in the viewstate dictionary or custom State management, developers can rewrite the loadviewstate method to customize state recovery and management.
(3) process the returned data-to enable the control to check the form data sent back by the client, you must implement the loadpostdata () method of the system. Web. UI. ipostbackdatahandler interface. Therefore, only the controls that process the returned data participate in this phase.
(4) Load ---- at this stage, the server control in the control tree has been created and initialized, the status has been restored, and the Form Control reflects the client data. In this case, developers can rewrite the onload () method to implement the common logic of each request.
(5) Send a change notification-in this phase, the server control uses an event as a signal, indicates that the status of the control changes due to sending back (so this stage is only used for sending back ). To establish this signal, developers must use the system. Web. UI. ipostbackdatahandler interface again and implement another method-raisepostbackchangedevent (). The determination process is as follows: if the control status is changed due to sending back, loadpostdata () returns true; otherwise, false. The page framework tracks all controls that return true and calls raisepostdatachangedevent () on these controls ().
(6) process the send-back event-process the client events that cause the send-back event in this phase. To map client events to server events for processing, developers can implement this logic by implementing the raisepostbackevent () method of the system. Web. UI. ipostbackeventhandler interface at this stage. In this way, the server control will successfully capture and send back client events for corresponding processing on the server side.
(7) pre-rendering-this stage completes any work required before the control is generated. Generally, the onprerender () method is rewritten to complete the task. Note that changes made to the control status in the pre-rendering phase can be saved in this phase, and changes made in the rendering phase will be lost.
(8) Save status-if the server control is not in the maintained status or has the ability to save all its statuses by default and use the viewstate dictionary, developers do not have to implement any logic at this stage. Because the process of saving the status is automatic. If the server control needs to save the custom state, or the control cannot store special data types in the viewstate dictionary, you must rewrite the saveviewstate () method to save the state.
(9) Rendering ---- indicates the process of writing markup text to the HTTP output stream. Developers can rewrite the render () method to customize the markup text on the output stream.
(10) disposal-in this phase, the dispose () method is rewritten to release references to expensive resources, such as database links.
(11) detach-the work completed is the same as that in the "dispose" phase. However, developers usually clear the work in the dispose () method without handling the unload event.
Note: Do not add the child control to page_load when adding it dynamically on the page. It can be placed in override oninit.
Asp.net Lifecycle
1. Pre-initialize object (onpreinit)
Ii. initialize the object (oninit)
3. Complete the initialization (oninitcomplete)
4. Import viewstate data (loadviewstate)
5. Use loadpostdata to process PostBack data (loadpostdata6. onpreload
7. Import object (onload)
8. raisepostbackchanged event (raisepostdatachangedevent)
9. Process client PostBack events (raisepostbackevent)
10. page_onloadcomplete11. Objects pre-submittedOnprerender
12. onprerendercomplete)
13. Save controlState (savecontrolstate)