Asp. NET component Design step by step (3)

Source: Internet
Author: User
Tags contains data structures form post garbage collection hash html tags new features reference
Getting Started with asp.net| design custom controls (custom control)

The component that executes program logic on the server side is the basic building block of the asp.net application. In order to be able to execute in the asp.net environment, certain conditions must be met.

In order to be able to participate in the web framework implementation, you need to meet IComponent, IDisposable, IParserAccessor, Idatabindingsaccessor and other interfaces. So the framework provides the System.Web.Control class for the control developer to inherit.

IComponent provides the basic functionality required by the component, and the interface is implemented as:

{

ISite Site {get; set;} Provides a site interface through which components can access the container

Event EventHandler disposed; Event

If this interface is implemented, it becomes a design component that can be added to the visual designer toolbox, can be put off onto the page (ISite interface), and properties are displayed in the property browser.

IDisposable interface implements the component's own resource release (IComponent actually inherits from IDisposable)

IParserAccessor this interface stipulates that the AddParsedSubObject (object obj) method is used to notify the server control of parsed elements (XML or HTML). When parsing an element, the element is recognized as a child of the server control that implements the interface. These elements will be converted to objects. When the interface is implemented, the control that is created defines the processing to occur immediately after it is notified of the parsed element.

The Idatabindingsaccessor interface allows you to access the collection of data-binding expressions for a control at design time.

{

DataBindingCollection DataBindings {get;} Indicates a collection of all data bindings for the control. This property is read-only.

BOOL Hasdatabindings {get;}//Return control contains any data binding logic.

}



The WebControl class that inherits from Control adds new features to the control class, such as style, font, background, foreground, and so on, to represent HTML (after all, the last user sees HTML).



Definition of Control class:

Public properties:

ClientID gets the server control identifier generated by asp.net.

Controls gets the ControlCollection object that represents the child controls of the specified server control in the UI hierarchy.

EnableViewState Gets or sets a value that indicates whether the server control maintains its own view state and the view state of any child controls it contains to the requesting client.

ID Gets or sets the programmatic identifier assigned to the server control.

NamingContainer gets a reference to the server control's naming container, which creates a unique namespace to differentiate between server controls that have the same Control.id property values.

Page gets a reference to the page instance that contains the server control.

Parent gets a reference to the parent control of the server control in the page UI hierarchy.

Site gets information about the WEB site that the server control belongs to (from MSDN, but I do not think it should be the container site for the component, not the Web site).

TemplateSourceDirectory gets the virtual directory that contains the page or UserControl of the current server control.

UniqueID gets the unique, hierarchically qualified identifier of the server control.

Visible Gets or sets a value that indicates whether the server control is rendered as a UI on the page.



Public methods

DataBind binds the data source to the invoked server control and all its child controls.

Dispose enables a server control to perform a final cleanup operation before it is freed from memory.

Equals (inherited from Object) has been overloaded. Determines whether two instances of Object are equal.

FindControl has been overloaded. Searches for the specified server control in the current naming container.

GetHashCode (inherited from Object) serves as a hash function of a specific type, and is suitable for use in hashing algorithms and data structures such as a hash table.

GetType (inherited from Object) gets the Type of the current instance.

Hascontrols determines whether the server control contains any child controls.

RenderControl outputs the contents of the server control to the provided HtmlTextWriter object, and if tracing is enabled, stores trace information about the control.

resolveurl resolves a relative URL to an absolute URL based on the value passed to the TemplateSourceDirectory property.

ToString (inherited from Object) returns a String representing the current Object.



Public events

DataBinding occurs when a server control is bound to a data source.

Disposed occurs when a server control is freed from memory, which is the last stage of the server control lifetime when the page is requested to be asp.net.

Init occurs when a server control initializes, and initialization is the first step in the lifetime of the control. server controls should perform any initialization steps required to create and set up an instance. The view state information cannot be used within the event; No other server control should be accessed during the lifetime of the event, whether it is a child or a parent of this control. It is not necessarily possible to create additional server controls or access them.

Load occurs when the server control is loaded into the Page object. Notifies the server control to perform any processing steps that are set to occur on each page request. The developer can access the view state information and use the event to form POST data. You can also access other server controls within the page control hierarchy.

PreRender occurs when the server control is to be rendered to its contained Page object. Use this event to perform any updates before the server control renders the output to the page. Any changes to the view state of the server control can be saved during the lifetime of the event. The same changes made in the rendering phase are not saved.

Unload occurs when a server control is unloaded from memory.



The protected properties and methods are:

Protected properties

ChildControlsCreated gets a value indicating whether the child controls of the server control have been created.

The context gets the HttpContext object associated with the server control for the current WEB request.

Events gets the list of event handler delegates for the control. This property is read-only.

Haschildviewstate gets a value that indicates whether the child controls of the current server control have any saved view state settings.

isTrackingViewState gets a value that indicates whether the server control saves changes to its view state.

ViewState gets a dictionary of state information that enables you to save and restore view state of a server control across multiple requests on the same page.

Viewstateignorescase gets a value that indicates whether the StateBag object is case-insensitive.

Protected methods

AddParsedSubObject notifies the server control that an element (XML or HTML) has been parsed and adds the element to the server control's ControlCollection object.

Clearchildviewstate deletes view state information for all child controls of a server control.

CreateChildControls notifications use server controls that are based on a composite implementation to create any child controls they contain to prepare for postback or rendering.

Createcontrolcollection creates a new ControlCollection object to hold the child controls of the server control, including text controls and server controls.

EnsureChildControls determines whether the server control contains child controls. If not, the child control is created.

Finalize (inherited from Object) has been overridden. Allows object to attempt to free resources and perform other cleanup operations before garbage collection reclaims Object.

Isliteralcontent determines whether the server control contains only textual content. The normal HTML tags in the asp.net page are asp.net compiled into a literalcontent control (meaning lightweight controls).

LoadViewState Restore view state information from the previous page saved by the SaveViewState method.

Mappathsecure retrieves the mapping physical file path relative to the source file if the requesting server control has sufficient security permissions to read the mapping results.

MemberwiseClone (inherited from Object) creates a shallow copy of the current object.

OnBubbleEvent determines whether the event of a server control is passed up along the page's UI server control hierarchy.

OnDataBinding raises the DataBinding event.

OnInit throws an Init event.

OnLoad raises the Load event.

OnPreRender raises the PreRender event.

OnUnload raises the Unload event. Note at this stage of the server control lifetime, the server control should perform all the last cleanup operations, such as closing the file, shutting down the database connection, and discarding the object.

RaiseBubbleEvent assigns all event sources and their information to the control's parent.

Render sends server control content to the provided HtmlTextWriter object, which writes what will be rendered on the client.

RenderChildren outputs the contents of the server control's child to the provided HtmlTextWriter object, which writes the content that will be rendered on the client.

SaveViewState saves any server control view state changes that occurred since the page was sent back to the server.

TrackViewState causes a change in the view state of the tracking server control so that the changes can be stored in the StateBag object of the server control. This object can be accessed through the Control.ViewState property.





Knowing the "cornerstone" of control, we can learn that writing server components is built on this cornerstone, and we can call methods, data, events that can be intercepted, and these data members are the "input data" we write server controls (and, of course, other input data, But the main data here is that it helps us manipulate controls and controls with external interactions, events provide the opportunity for us to "perceive" external interactions, and server controls need to be called by the container for related methods (such as render), which we can override to modify the control's default GUI representation.




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.