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.

每個控制項都有透過瀏覽器定義檔案的明確對應至介面卡。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呼叫基底方法, 它Control.RenderChildren會從您Render的覆寫呼叫控制項的方法。You can do this by calling the RenderChildren base method, which calls the Control.RenderChildren method of the control, from your Render override.

控制項會EndRender在控制項呼叫Render方法之前和之後, 立即呼叫和方法。BeginRenderThe BeginRender and EndRender methods are called by the control immediately before and after (respectively) the control calls the Render method. 如果前置和後置轉譯是唯一需要的瀏覽器特定處理工作, 使用BeginRenderEndRender可能會使它不需要覆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.

若要維護其本身的狀態資訊, 控制項介面卡可以SaveAdapterControlStateLoadAdapterControlStateSaveAdapterViewState、、 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類別繼承時, 需要一般介面卡功能的控制項應該具有對應的介面卡基類, 並在Adapter模式ControlType中命名 ( 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. 當沒有關聯的介面卡null時, 控制項的屬性就是在呼叫介面卡的方法之前,任何程式碼都應該檢查該條件。AdapterThe 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. 在衍生的配置器類別中,產生特定目標所需的開頭標記 (而非 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. 在衍生的配置器類別中,產生特定目標所需的結尾標記 (而非 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 的淺層複本 (Shallow Copy)。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)

適用於

另請參閱