DataBoundControlAdapter Class

Definition

Customizes the behavior of a DataBoundControl object with which the adapter is associated for specific browser requests.

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
Inheritance
DataBoundControlAdapter

Examples

The following code examples create and use two derived controls:

  • The MyDataBound class, derived from DataBoundControl, is a simple read-only grid control.

  • The MyDataBoundAdapter class, derived from DataBoundControlAdapter, renders the grid data as a one-dimensional list with row separators, suitable for small screen browsers.

The first code example uses a Web page to declare a MyDataBound control and an instance of ObjectDataSource that provides data in the form of a DataView object.

The second code example contains the derived MyDataBound and MyDataBoundAdapter classes:

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

The third code example uses a configuration file to specify that no control adapter is to be used with MyDataBound controls for Microsoft Internet Explorer browsers, and that a MyDataBoundAdapter is used with MyDataBound controls for Openwave UP browsers.

Remarks

A control derived from the DataBoundControl class is bound to a data source and generates its user interface or child control hierarchy by enumerating the items in the data source to which it is bound. DataBoundControl is an abstract base class that defines the common characteristics of all controls that can be bound to a data source, such as the DataGrid and ListBox controls. For more information, see DataBoundControl.

A DataBoundControlAdapter modifies the behavior of a DataBoundControl for a specific browser or class of browsers, or acts as a filter on some capability. Much of the adaptability in rendering behavior can be encapsulated in the specialized classes that derive from the HtmlTextWriter class. 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 can make the use of a control adapter unnecessary.

Each control has explicit mappings to adapters through the .browser definition files, if there are <controlAdapter> entries in these files. Thus, any access to the Adapter property of the DataBoundControl uses the HttpBrowserCapabilities object extracted from the .browser definition files to perform the lookup for the mapping of the adapter to control.

During processing, the .NET Framework intercepts calls to the methods of a control that could be browser specific. If a control adapter is attached, the .NET Framework calls the associated adapter methods. For more information, see ControlAdapter.

The M:System.Web.UI.WebControls.Adapters.DataBoundControlAdapter.PerformDataBinding(System.Collections.IEnumerable) method binds an enumerable collection to the associated DataBoundControl. The Control property returns a strongly typed reference to the DataBoundControl.

Constructors

DataBoundControlAdapter()

Initializes a new instance of the DataBoundControlAdapter class.

Properties

Browser

Gets a reference to the browser capabilities of the client making the current HTTP request.

(Inherited from ControlAdapter)
Control

Retrieves a strongly-typed reference to the DataBoundControl object associated with this control adapter.

IsEnabled

Gets a value indicating whether the Web control and all its parent controls are enabled.

(Inherited from WebControlAdapter)
Page

Gets a reference to the page where the control associated with this adapter resides.

(Inherited from ControlAdapter)
PageAdapter

Gets a reference to the page adapter for the page where the associated control resides.

(Inherited from ControlAdapter)

Methods

BeginRender(HtmlTextWriter)

Called prior to the rendering of a control. In a derived adapter class, generates opening tags that are required by a specific target but not needed by HTML browsers.

(Inherited from ControlAdapter)
CreateChildControls()

Creates the target-specific child controls for a composite control.

(Inherited from ControlAdapter)
EndRender(HtmlTextWriter)

Called after the rendering of a control. In a derived adapter class, generates closing tags that are required by a specific target but not needed by HTML browsers.

(Inherited from ControlAdapter)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
LoadAdapterControlState(Object)

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.

(Inherited from ControlAdapter)
LoadAdapterViewState(Object)

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.

(Inherited from ControlAdapter)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
OnInit(EventArgs)

Overrides the OnInit(EventArgs) method for the associated control.

(Inherited from ControlAdapter)
OnLoad(EventArgs)

Overrides the OnLoad(EventArgs) method for the associated control.

(Inherited from ControlAdapter)
OnPreRender(EventArgs)

Overrides the OnPreRender(EventArgs) method for the associated control.

(Inherited from ControlAdapter)
OnUnload(EventArgs)

Overrides the OnUnload(EventArgs) method for the associated control.

(Inherited from ControlAdapter)
PerformDataBinding(IEnumerable)

Binds the data in the data source of the associated DataBoundControl object to the control adapter.

Render(HtmlTextWriter)

Generates the target-specific markup for the control to which the control adapter is attached.

(Inherited from WebControlAdapter)
RenderBeginTag(HtmlTextWriter)

Creates the beginning tag for the Web control in the markup that is transmitted to the target browser.

(Inherited from WebControlAdapter)
RenderChildren(HtmlTextWriter)

Generates the target-specific markup for the child controls in a composite control to which the control adapter is attached.

(Inherited from ControlAdapter)
RenderContents(HtmlTextWriter)

Generates the target-specific inner markup for the Web control to which the control adapter is attached.

(Inherited from WebControlAdapter)
RenderEndTag(HtmlTextWriter)

Creates the ending tag for the Web control in the markup that is transmitted to the target browser.

(Inherited from WebControlAdapter)
SaveAdapterControlState()

Saves control state information for the control adapter.

(Inherited from ControlAdapter)
SaveAdapterViewState()

Saves view state information for the control adapter.

(Inherited from ControlAdapter)
ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

See also