Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

IWebPartTable.GetTableData Method

Returns the data for the table that is being used by the interface as the basis of a connection between two WebPart controls.

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

void GetTableData(
	TableCallback callback
)

Parameters

callback
Type: System.Web.UI.WebControls.WebParts.TableCallback

A TableCallback delegate that contains the address of a method that receives the data.

Use this method to retrieve the data table values passed by the interface. The provider implements this method, and the consumer invokes it at the point when the consumer is ready to process or render data.

The following code example shows the implementation and use of the GetTableData method. The complete example is shown in the class overview for the IWebPartTable interface. The provider control implements the GetTableData method, returning the data in the specified table. The consumer control calls the GetTableData method in its OnPreRender method, so that the value of the data can be written 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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

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

.NET Framework

Supported in: 3.5, 3.0, 2.0

Community Additions

ADD
Show:
© 2015 Microsoft