Introduction to ASP. NET Custom Controls

Source: Internet
Author: User
Tags form post

What do I need to know about ASP. NET custom controls? Let's start with the story:

The necessity of ASP. NET custom controls: the component that executes program logic on the server side is the basic construction block of ASP. NET applications. Certain conditions must be met for execution in the ASP. NET environment.

To be able to participate in the execution of the webpage framework, the IComponent, IDisposable, IParserAccessor, IdataBindingsAccessor, and other interfaces must be met. Therefore, the Framework provides the System. Web. Control class for Control developers to inherit.

 
 
  1. IComponent// Provides the basic functions required by the component. The interface is implemented as follows: 
  2.  
  3. {
  4.  
  5. ISite Site {Get;Set;}// Provides the site interface through which components can access the container 
  6.  
  7. EventEventHandler Disposed;// Event 
  8.  
  9. }

If this interface is implemented, it becomes a component that can be designed. You can add it to the visualization Designer toolbox, which can be removed from the Isite interface on the page) and display the properties in the property browser.

The IDisposable interface is used to release component resources. Icomponent actually inherits from IDisposable)

IParserAccessor this interface requires AddParsedSubObjectobject obj) to notify the Server Control of XML or HTML elements that have been analyzed ). When analyzing an element, the element is recognized as a child of the server control that implements this interface. These elements are converted to objects. When this interface is implemented, the created control immediately defines the processing to happen after being notified of the analyzed elements.

 
 
  1. IDataBindingsAccessor// The interface allows access to the data binding expression set of the control during design. 
  2.  
  3. {
  4.  
  5. DataBindingCollection DataBindings {get ;}// Indicates the set of all data bound to the control. This attribute is read-only. 
  6.  
  7. BoolHasDataBindings {get ;}// Whether the return control contains any data binding logic. 
  8.  
  9. }

The WebControl class inherited from Control represents HTML (after all, the user sees HTML) and adds some new functions to the Control class, such as style, Font, background, and foreground...

Definition of Control class for ASP. NET custom controls:

◆ Public attributes:

ClientID gets the Server Control identifier generated by ASP. NET.

Controls gets the ControlCollection object, which indicates the child control of the specified server control in the UI hierarchy.

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

ID.

NamingContainer gets a reference to the name container of the server Control. This reference creates a unique namespace to distinguish the server Control with the same Control. ID attribute value.

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.

The Site gets the original information about the Web site of the server control from MSDN, but I don't think it is correct. It should be the "Container" Site of the component, not the web site ).

TemplateSourceDirectory: Obtain the virtual directory of the Page or UserControl that contains the current server control.

UniqueID obtains the unique identifier of the server control in hierarchical form.

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

◆ Public methods

DataBind binds the data source to the called Server Control and all its child controls.

Dispose allows the Server Control to perform the final cleanup operation before being released from the memory.

Equals inherited from Object) is overloaded. Determine whether the two Object instances are equal.

FindControl is overloaded. Search for the specified server control in the current named container.

GetHashCode inherited from objects) is used as a hash function of a specific type. It is applicable to hash algorithms and data structures such as hash tables.

GetType inherited from Object) to get the Type of the current instance.

HasControls determines whether the server control contains any child control.

RenderControl outputs the content of the server control to the provided HtmlTextWriter object. If the tracking function is enabled, the tracking information about the control is stored.

ResolveUrl resolves the relative URL to an absolute URL based on the value passed to the TemplateSourceDirectory attribute.

Returns the String of the current Object.

◆ Public Events

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

Disposed occurs when the server control is released from the memory. This is the final stage of the server control survival when ASP. NET pages are requested.

Init occurs when the server control is initialized. Initialization is the first step of the control's survival. The server control should perform any initialization steps required to create and set instances. View status information cannot be used in this event; it is not filled yet. Other server controls should not be accessed during the lifetime of the event, whether it is a child or parent level of the control. Other server controls may not be created or accessed.

Load occurs when the server control is loaded to the Page object. Notify the Server Control to perform any processing steps that are set to occur each page request. Developers can access view status information and use this event to form POST data. You can also access other server controls in the page control hierarchy.

PreRender occurs when the server control is to be rendered to the Page object it contains. Use this event to execute any updates before the Server Control displays the page output. During the lifetime of the event, you can save any changes to the server control view status. Do not save the same changes made during the rendering phase.

Unload occurs when the server control is detached from the memory.

ASP. NET custom controls are protected by the following attributes and methods:

◆ Protected attributes

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

Context is the HTTP Context object associated with the server control for the current Web request.

Events obtains the event handler delegate list of the control. This attribute 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: A Dictionary of State information that allows you to save and restore the view State of the server control between 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 analyzed in syntax and added to the ControlCollection object of the server control.

ClearChildViewState deletes the view status information of all child controls of the server control.

CreateChildControls notifies you to create any child controls they contain using the server controls implemented based on synthesis to prepare for sending back or rendering.

CreateControlCollection creates a new ControlCollection object to save child controls of server controls, including text controls and server controls ).

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

Finalize inherited from Object) is overwritten. Allow objects to try to release resources and perform other cleanup operations before they are recycled.

IsLiteralContent determines whether the server control only contains text. Common html tags on ASP. NET pages are compiled into a LiteralContent control by ASP. NET, which means a lightweight control ).

LoadViewState requests to restore the view status information from the previous page saved by the SaveViewState method.

MapPathSecure: if the request Server Control has sufficient security permissions to read the ing result, retrieve the mapped physical file path relative to the source file.

Creates a superficial copy of the current Object.

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

OnDataBinding triggers a DataBinding event.

OnInit triggers the Init event.

OnLoad triggers the Load event.

OnPreRender triggers the PreRender event.

OnUnload triggers the Unload event. Note that during this stage of server control survival, the server control should perform all final cleanup operations, such as closing files, closing database connections, and dropping objects.

RaiseBubbleEvent assigns all event sources and their information to the parent level of the control.

Render sends the server control content to the provided HtmlTextWriter object, which is written in the content that will be presented on the client.

RenderChildren outputs the child-level content of the server control to the provided HtmlTextWriter object, which is written in the client.

SaveViewState saves any server control view status changes that occur after the page is sent back to the server.

TrackViewState causes the trail to change the view status of the server control so that these changes can be stored in the StateBag object of the server control. You can access this object through the Control. ViewState attribute.

After learning about the "cornerstone" of Control, we can understand that writing server components is built on this cornerstone. At the same time, we can call methods, data, and intercepted events, the above data member is the "input data" of the server control. Of course, there can be other input data, but the main data is here). The above method is to help us control the control and control interaction with the external, events give us the opportunity to "perceive" external interactions. In addition, server controls also need to be called by containers, such as render). We can rewrite these methods to modify the default GUI performance of Control.

The introduction to ASP. NET custom controls is here to help you learn ASP. NET custom controls.

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.