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
- 继承
示例
以下代码示例创建并使用两个派生控件:
MyDataBound
派生自 DataBoundControl的 类是一个简单的只读网格控件。派生自 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是一个抽象基类,用于定义可绑定到数据源的所有控件(如 和 ListBox 控件)DataGrid的共同特征。 有关详细信息,请参阅 DataBoundControl。
修改 DataBoundControlAdapter 特定浏览器或浏览器类的行为 DataBoundControl ,或充当某些功能的筛选器。 呈现行为的很多适应性都可以封装在派生自 类的专用类中 HtmlTextWriter 。 因此,单个适配器可能用于许多浏览器类行为,或者类中包含 HtmlTextWriter 适应性会使使用控件适配器变得不必要。
如果这些文件中有条目,则 <controlAdapter>
每个控件都通过 .browser 定义文件显式映射到适配器。 因此,对 Adapter 属性 DataBoundControl 的任何访问都使用 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) |