WSS3 中数据提供者与数据消费者范例 (算是为和我一样苦苦学习WSS的同志们贡献一点力量,我可是找了很久才知道的)

//数据消费者
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 Test
{
    public sealed class RowConsumerWebPart : WebPart
    {
        #region 数据消费者
        private IWebPartRow _provider;
        private DataRowView  _tableData;
        private void GetRowData(object rowData)
        {
            _tableData = (DataRowView)rowData;
        }

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

        [ConnectionConsumer("Row")]
        public void SetConnectionInterface(IWebPartRow provider)
        {
            _provider = provider;
        }
        #endregion

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

//数据提供者
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 ToneluckWss
{
    //[AspNetHostingPermission(SecurityAction.Demand,
    //  Level = AspNetHostingPermissionLevel.Minimal)]
    //[AspNetHostingPermission(SecurityAction.InheritanceDemand,
    //  Level = AspNetHostingPermissionLevel.Minimal)]
    public sealed class RowProviderWebPart : WebPart, IWebPartRow
    {
        private DataTable _table;
        private DataView _view;
        public RowProviderWebPart()
        {
            _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);

            _view = new DataView(_table);
        }

        [ConnectionProvider("Row")]
        public IWebPartRow GetConnectionInterface()
        {
            return new RowProviderWebPart();
        }

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

        public void GetRowData(RowCallback callback)
        {
            callback(_view[0]);
        }

    }
}

posted @ 2006-09-12 11:15  随风而去  阅读(80)  评论(0)    收藏  举报