【发布时间】:2014-10-02 11:00:07
【问题描述】:
这是它的工作原理:
过滤器 Web 部件将数据行发送到页面上的所有其他 Web 部件。 它的控件在加载时呈现,呈现控件选择将哪一行发送回页面上的其他 webpart。
这会导致在第一个页面加载时出现问题,其他 web 部件将在完成加载之前从提供程序请求该行,因此尚无信息可提供。
唯一的解决方案(这真的很丑陋、缓慢和可怕)是运行所有将在 webpart 在 webpart 的构造函数中使用的控件类中运行的代码,并使用它来预测控件将具有的值第一次运行。这也导致了一大堆我宁愿避免的部署问题。
这是网页部分代码:
public class FilterProjectHeader : WebPart, IWebPartRow
{
// Visual Studio might automatically update this path when you change the Visual Web Part project item.
private const string _ascxPath = @"[link goes here]";
public DataRowView data;
public DataTable table;
private FilterProjectHeaderUserControl control;
public FilterProjectHeader()
{
//Code I want to avoid using:
//var web = SPContext.Current.Web;
//table = web.Lists["foo"].Items.GetDataTable();
//data = foo();
}
protected override void CreateChildControls()
{
control = Page.LoadControl(_ascxPath) as FilterProjectHeaderUserControl;
control.provider = this;
Controls.Add(control);
}
public PropertyDescriptorCollection Schema
{
get
{
return TypeDescriptor.GetProperties(table.DefaultView[0]);
}
}
[ConnectionProvider("Row")]
public IWebPartRow GetConnectionInterface()
{
return this;
}
public void GetRowData(RowCallback callback)
{
callback(data);
}
}
对于控制:
public partial class FilterProjectHeaderUserControl : UserControl
{
public FilterProjectHeader provider { get; set; }
private String _selectedValue;
//Both OnLoad and OnInit have the same result.
protected override void OnInit(EventArgs e)
{
//This is what gets run the first time:
if (!IsPostBack)
{
//Code here finds data then sends it back to webpart like this:
//All of the code in this method definitely does run; I have stepped
//through it and it works but it seems to happen too late to have any
//effect.
provider.data = item;
provider.table = profilesTable;
}
}
protected void filterDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
//Post back method code exempted... it works.
provider.data = item;
provider.table = profilesTable;
}
【问题讨论】: