Edit

Share via


IDataItemContainer Interface

Definition

Enables data-bound control containers to identify a data item object for simplified data-binding operations.

C#
public interface IDataItemContainer : System.Web.UI.INamingContainer
Derived
Implements

Examples

The following code example demonstrates how to use the IDataItemContainer interface. The SimpleSpreadsheetControl is a control that displays tabular-style data, similar to a DataGrid or GridView control. It contains a set of SimpleSpreadsheetRow objects.

The SimpleSpreadsheetRow class is a container class that implements the IDataItemContainer interface. Although in this example the data item property is intuitively named Data, the DataItem property can be implemented to map to any property or return value of a method.

C#
using System;
using System.Collections;
using System.Data.Common;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.CS
{
    public class SimpleSpreadsheetControl : CompositeDataBoundControl
    {
        protected Table table = new Table();

        public virtual TableRowCollection Rows
        {
            get
            {
                return table.Rows;
            }
        }

        protected override int CreateChildControls(IEnumerable dataSource, bool dataBinding)
        {

            int count = 0;
            // If dataSource is not null, iterate through it and
            // extract each element from it as a row, then
            // create a SimpleSpreadsheetRow and add it to the
            // rows collection.
            if (dataSource != null)
            {

                SimpleSpreadsheetRow row;
                IEnumerator e = dataSource.GetEnumerator();

                while (e.MoveNext())
                {
                    object datarow = e.Current;
                    row = new SimpleSpreadsheetRow(count, datarow);
                    this.Rows.Add(row);
                    ++count;
                }

                Controls.Add(table);
            }
            return count;
        }
    }

    //
    //
    public class SimpleSpreadsheetRow : TableRow, IDataItemContainer
    {
        private object data;
        private int _itemIndex;

        public SimpleSpreadsheetRow(int itemIndex, object o)
        {
            data = o;
            _itemIndex = itemIndex;
        }

        public virtual object Data
        {
            get
            {
                return data;
            }
        }
        object IDataItemContainer.DataItem
        {
            get
            {
                return Data;
            }
        }
        int IDataItemContainer.DataItemIndex
        {
            get
            {
                return _itemIndex;
            }
        }
        int IDataItemContainer.DisplayIndex
        {
            get
            {
                return _itemIndex;
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {

            if (Data != null)
            {
                if (Data is System.Data.Common.DbDataRecord)
                {
                    DbDataRecord temp = (DbDataRecord)Data;
                    for (int i = 0; i < temp.FieldCount; ++i)
                    {
                        writer.Write("<TD>");
                        writer.Write(temp.GetValue(i).ToString());
                        writer.Write("</TD>");
                    }
                }
                else
                {
                    writer.Write("<TD>" + Data.ToString() + "</TD>");
                }
            }

            else
            {
                writer.Write("<TD>This is a test</TD>");
            }
        }
    }
}

The following code example demonstrates how to use the SimpleSpreadsheetControl and AccessDataSource control to display data in an Access database.

ASP.NET (C#)
<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS"
    Assembly="Samples.AspNet.CS"  %>
<%@ Page language="c#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>IDataItemContainer - C# Example</title>
  </head>
  <body>
    <form id="Form1" method="post" runat="server">

      <aspSample:SimpleSpreadsheetControl
          id="SimpleSpreadsheet1"
          runat="server"
          datasourceid="AccessDataSource1" />

      <asp:accessdatasource
          id="AccessDataSource1"
          runat="server"
          datasourcemode="DataReader"
          datafile="Northwind.mdb"
          SelectCommand="SELECT OrderID,CustomerID,OrderDate,RequiredDate,
          ShippedDate FROM Orders WHERE EmployeeID = (SELECT EmployeeID 
          FROM Employees WHERE LastName = 'King')">
      </asp:accessdatasource>

    </form>
  </body>
</html>

Remarks

Container classes that implement the IDataItemContainer interface can specify which of their properties identifies the data item for data-binding operations, such as late binding with the DataBinder class.

Properties

DataItem

When implemented, gets an object that is used in simplified data-binding operations.

DataItemIndex

When implemented, gets the index of the data item bound to a control.

DisplayIndex

When implemented, gets the position of the data item as displayed in a control.

Extension Methods

GetDefaultValues(INamingContainer)

Gets the collection of the default values for the specified data control.

GetMetaTable(INamingContainer)

Gets the table metadata for the specified data control.

SetMetaTable(INamingContainer, MetaTable, IDictionary<String,Object>)

Sets the table metadata and default value mapping for the specified data control.

SetMetaTable(INamingContainer, MetaTable, Object)

Sets the table metadata and default value mapping for the specified data control.

SetMetaTable(INamingContainer, MetaTable)

Sets the table metadata for the specified data control.

TryGetMetaTable(INamingContainer, MetaTable)

Determines whether table metadata is available.

EnableDynamicData(INamingContainer, Type, IDictionary<String,Object>)

Enables Dynamic Data behavior for the specified data control.

EnableDynamicData(INamingContainer, Type, Object)

Enables Dynamic Data behavior for the specified data control.

EnableDynamicData(INamingContainer, Type)

Enables Dynamic Data behavior for the specified data control.

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also