Export (0) Print
Expand All

IWebPartTable.Schema Property

Gets the schema information for a data table that is used to share data between two WebPart controls.

Namespace:  System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

PropertyDescriptorCollection Schema { get; }

This property provides property information on the data in an IWebPartTable instance. There is an implicit contract between the schema referenced by the Schema property and the data in the table. The contract is that for each PropertyDescriptor object in the Schema property, you can call GetValue on an element of the table data to get the value in a column or a row of the table.

The following code example shows the implementation and use of the Schema property. The complete example is shown in the class overview for the IWebPartTable interface. The provider control implements the Schema property. Then the consumer control, in its RenderContents method, queries the Schema property to retrieve information about the table as it writes the table data to the page.


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Reflection;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Samples.AspNet.CS.Controls
{
  // This sample code creates a Web Parts control that acts as a provider 
  // of table data.
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
	public sealed class TableProviderWebPart : WebPart, IWebPartTable
	{
		DataTable _table;

		public TableProviderWebPart()
		{
			_table = new DataTable();

			DataColumn col = new DataColumn();
			col.DataType = typeof(string);
			col.ColumnName = "Name";
			_table.Columns.Add(col);

			col = new DataColumn();
			col.DataType = typeof(string);
			col.ColumnName = "Address";
			_table.Columns.Add(col);

			col = new DataColumn();
			col.DataType = typeof(int);
			col.ColumnName = "ZIP Code";
			_table.Columns.Add(col);

			DataRow row = _table.NewRow();
			row["Name"] = "John Q. Public";
			row["Address"] = "123 Main Street";
			row["ZIP Code"] = 98000;
			_table.Rows.Add(row);
		}

		public PropertyDescriptorCollection Schema
		{
			get
			{
				return TypeDescriptor.GetProperties(_table.DefaultView[0]);
			}
		}

		public void GetTableData(TableCallback callback)
		{
				callback(_table.Rows);
		}

		public bool ConnectionPointEnabled
		{
			get
			{
				object o = ViewState["ConnectionPointEnabled"];
				return (o != null) ? (bool)o : true;
			}
			set
			{
				ViewState["ConnectionPointEnabled"] = value;
			}
		}

		[ConnectionProvider("Table", typeof(TableProviderConnectionPoint), 
      AllowsMultipleConnections = true)]
		public IWebPartTable GetConnectionInterface()
		{
			return new TableProviderWebPart();
		}

		public class TableProviderConnectionPoint : ProviderConnectionPoint
		{
			public TableProviderConnectionPoint(MethodInfo callbackMethod, 
        Type interfaceType, Type controlType, string name, string id, 
        bool allowsMultipleConnections) 
        : base(callbackMethod, interfaceType, controlType, name, id, 
          allowsMultipleConnections)
			{
			}

			public override bool GetEnabled(Control control)
			{
				return ((TableProviderWebPart)control).ConnectionPointEnabled;
			}

		}

	}


  // This code sample creates a Web Parts control that acts as a consumer 
  // of information provided by the TableProvider.ascx control.
  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class TableConsumer : WebPart
  {
    private IWebPartTable _provider;
    private ICollection _tableData;

    private void GetTableData(object tableData)
    {
      _tableData = (ICollection)tableData;
    }

    protected override void OnPreRender(EventArgs e)
    {
      if (_provider != null)
      {
        _provider.GetTableData(new TableCallback(GetTableData));
      }
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
      if (_provider != null)
      {
        PropertyDescriptorCollection props = _provider.Schema;
        int count = 0;
        if (props != null && props.Count > 0 && _tableData != null)
        {
          foreach (PropertyDescriptor prop in props)
          {
            foreach (DataRow o in _tableData)
            {
              writer.Write(prop.DisplayName + ": " + o[count]);
            }
            writer.WriteBreak();
            writer.WriteLine();
            count = count + 1;
          }
        }
        else
        {
          writer.Write("No data");
        }
      }
      else
      {
        writer.Write("Not connected");
      }
    }

    [ConnectionConsumer("Table")]
    public void SetConnectionInterface(IWebPartTable provider)
    {
      _provider = provider;
    }

    public class TableConsumerConnectionPoint : ConsumerConnectionPoint
    {
      public TableConsumerConnectionPoint(MethodInfo callbackMethod,
        Type interfaceType, Type controlType, string name, string id,
        bool allowsMultipleConnections)
        : base(callbackMethod, interfaceType, controlType, name, id,
        allowsMultipleConnections)
      {
      }

    } // TableConsumerConnectionPoint

  } // TableConsumer

} // Samples.AspNet.CS.Controls


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft