【问题标题】:How to use data from a IWebPartRow connection when Button is clicked. Provider is always null单击 Button 时如何使用来自 IWebPartRow 连接的数据。提供者始终为空
【发布时间】:2012-01-21 13:21:58
【问题描述】:

我不知道如何解释我的问题 我正在尝试使用与 listview webpart 的 webpart 连接来实现 WebPart。对于连接,我使用 IWebPartRow 接口。 这是我想要实现的目标: 在列表视图 webpart 中,我可以选择一个项目。在我的 WebPart 中,显示了来自连接提供程序的详细信息和一个按钮。单击此按钮时,我想使用来自连接提供程序的数据做一些事情。

这里是webpart代码:

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Text;

namespace ConnectedFilesWebPart.TestWebPart
{
    [ToolboxItemAttribute(false)]
    public class TestWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {

        public DataRowView DataRow
        {
            get; 
            set;
        }

        public IWebPartRow Provider
        {
            get;
            set;
        }
        private readonly StringBuilder _sb = new StringBuilder();

        protected override void CreateChildControls()
        {
            //TestWebPartUserControl control = (TestWebPartUserControl)Page.LoadControl(_ascxPath);
            Button myButton = new Button();
            myButton.Text = "myButton";
            myButton.Click += new EventHandler(myButton_Click);
            Controls.Add(myButton);
        }

        protected void myButton_Click(object sender, EventArgs e)
        {

            if (DataRow != null)
            {
                int lookupId = Int32.Parse(DataRow["ID"].ToString());
                string title = DataRow["Title"].ToString();
            }
        }

        private void GetRowData(object rowData)
        {
            try
            {
                DataRow = (DataRowView)rowData;
            }
            catch
            {
                DataRow = null;
            }
        }

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

        [ConnectionConsumer("Row", AllowsMultipleConnections = true)]
        public void ReceiveProvider(IWebPartRow p)
        {
            Provider = p;
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            if (DataRow != null)
            {
                DataRow providerRow = DataRow.Row;
                foreach (DataColumn column in providerRow.Table.Columns)
                    _sb.AppendFormat("- data column: {0} = {1}\n", column.ColumnName ?? "n/a", providerRow[column]);
                _sb.Append("\n");
                writer.Write(_sb.ToString().Replace("\n", "<br>"));
            }
        }
    }
}

加载 Web 部件后,连接工作正常。但是由于某种原因,当我单击 myButton_Click 中的按钮时,对象 DataRow 始终为空。在我看来,好像再次加载了 webpart,但这次没有调用提供程序。我做错了什么?

【问题讨论】:

    标签: sharepoint null connection web-parts


    【解决方案1】:

    我正在尝试做同样的事情。我在 MSDN 上找到了一个tutorial,看起来它会连接点(对你和我来说!)。希望这会有所帮助。

    编辑:可以找到更好的答案here。它正在做类似于您正在尝试做的事情,并且有效!也有可下载的代码!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多