Custom Control (CUSTOM Control)
The component logic is executed in the server side, which is the basic construction block of the ASP.NET application. In order to be executed in the ASP.NET environment, certain conditions must be met.
In order to be able to participate in the web frame execution, you need to meet interfaces such as IComponent, IDisposable, iParsractor, iDatabasesAccessor. So the framework provides System.Web.Control to the controller developers inherit.
IComponent provides basic functions required for components, and interface implementation is:
{
Isite Site {get; set;} // Provide site interface, component can access containers via this interface
Event EventHandler Disposed; // Event
} If this interface is implemented, it becomes a designable component, which can be added to the visual designer toolbox, which can be released into the page (ISITE interface), and the properties are displayed in the Properties browser.
Idisposable interface implementation component self-resource release (IComponent inherits from idisposable)
IParseseraccessor This interface specifies the AddPARSUBOBIECT (Object Obj) method to notify the server controls analyzed elements (XML or HTML). When analyzing elements, the element is identified as a child of a server control that implements the interface. These elements will be converted to objects. When this interface is implemented, the created control is immediately defined immediately after the analyzed element is notified.
The IDATABINDINGSACCESSOR interface allows the data binding expression collection of the control to access the control during design.
{
Database {get;} / / indicate a collection of all data bindings of the control. This property is read-only.
Bool HasDataBindings {get;} // Returns whether the control contains any data binding logic.
}
Inheriting the WebControl class from Control to express HTML (after all, the last user see html) and add some new features in the Control class, such as style, font, background, prospect. . .
Control class definition:
Public property:
ClientID gets the server control identifier generated by the ASP.NET.
Controls Get the ControlCollection object that represents the child control of the specified server control in the UI hierarchy.
EnableViewState Gets or sets a value indicating whether the server control keeps its own view status to the requesting client and the view status of any sub-control it contains.
ID Get or sets the programming identifier assigned to the server control.
NamingContainer Gets a reference to the name container for the server control, which creates a unique namespace to distinguish between server controls with the same control .id attribute value.
Page Gets a reference to the Page instance containing the server control.
Parent Gets a reference to the parent control of the server control in the page UI hierarchy.
Site gains information about the Web site of the server control (original from MSDN, but I think it is wrong, it should refer to the "container" site of the component, not a web site).
TemplatesourceDirectory gains the virtual directory that contains the Page or UserControl of the current server control.
UNIQUEID Gets the unique identifier of the server control to a hierarchical form.
Visible Gets or sets a value indicating whether the server control is presented on the page as a UI.
Public approach
Database binds the data source to the called server control and all its child controls.
Dispose enables server controls to perform the last cleaning operation before release from memory. Equals (inherited from Object) has been overloaded. Determine if the two Object instances are equal.
FindControl has been overloaded. Search for specified server controls in the current naming container.
GetHashCode (inherited from Object) is used as a specific type of hash function, which is suitable for use in a hash algorithm and data structure (such as a hash table).
GetType (inherited from Object) Gets the TYPE of the current instance.
HasControls determines if the server control contains any child controls.
RenderControl outputs the contents of the server control to the HTMLTextWriter object provided; if the trace function is enabled, store the tracking information about the control.
Resolveurl resolves the relative URL to an absolute URL based on the value passed to the TemplatesourCedirectory property.
Tostring returns a String that represents the current object.
Public event data occurs when the server control is bound to the data source. Disposed occurs when the server control is released from the memory, which is the final phase of the server control life time when the ASP.NET page is requested. INIT occurs when the server control is initialized; initialization is the first step in the survival of the control. Server controls should perform any initialization steps required to create and set instances. View status information cannot be used within the event; it has not been filled. Other server controls should not be accessed within the survival of the event, whether it is a child or parent of this control. Do not create other server controls, and do not necessarily access them. LOAD occurs when the server control is loaded into the Page object. The notification server control performs any processing steps that occur when requesting each page request. Developers can access view status information and use this event to form POST data. You can also access other server controls within the hierarchy of the page control. Prerender occurs when the server control will appear to the Page object included. Use this event to perform any updates before the server control is presented to the page. Any changes to the server control view status can be saved during the survival of the event. Do not save the same changes in the presentation phase. Unload occurs when the server control is uninstalled from memory.
Protected properties and methods include: Protected properties ChildControlScreated Get a value that indicates whether the sub-control of the server control has been created. Context is the HTTPContext object that is associated with the server control for the current Web request. Events Gets the event handler delegate list. This property is read-only. HaschildViewState gets a value that indicates whether the child control of the current server control has any saved view status settings. IStrackingViewState gets a value that indicates whether the server control saves changes to its view status. ViewState Gets the status information, which allows you to save and restore the view status of the server control between multiple requests between the same page. ViewStateIgnoreScase gets a value indicating whether the StateBag object is not case sensitive. Protected Method AddParsedSubObject Notification Server Controls Elements (XML or HTML) have passed the grammatical analysis and add this element to the ControlCollection object of the server control. ClearChildViewState deletes view status information for all subcipes of the server control. The CreateChildControls notifies the use of a server control based on the synthetic implementation to create any child controls they contain to prepare for retrace or presentation. CreateControlCollection creates a new ControlCollection object to save the subcipes of the server control (including text controls and server controls). EnSureChildControls determines if the server control contains a child control. If not included, create a child control. Finalize (inherited from Object) has been overwritten. Allow Object to try to release resources and perform other cleaning operations before "garbage recycling" recycle Object. IsliteralContent determines if the server control contains only text content. The ordinary HTML tag in the ASP.NET page is compiled as a LiteralContent control (meant light control) by ASP.NET. LoadViewState Restores view status information from the last page saved from the SaveViewState method. MappathSecure If the request server control has sufficient security permission to read the mapping result, retrieve the mapping physical file path relative to the source file. Memberwiseclone (inherited from Object) Creates a shallow copy of the current Object. Onbubbleevent determines whether the event of the server control is passed upward along the UI server control hierarchy along the page. OndataBinding triggers a DataBinding event. OnInit triggers an init event. ONLOAD triggers the LOAD event. OnPrender triggers a prerender event. ONUNLOAD triggers UNLOAD event. Note At this stage of the server control life, the server control should perform all the last cleaning operations, such as turning off files, closing the database connection and discarding objects. RaisebubbleEvent assigns all event sources and their information to the parent of the control. Render sends the server control content to the provided HTMLTextWriter object, this object writes the content that will be rendered on the client. RenderChildren outputs the contents of the server control sub-level to the supplied HTMLTextWriter object, this object writes the content that will be rendered on the client. SaveViewState saves the status of any server control view status changes that are rapid to the server. TrackViewState leads to the change of the view status of the server control so that these changes can be stored in the StateBag object of the server control. This object can be accessed by the Control.ViewState property.