我不确定如何解释我的问题 我正在尝试使用webpart连接到listview webparts实现WebPart。对于连接,我使用IWebPartRow接口。 这是我想要实现的目标: 在列表视图webpart中,我可以选择一个Item。在我的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>"));
}
}
}
}
加载webpart后,连接正常。但出于某些原因,当我单击myButton_Click中的按钮时,对象DataRow始终为null。在我看来,好像再次加载了webpart,但这次没有调用提供程序。我做错了什么?