ControlAdapter ControlAdapter ControlAdapter ControlAdapter Class

定义

自定义附加了适配器的派生控件的呈现,以修改特定浏览器的默认标记或行为,是所有控件适配器都可以继承的基类。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
继承
ControlAdapterControlAdapterControlAdapterControlAdapter
派生

注解

控件适配器是重写其执行Control生命周期中的某些类方法和事件以允许浏览器或特定于标记的处理的组件。Control adapters are components that override certain Control class methods and events in its execution lifecycle to allow browser or markup-specific handling. .NET Framework 将单个派生控件适配器映射到Control每个客户端请求的对象。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. 通常, 适配器是由浏览器使用的标记语言 (例如, XHTML 或 HTML 3.2) 定义的。Typically the adapter is defined by the markup language that the browser uses (for example, XHTML or HTML 3.2). 呈现行为中的大部分适应性都可以封装在派生自HtmlTextWriter类的专用类中。Much of the adaptability in rendering behavior can be encapsulated in the specialized classes that derive from the HtmlTextWriter class. 因此, 可以将单个适配器用于许多浏览器类行为, 或在HtmlTextWriter类中包含适应性, 这可能导致不必要地使用控件适配器。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. 例如, BaseValidator类的适配器可用于所有Validator对象。For example, an adapter for the BaseValidator class can be used for all Validator objects.

适配器通常不直接从ControlAdapter类继承, 而是从特定于目标的适配器基类之一, 这些基类提供特定于控件类型和目标浏览器或所需的特定呈现的附加功能。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.

每个控件都通过 .browser 定义文件显式映射到适配器。Each control has explicit mappings to adapters through the .browser definition files. 因此, 对Control.Adapter属性的任何访问都HttpBrowserCapabilities使用从浏览器定义文件中提取的对象来执行要控制的适配器映射。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.

在处理过程中, .NET Framework 会截获对可能特定于目标的控件的可重写方法的调用。During processing, the .NET Framework intercepts calls to the overridable methods of a control that could be target-specific. 如果附加了控件适配器, .NET Framework 将调用关联的适配器方法。If a control adapter is attached, the .NET Framework calls the associated adapter methods.

适配器通过Render方法为控件执行呈现。The adapter performs rendering for the control through the Render method. 如果重写Render , 则可能不应调用基类实现, 因为这会Control.Render在方法上执行回调。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.

基方法回调控件的Control.Render方法。 RenderThe Render base method calls back on the Control.Render method of the control. 因此, 如果你重Render写, 则不应调用基类实现, 除非你实现的呈现除了由Control.Render控件提供的。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.

必须确保 .NET Framework 为子控件的适配器执行侦听。You must ensure that the .NET Framework performs interception for adapters of the child controls. RenderChildren此, 可以调用基方法, 该方法从Render重写Control.RenderChildren调用控件的方法。You can do this by calling the RenderChildren base method, which calls the Control.RenderChildren method of the control, from your Render override.

和方法由控件在控件调用Render方法之前和之后立即调用。 EndRender BeginRenderThe BeginRender and EndRender methods are called by the control immediately before and after (respectively) the control calls the Render method. 如果预呈现和后期呈现是必需的特定于浏览器的处理任务, 则BeginRender使用EndRender和可能会使其不Render需要重写。If pre- and post-rendering are the only browser-specific processing tasks required, using BeginRender and EndRender might make it unnecessary to override Render. BeginRender HtmlTextWriter和方法的默认行为是调用的对应方法EndRenderThe default behavior of the BeginRender and EndRender methods is to call the corresponding methods of the HtmlTextWriter.

为了维护其自己的状态信息, 控件适配器可以重写SaveAdapterControlState SaveAdapterViewStateLoadAdapterControlState、和LoadAdapterViewState方法。To maintain its own state information, a control adapter can override the SaveAdapterControlState, LoadAdapterControlState, SaveAdapterViewState, and LoadAdapterViewState methods. SaveAdapterControlState分别SaveAdapterViewState保存LoadAdapterControlState和加载专用控件和视图状态时, 将调用、、和LoadAdapterViewStateSaveAdapterControlState, SaveAdapterViewState, LoadAdapterControlState, and LoadAdapterViewState are called when the private control and view states are saved and loaded, respectively.

OnInit、 、和OnPreRender基方法回调相应Control的类方法。 OnUnload OnLoadThe OnInit, OnLoad, OnPreRender, and OnUnload base methods call back on the corresponding Control class methods. 因此, 重写的ControlAdapter这些方法中的任何一种都必须调用它们的基方法; 否则, Control将不会引发与类方法关联的事件。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.

控件和适配器可选择实现IPostBackDataHandlerIPostBackEventHandler接口。Controls and adapters optionally implement the IPostBackDataHandler and IPostBackEventHandler interfaces. .NET Framework 确定适配器是否存在以及适配器是否实现这些接口。The .NET Framework determines whether an adapter exists and whether the adapter implements these interfaces. 如果是这样, 则适配器应根据需要LoadPostDataRaisePostDataChangedEvent写、 RaisePostBackEvent和方法。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.

继承者说明

ControlAdapter类继承时, 需要常规适配器功能的控件应具有相应的适配器基类, 该基类在模式ControlType Adapter中命名 (例如TextBoxAdapter)。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 ControlTypeAdapter (for example, TextBoxAdapter). 适配器应最少通过其Control属性返回控件的强类型实例。The adapter should at a minimum return a strongly-typed instance of the control through its Control property.

1.1. 给定控件类型和标记语言的控件适配器应以模式MarkupControlType Adapter命名 (例如XhtmlTextBoxAdapter)。Control adapters for a given control type and markup language should be named in the pattern MarkupControlTypeAdapter (for example, XhtmlTextBoxAdapter). 控件的适配器应在Adapters subnamespace 中实现。Adapters for a control should be implemented in an Adapters subnamespace.

控件适配器应从适当的基类继承, 并遵循与控件相同的继承模型。Control adapters should inherit from the appropriate base class and follow the same inheritance model as the control. 例如, 从Control基类继承的控件的适配器应继承自ControlAdapter类或相关ControlType Adapter类。For example, an adapter for a control inheriting from the Control base class should inherit from either the ControlAdapter class or the relevant ControlTypeAdapter class.

应为配置 .browser 文件中所有设备节点下的专用控件定义专用适配器。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.

可以模拟在控件中重写受保护的事件方法, 如OnClick(EventArgs)的方法。 LinkButtonIt is possible to simulate overriding protected event methods in the control, such as the OnClick(EventArgs) method of the LinkButton. 首先, 使用OnClick方法创建适配器类。First, create an adapter class with an OnClick method. 然后, 创建一个从派生的LinkButton新控件, OnClick(EventArgs)并重写方法。Then create a new control derived from LinkButton and override the OnClick(EventArgs) method. 重写OnClick(EventArgs)的方法调用适配器的OnClick方法。The overridden OnClick(EventArgs) method calls the OnClick method of the adapter. 适配器对象通过Adapter Control类的 protected 属性提供。The adapter object is available through the protected Adapter property of the Control class. Adapter没有关联的适配器时null , 控件的属性为, 因此任何代码都应在调用适配器的方法之前检查该条件。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.

构造函数

ControlAdapter() ControlAdapter() ControlAdapter() ControlAdapter()

初始化 ControlAdapter 类的新实例。Initializes a new instance of the ControlAdapter class.

属性

Browser Browser Browser Browser

获取对发出当前 HTTP 请求的客户端的浏览器功能的引用。Gets a reference to the browser capabilities of the client making the current HTTP request.

Control Control Control Control

获取对附加了此控件适配器的控件的引用。Gets a reference to the control to which this control adapter is attached.

Page Page Page Page

获取对与此适配器关联的控件所驻留的页的引用。Gets a reference to the page where the control associated with this adapter resides.

PageAdapter PageAdapter PageAdapter PageAdapter

获取对关联控件所驻留的页的页适配器的引用。Gets a reference to the page adapter for the page where the associated control resides.

方法

BeginRender(HtmlTextWriter) BeginRender(HtmlTextWriter) BeginRender(HtmlTextWriter) BeginRender(HtmlTextWriter)

在呈现控件前调用。Called prior to the rendering of a control. 在派生的 adapter 类中,生成特定目标需要但 HTML 浏览器不需要的开始标记。In a derived adapter class, generates opening tags that are required by a specific target but not needed by HTML browsers.

CreateChildControls() CreateChildControls() CreateChildControls() CreateChildControls()

为复合控件创建特定于目标的子控件。Creates the target-specific child controls for a composite control.

EndRender(HtmlTextWriter) EndRender(HtmlTextWriter) EndRender(HtmlTextWriter) EndRender(HtmlTextWriter)

在呈现控件后调用。Called after the rendering of a control. 在派生的 adapter 类中,生成特定目标需要但 HTML 浏览器不需要的结束标记。In a derived adapter class, generates closing tags that are required by a specific target but not needed by HTML browsers.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
LoadAdapterControlState(Object) LoadAdapterControlState(Object) LoadAdapterControlState(Object) LoadAdapterControlState(Object)

加载适配器控件状态信息,该信息由 SaveAdapterControlState() 在以前请求与此控件适配器关联的控件所驻留的页时保存。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) LoadAdapterViewState(Object) LoadAdapterViewState(Object) LoadAdapterViewState(Object)

加载适配器视图状态信息,该信息由 SaveAdapterViewState() 在以前请求与此控件适配器关联的控件所驻留的页时保存。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() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
OnInit(EventArgs) OnInit(EventArgs) OnInit(EventArgs) OnInit(EventArgs)

重写关联控件的 OnInit(EventArgs) 方法。Overrides the OnInit(EventArgs) method for the associated control.

OnLoad(EventArgs) OnLoad(EventArgs) OnLoad(EventArgs) OnLoad(EventArgs)

重写关联控件的 OnLoad(EventArgs) 方法。Overrides the OnLoad(EventArgs) method for the associated control.

OnPreRender(EventArgs) OnPreRender(EventArgs) OnPreRender(EventArgs) OnPreRender(EventArgs)

重写关联控件的 OnPreRender(EventArgs) 方法。Overrides the OnPreRender(EventArgs) method for the associated control.

OnUnload(EventArgs) OnUnload(EventArgs) OnUnload(EventArgs) OnUnload(EventArgs)

重写关联控件的 OnUnload(EventArgs) 方法。Overrides the OnUnload(EventArgs) method for the associated control.

Render(HtmlTextWriter) Render(HtmlTextWriter) Render(HtmlTextWriter) Render(HtmlTextWriter)

为附加了控件适配器的控件生成目标特定的标记。Generates the target-specific markup for the control to which the control adapter is attached.

RenderChildren(HtmlTextWriter) RenderChildren(HtmlTextWriter) RenderChildren(HtmlTextWriter) RenderChildren(HtmlTextWriter)

为附加了控件适配器的复合控件中的子控件生成特定于目标的标记。Generates the target-specific markup for the child controls in a composite control to which the control adapter is attached.

SaveAdapterControlState() SaveAdapterControlState() SaveAdapterControlState() SaveAdapterControlState()

保存控件适配器的控件状态信息。Saves control state information for the control adapter.

SaveAdapterViewState() SaveAdapterViewState() SaveAdapterViewState() SaveAdapterViewState()

保存控件适配器的视图状态信息。Saves view state information for the control adapter.

ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

适用于

另请参阅