Sharepoint 单击按钮时如何使用来自IWebPartRow连接的数据。提供程序始终为空
我不知道如何解释我的问题 我正在尝试使用与listview Web部件的Web部件连接来实现Web部件。对于连接,我使用IWebPartRow接口。 以下是我想要实现的目标: 在列表视图Web部件中,我可以选择一个项目。在我的Web部件中,将显示连接提供程序的详细信息和一个按钮。单击此按钮时,我想使用来自连接提供程序的数据执行某些操作 以下是Web部件代码:Sharepoint 单击按钮时如何使用来自IWebPartRow连接的数据。提供程序始终为空,sharepoint,null,connection,web-parts,Sharepoint,Null,Connection,Web Parts,我不知道如何解释我的问题 我正在尝试使用与listview Web部件的Web部件连接来实现Web部件。对于连接,我使用IWebPartRow接口。 以下是我想要实现的目标: 在列表视图Web部件中,我可以选择一个项目。在我的Web部件中,将显示连接提供程序的详细信息和一个按钮。单击此按钮时,我想使用来自连接提供程序的数据执行某些操作 以下是Web部件代码: using System; using System.ComponentModel; using System.Web; using Sy
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>"));
}
}
}
}
使用系统;
使用系统组件模型;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用System.Web.UI.WebControl.WebParts;
使用Microsoft.SharePoint;
使用Microsoft.SharePoint.WebControl;
使用系统数据;
使用系统文本;
命名空间ConnectedFilesWebPart.TestWebPart
{
[ToolboxItemAttribute(false)]
公共类TestWebPart:Microsoft.SharePoint.WebPartPages.WebPart
{
公共数据行查看数据行
{
得到;
设置
}
公共IWebPartRow提供程序
{
得到;
设置
}
私有只读StringBuilder_sb=新StringBuilder();
受保护的覆盖无效CreateChildControls()
{
//TestWebPartUserControl=(TestWebPartUserControl)Page.LoadControl(_);
按钮myButton=新按钮();
myButton.Text=“myButton”;
myButton.Click+=新建事件处理程序(myButton\u Click);
控件。添加(myButton);
}
受保护的无效myButton\u单击(对象发送者,事件参数e)
{
if(DataRow!=null)
{
int lookupId=Int32.Parse(DataRow[“ID”].ToString());
字符串title=DataRow[“title”].ToString();
}
}
私有void GetRowData(对象rowData)
{
尝试
{
DataRow=(DataRowView)rowData;
}
抓住
{
DataRow=null;
}
}
受保护的覆盖无效OnPreRender(EventArgs e)
{
if(提供程序!=null)
GetRowData(newRowCallback(GetRowData));
}
[ConnectionConsumer(“行”,AllowsMultipleConnections=true)]
公共无效接收提供程序(IWebPartRow p)
{
提供者=p;
}
受保护的覆盖无效呈现内容(HtmlTextWriter)
{
基本渲染内容(编写器);
if(DataRow!=null)
{
DataRow providerRow=DataRow.Row;
foreach(providerRow.Table.Columns中的DataColumn列)
_sb.AppendFormat(“-data列:{0}={1}\n”,column.ColumnName??“n/a”,providerRow[column]);
_某人附加(“\n”);
writer.Write(_sb.ToString().Replace(“\n”,“
”);
}
}
}
}
加载Web部件后,连接工作正常。但由于某种原因,当我单击myButton_中的按钮时,对象数据行始终为空。在我看来,似乎再次加载了Web部件,但这次没有调用提供程序。我做错了什么?我正试图做同样的事情。我在MSDN上找到了一个连接点(对你和我来说!)。希望这有帮助 编辑:可以找到更好的答案。它正在做一些类似于你试图做的事情,而且它是有效的!还有可下载的代码