ControlAdapter Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Customizes rendering for the derived control to which the adapter is attached, to modify the default markup or behavior for specific browsers, and is the base class from which all control adapters inherit.
public ref class ControlAdapter abstract
public abstract class ControlAdapter
type ControlAdapter = class
Public MustInherit Class ControlAdapter
- Inheritance
-
ControlAdapter
- Derived
Remarks
Control adapters are components that override certain Control class methods and events in its execution lifecycle to allow browser or markup-specific handling. The .NET Framework maps a single derived control adapter to a Control object for each client request.
An adapter modifies a control for a specific browser or class of browsers or acts as an arbitrary filter on some capability. Typically the adapter is defined by the markup language that the browser uses (for example, XHTML or HTML 3.2). Much of the adaptability in rendering behavior can be encapsulated in the specialized classes that derive from the HtmlTextWriter class. Therefore, it is likely that a single adapter can be used for a number of browser class behaviors or that inclusion of the adaptability in the HtmlTextWriter classes could make the use of a control adapter unnecessary.
An adapter for a control class applies to all controls that inherit from that class, unless more specialized adapters are present. For example, an adapter for the BaseValidator class can be used for all Validator
objects.
Adapters typically do not inherit directly from the ControlAdapter class, but from one of the target-specific adapter base classes that provide additional functionality specific to the control type and target browser or the particular rendering required.
Controls themselves do not necessarily require an adapter. If controls are extended through composition, generally the child control adapters are sufficient.
Each control has explicit mappings to adapters through the .browser definition files. Thus, any access to the Control.Adapter property uses the HttpBrowserCapabilities object extracted from the browser definition files to perform the lookup for the mapping of the adapter to control.
During processing, the .NET Framework intercepts calls to the overridable methods of a control that could be target-specific. If a control adapter is attached, the .NET Framework calls the associated adapter methods.
The adapter performs rendering for the control through the Render method. If overridden, Render potentially should not call the base class implementation because that performs a call back on the Control.Render method. This might cause the rendering to occur twice, once by the adapter and once by the control.
The Render base method calls back on the Control.Render method of the control. Thus, if you override Render, you should not call the base class implementation unless the rendering you implement is in addition to that provided by Control.Render of the control.
You must ensure that the .NET Framework performs interception for adapters of the child controls. You can do this by calling the RenderChildren base method, which calls the Control.RenderChildren method of the control, from your Render override.
The BeginRender and EndRender methods are called by the control immediately before and after (respectively) the control calls the Render method. If pre- and post-rendering are the only browser-specific processing tasks required, using BeginRender and EndRender might make it unnecessary to override Render. The default behavior of the BeginRender and EndRender methods is to call the corresponding methods of the HtmlTextWriter.
To maintain its own state information, a control adapter can override the SaveAdapterControlState, LoadAdapterControlState, SaveAdapterViewState, and LoadAdapterViewState methods. SaveAdapterControlState, SaveAdapterViewState, LoadAdapterControlState, and LoadAdapterViewState are called when the private control and view states are saved and loaded, respectively.
The OnInit, OnLoad, OnPreRender, and OnUnload base methods call back on the corresponding Control class methods. Thus, any of these ControlAdapter methods that are overridden must call their base methods; otherwise, the event associated with the Control class method will not be raised.
Controls and adapters optionally implement the IPostBackDataHandler and IPostBackEventHandler interfaces. The .NET Framework determines whether an adapter exists and whether the adapter implements these interfaces. If it does, the adapter should override the LoadPostData, RaisePostDataChangedEvent, and RaisePostBackEvent methods, as necessary. If the postback data is not recognized in the adapter, it must call back on the control to process it. Subsequent event handlers also must call back on the control.
Notes to Implementers
When you inherit from the ControlAdapter class, a control that requires general adapter functionality should have a corresponding adapter base class, named in the pattern ControlType
Adapter
(for example, TextBoxAdapter
). The adapter should at a minimum return a strongly-typed instance of the control through its Control property.
- Control adapters for a given control type and markup language should be named in the pattern
MarkupControlType
Adapter
(for example,XhtmlTextBoxAdapter
). Adapters for a control should be implemented in anAdapters
subnamespace.
Control adapters should inherit from the appropriate base class and follow the same inheritance model as the control. For example, an adapter for a control inheriting from the Control base class should inherit from either the ControlAdapter class or the relevant ControlType
Adapter
class.
Any specialized adapters should be defined for the specialized control under all of the device nodes in configuration .browser files.
A properly implemented control should not assume that an adapter is attached, or that the attached adapter implements a specific interface. Instead, it should check for these before calling.
It is possible to simulate overriding protected event methods in the control, such as the OnClick(EventArgs) method of the LinkButton. First, create an adapter class with an OnClick
method. Then create a new control derived from LinkButton and override the OnClick(EventArgs) method. The overridden OnClick(EventArgs) method calls the OnClick
method of the adapter. The adapter object is available through the protected Adapter property of the Control class. The Adapter property of the control is null
when there is no associated adapter, so any code should check for that condition before calling methods of the adapter.
Constructors
ControlAdapter() |
Initializes a new instance of the ControlAdapter class. |
Properties
Browser |
Gets a reference to the browser capabilities of the client making the current HTTP request. |
Control |
Gets a reference to the control to which this control adapter is attached. |
Page |
Gets a reference to the page where the control associated with this adapter resides. |
PageAdapter |
Gets a reference to the page adapter for the page where the associated control resides. |
Methods
BeginRender(HtmlTextWriter) |
Called prior to the rendering of a control. In a derived adapter class, generates opening tags that are required by a specific target but not needed by HTML browsers. |
CreateChildControls() |
Creates the target-specific child controls for a composite control. |
EndRender(HtmlTextWriter) |
Called after the rendering of a control. In a derived adapter class, generates closing tags that are required by a specific target but not needed by HTML browsers. |
Equals(Object) |
Determines whether the specified object is equal to the current object. (Inherited from Object) |
GetHashCode() |
Serves as the default hash function. (Inherited from Object) |
GetType() |
Gets the Type of the current instance. (Inherited from Object) |
LoadAdapterControlState(Object) |
Loads adapter control state information that was saved by SaveAdapterControlState() during a previous request to the page where the control associated with this control adapter resides. |
LoadAdapterViewState(Object) |
Loads adapter view state information that was saved by SaveAdapterViewState() during a previous request to the page where the control associated with this control adapter resides. |
MemberwiseClone() |
Creates a shallow copy of the current Object. (Inherited from Object) |
OnInit(EventArgs) |
Overrides the OnInit(EventArgs) method for the associated control. |
OnLoad(EventArgs) |
Overrides the OnLoad(EventArgs) method for the associated control. |
OnPreRender(EventArgs) |
Overrides the OnPreRender(EventArgs) method for the associated control. |
OnUnload(EventArgs) |
Overrides the OnUnload(EventArgs) method for the associated control. |
Render(HtmlTextWriter) |
Generates the target-specific markup for the control to which the control adapter is attached. |
RenderChildren(HtmlTextWriter) |
Generates the target-specific markup for the child controls in a composite control to which the control adapter is attached. |
SaveAdapterControlState() |
Saves control state information for the control adapter. |
SaveAdapterViewState() |
Saves view state information for the control adapter. |
ToString() |
Returns a string that represents the current object. (Inherited from Object) |
Applies to
See also
Commentaires
https://aka.ms/ContentUserFeedback.
Bientôt disponible : Tout au long de l’année 2024, nous abandonnerons progressivement le mécanisme de retour d’information GitHub Issues pour le remplacer par un nouveau système de commentaires. Pour plus d’informations, consultez :Soumettre et afficher des commentaires pour