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.

Render基礎方法呼叫Control.Render控制項的方法。The 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.

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. 預設行為BeginRenderEndRender方法是呼叫的對應方法HtmlTextWriterThe 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. SaveAdapterControlStateSaveAdapterViewStateLoadAdapterControlState,和LoadAdapterViewState私用的控制項和檢視狀態會儲存及載入,分別時呼叫。SaveAdapterControlState, SaveAdapterViewState, LoadAdapterControlState, and LoadAdapterViewState are called when the private control and view states are saved and loaded, respectively.

OnInitOnLoadOnPreRender,以及OnUnloadbase 回在相對應的方法呼叫Control類別方法。The 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). 控制項的配接器應該實作在Adapterssubnamespace。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類別或相關ControlTypeAdapter類別。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類別。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. 在衍生的配置器類別中,產生特定目標所需的開頭標記 (而非 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 的淺層複製。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)

適用於

另請參閱