ControlAdapter 類別

定義

為附加配置器的衍生控制項自訂呈現方式,以針對特定瀏覽器修改預設標記或行為,而且此類別為所有控制項配置器繼承來源的基底類別。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
繼承
ControlAdapter
衍生

備註

控制項介面卡是 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). 轉譯行為的大部分適應性都可以封裝在衍生自類別的特製化類別中 HtmlTextWriterMuch 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.

介面卡會透過方法來執行控制項的呈現 RenderThe adapter performs rendering for the control through the Render method. 如果已覆寫, Render 可能不應呼叫基類實作為,因為這會在方法上執行回呼 Control.RenderIf 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.

Render基底方法會回檔控制項的 Control.Render 方法。The Render base method calls back on the Control.Render method of the control. 因此,如果您覆寫 Render ,除非您所執行的轉譯是由控制項所提供的,否則您不應該呼叫基類實作為 Control.RenderThus, 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 RenderYou can do this by calling the RenderChildren base method, which calls the Control.RenderChildren method of the control, from your Render override.

BeginRenderEndRender 方法是由控制項在 (的前後呼叫,) 控制項呼叫 Render 方法。The BeginRender and EndRender methods are called by the control immediately before and after (respectively) the control calls the Render method. 如果預先轉譯和之後轉譯是唯一需要的瀏覽器特定處理工作,使用 BeginRenderEndRender 可能會使其不需要覆寫 RenderIf pre- and post-rendering are the only browser-specific processing tasks required, using BeginRender and EndRender might make it unnecessary to override Render. 和方法的預設行為 BeginRender EndRender 是呼叫的對應方法 HtmlTextWriterThe default behavior of the BeginRender and EndRender methods is to call the corresponding methods of the HtmlTextWriter.

若要維護其本身的狀態資訊,控制項介面卡可以覆寫 SaveAdapterControlState 、、 LoadAdapterControlState SaveAdapterViewStateLoadAdapterViewState 方法。To maintain its own state information, a control adapter can override the SaveAdapterControlState, LoadAdapterControlState, SaveAdapterViewState, and LoadAdapterViewState methods. SaveAdapterControlStateSaveAdapterViewState LoadAdapterControlState LoadAdapterViewState 分別儲存和載入私用控制項和檢視狀態時,會呼叫、、和。SaveAdapterControlState, SaveAdapterViewState, LoadAdapterControlState, and LoadAdapterViewState are called when the private control and view states are saved and loaded, respectively.

OnInitOnLoadOnPreRenderOnUnload 基底方法會回呼對應的 Control 類別方法。The OnInit, OnLoad, OnPreRender, and OnUnload base methods call back on the corresponding Control class methods. 因此,任何覆 ControlAdapter 寫的方法都必須呼叫其基底方法,否則不會引發與類別方法相關聯的事件 ControlThus, 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. 如果有的話,介面卡應該 LoadPostData 視需要覆寫、 RaisePostDataChangedEventRaisePostBackEvent 方法。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). 介面卡至少應該透過其屬性傳回強型別的控制項實例 ControlThe 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). 控制項的介面卡應該在 somenamespace.subnamespace 中執行 AdaptersAdapters 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.

應針對設定檔中所有裝置節點下的專用控制項定義任何特製化的介面卡。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. 介面卡物件可透過類別的 protected Adapter 屬性取得 ControlThe 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 類別的新執行個體。Initializes a new instance of the ControlAdapter class.

屬性

Browser

取得對用戶端瀏覽器功能的參考 (此用戶端是發出目前 HTTP 要求的用戶端)。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.

方法

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()

為複合控制項建立特定目標的子控制項。Creates the target-specific child controls for a composite control.

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)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 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)

載入之前在對頁面發出要求的期間由 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()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
OnInit(EventArgs)

為關聯控制項覆寫 OnInit(EventArgs) 方法。Overrides the OnInit(EventArgs) method for the associated control.

OnLoad(EventArgs)

為關聯控制項覆寫 OnLoad(EventArgs) 方法。Overrides the OnLoad(EventArgs) method for the associated control.

OnPreRender(EventArgs)

為關聯控制項覆寫 OnPreRender(EventArgs) 方法。Overrides the OnPreRender(EventArgs) method for the associated control.

OnUnload(EventArgs)

為關聯控制項覆寫 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.

(繼承來源 Object)

適用於

另請參閱