DataBoundControlAdapter 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
針對特定瀏覽器要求,自訂與控制項配置器關聯的 DataBoundControl 物件之行為。
public ref class DataBoundControlAdapter : System::Web::UI::WebControls::Adapters::WebControlAdapter
public class DataBoundControlAdapter : System.Web.UI.WebControls.Adapters.WebControlAdapter
type DataBoundControlAdapter = class
inherit WebControlAdapter
Public Class DataBoundControlAdapter
Inherits WebControlAdapter
- 繼承
範例
下列程式碼範例會建立和使用兩個衍生控制項:
衍生自 DataBoundControl 的
MyDataBound
類別是簡單的唯讀方格控制項。衍生自 DataBoundControlAdapter 的
MyDataBoundAdapter
類別會將方格資料轉譯為具有資料列分隔符號的一維清單,適用于小型螢幕瀏覽器。
第一個程式碼範例會使用網頁來宣告 MyDataBound
控制項和 的 ObjectDataSource 實例,該控制項會以 物件的形式 DataView 提供資料。
第二個程式碼範例包含衍生 MyDataBound
的 和 MyDataBoundAdapter
類別:
類別
MyDataBound
會 PerformDataBinding 覆寫 方法以儲存 IEnumerator 資料來源的集合,並覆寫 RenderContents 方法,將資料來源轉譯為 HTML<table>
。類別
MyDataBoundAdapter
會 PerformDataBinding 覆寫 ,以將資料來源儲存至一維 ArrayList 並加入資料列分隔符號。 它會覆寫 RenderContents ,以轉 ArrayList 譯為以<br />
標記分隔的欄位清單。
using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Security.Permissions;
namespace MyControls
{
// MyDataBound control is a simple read-only grid control.
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class MyDataBound : System.Web.UI.WebControls.DataBoundControl
{
// This is an enumerator for the data source.
IEnumerator dataSourceEnumerator = null;
// Render the data source as a table, without row and column headers.
protected override void RenderContents(
System.Web.UI.HtmlTextWriter writer)
{
// Render the <table> tag.
writer.RenderBeginTag(HtmlTextWriterTag.Table);
// Render the table rows.
while (dataSourceEnumerator.MoveNext())
{
// Get the next data row as an object array.
object[] dataArray =
((DataRowView)dataSourceEnumerator.Current).Row.ItemArray;
// Render the <tr> tag.
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
// Render the fields of the row.
for(int col = 0; col<dataArray.GetLength(0) ; col++)
{
//Render the <td> tag, the field data and the </td> tag.
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(dataArray[col]);
writer.RenderEndTag();
}
// Render the </tr> tag.
writer.RenderEndTag();
}
// Render the </table> tag.
writer.RenderEndTag();
}
// Data binding consists of saving an enumerator for the data.
protected override void PerformDataBinding(IEnumerable data)
{
dataSourceEnumerator = data.GetEnumerator();
}
}
// MyDataBoundAdapter modifies a MyDataBound control to display a
// grid as a list with row separators.
[AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal)]
[AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]
public class MyDataBoundAdapter :
System.Web.UI.WebControls.Adapters.DataBoundControlAdapter
{
// Returns a strongly-typed reference to the MyDataBound control.
public new MyDataBound Control
{
get
{
return (MyDataBound)base.Control;
}
}
// One-dimensional list for the grid data.
ArrayList dataArray = new ArrayList();
// Copy grid data to one-dimensional list, add row separators.
protected override void PerformDataBinding(IEnumerable data)
{
IEnumerator dataSourceEnumerator = data.GetEnumerator();
// Iterate through the table rows.
while (dataSourceEnumerator.MoveNext())
{
// Add the next data row to the ArrayList.
dataArray.AddRange(
((DataRowView)dataSourceEnumerator.Current).Row.ItemArray);
// Add a separator to the ArrayList.
dataArray.Add("----------");
}
}
// Render the data source as a one-dimensional list.
protected override void RenderContents(
System.Web.UI.HtmlTextWriter writer)
{
// Render the data list.
for( int col=0; col<dataArray.Count;col++)
{
writer.Write(dataArray[col]);
writer.WriteBreak();
}
}
}
}
Imports System.Data
Imports System.Web
Imports System.Web.UI
Imports System.Collections
Imports System.Security
Imports System.Security.Permissions
Namespace MyControls
' MyDataBound control is a simple read-only grid control.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class MyDataBound
Inherits System.Web.UI.WebControls.DataBoundControl
' This is an enumerator for the data source.
Private dataSourceEnumerator As IEnumerator = Nothing
' Render the data source as a table, without row and column headers.
Protected Overrides Sub RenderContents( _
ByVal writer As System.Web.UI.HtmlTextWriter)
' Render the <table> tag.
writer.RenderBeginTag(HtmlTextWriterTag.Table)
' Render the table rows.
While dataSourceEnumerator.MoveNext()
' Get the next data row as an object array.
Dim dataArray As Object() = CType( _
dataSourceEnumerator.Current, DataRowView).Row.ItemArray
' Render the <tr> tag.
writer.RenderBeginTag(HtmlTextWriterTag.Tr)
' Render the fields of the row.
Dim col As Integer
For col = 0 To (dataArray.GetLength(0)) - 1
'Render the <td> tag, the field data and the </td> tag.
writer.RenderBeginTag(HtmlTextWriterTag.Td)
writer.Write(dataArray(col))
writer.RenderEndTag()
Next col
' Render the </tr> tag.
writer.RenderEndTag()
End While
' Render the </table> tag.
writer.RenderEndTag()
End Sub
' Data binding consists of saving an enumerator for the data.
Protected Overrides Sub PerformDataBinding(ByVal data As IEnumerable)
dataSourceEnumerator = data.GetEnumerator()
End Sub
End Class
' MyDataBoundAdapter modifies a MyDataBound control to display a
' grid as a list with row separators.
<AspNetHostingPermission(SecurityAction.Demand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
<AspNetHostingPermission(SecurityAction.InheritanceDemand, _
Level:=AspNetHostingPermissionLevel.Minimal)> _
Public Class MyDataBoundAdapter
Inherits System.Web.UI.WebControls.Adapters.DataBoundControlAdapter
' Returns a strongly-typed reference to the MyDataBound control.
Public Shadows ReadOnly Property Control() As MyDataBound
Get
Return CType(MyBase.Control, MyDataBound)
End Get
End Property
' One-dimensional list for the grid data.
Private dataArray As New ArrayList()
' Copy grid data to one-dimensional list, add row separators.
Protected Overrides Sub PerformDataBinding(ByVal data As IEnumerable)
Dim dataSourceEnumerator As IEnumerator = data.GetEnumerator()
' Iterate through the table rows.
While dataSourceEnumerator.MoveNext()
' Add the next data row to the ArrayList.
dataArray.AddRange(CType(dataSourceEnumerator.Current, _
DataRowView).Row.ItemArray)
' Add a separator to the ArrayList.
dataArray.Add("----------")
End While
End Sub
' Render the data source as a one-dimensional list.
Protected Overrides Sub RenderContents( _
ByVal writer As System.Web.UI.HtmlTextWriter)
' Render the data list.
Dim col As Integer
For col = 0 To dataArray.Count - 1
writer.Write(dataArray(col))
writer.WriteBreak()
Next col
End Sub
End Class
End Namespace ' MyControls
第三個程式碼範例會使用組態檔來指定沒有任何控制項配接器用於 MyDataBound
Microsoft Internet Explorer 瀏覽器的控制項,以及 MyDataBoundAdapter
與 Openwave UP 瀏覽器的控制項搭配 MyDataBound
使用。
備註
衍生自 類別的 DataBoundControl 控制項會系結至資料來源,並藉由列舉其系結的資料來源中的專案,來產生其使用者介面或子控制項階層。 DataBoundControl 是抽象基類,可定義可系結至資料來源的所有控制項的通用特性,例如 DataGrid 和 ListBox 控制項。 如需詳細資訊,請參閱DataBoundControl。
DataBoundControlAdapter會修改 特定瀏覽器或瀏覽器類別的行為 DataBoundControl ,或做為某些功能的篩選。 轉譯行為的大部分可調整性可以封裝在衍生自 類別的特殊 HtmlTextWriter 類別中。 因此,單一配接器可能用於許多瀏覽器類別行為,或包含類別中的可調整性 HtmlTextWriter ,可能會不必要地使用控制項配接器。
如果 <controlAdapter>
這些檔案中有專案,則每個控制項都會透過 .browser 定義檔案明確對應介面卡。 因此,對 屬性 DataBoundControl 的任何存取 Adapter 都會 HttpBrowserCapabilities 使用從 .browser 定義檔案擷取的物件來執行查閱,以取得配接器要控制的對應。
在處理期間,.NET Framework會攔截可能為瀏覽器特定控制項方法的呼叫。 如果附加控制項配接器,.NET Framework會呼叫相關聯的配接器方法。 如需詳細資訊,請參閱ControlAdapter。
M:System.Web.UI.WebControls.Adapters.DataBoundControlAdapter.PerformDataBinding (System.Collections.IEnumerable) 方法會將可列舉的集合系結至相關聯的 DataBoundControl 。 屬性 Control 會傳回 的 DataBoundControl 強型別參考。
建構函式
DataBoundControlAdapter() |
初始化 DataBoundControlAdapter 類別的新執行個體。 |
屬性
Browser |
取得對用戶端瀏覽器功能的參考 (此用戶端是發出目前 HTTP 要求的用戶端)。 (繼承來源 ControlAdapter) |
Control |
擷取與這個控制項配置器關聯的 DataBoundControl 物件的強型別參考。 |
IsEnabled |
取得值,表示 Web 控制器及其所有父控制項是否都已啟用。 (繼承來源 WebControlAdapter) |
Page |
取得對頁面的參考 (與此配置器相關聯的控制項便存在於此頁面中)。 (繼承來源 ControlAdapter) |
PageAdapter |
取得對此頁的頁面配置器的參考 (關聯的控制項便存在於此頁面中)。 (繼承來源 ControlAdapter) |