Sharepoint 2010 加载前请求信息的SharePoint Web部件

Sharepoint 2010 加载前请求信息的SharePoint Web部件,sharepoint-2010,web-parts,Sharepoint 2010,Web Parts,以下是它的工作原理: 筛选web部件将数据行发送到页面上的所有其他web部件。 它的控件在加载时呈现,呈现控件选择将哪一行发送回页面上的其他Web部件 这会导致在第一个页面加载时出现问题,其他Web部件将在加载完成之前向提供程序请求该行,因此尚未提供任何信息 唯一真正丑陋、缓慢和可怕的解决方案是运行webpart在webpart的构造函数中使用的控件类中运行的所有代码,并使用它来预测控件在第一次运行时的值。这也导致了部署方面的一系列问题,我真的宁愿避免这些问题 以下是Web部件代码: publi

以下是它的工作原理:

筛选web部件将数据行发送到页面上的所有其他web部件。 它的控件在加载时呈现,呈现控件选择将哪一行发送回页面上的其他Web部件

这会导致在第一个页面加载时出现问题,其他Web部件将在加载完成之前向提供程序请求该行,因此尚未提供任何信息

唯一真正丑陋、缓慢和可怕的解决方案是运行webpart在webpart的构造函数中使用的控件类中运行的所有代码,并使用它来预测控件在第一次运行时的值。这也导致了部署方面的一系列问题,我真的宁愿避免这些问题

以下是Web部件代码:

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;
    }

因此,在花了很多时间研究这个问题之后,我发现问题实际上在于微软建议做什么,正如他们所说的,始终使用CreateChildControls将控件加载到页面上

第一次加载页面时,CreateChildControls在加载之后运行,但在重新发布时在加载之前运行。 这就是为什么它适用于repost,但不适用于第一页加载

将CreateChildControls切换到OnInit解决了这个问题,因为OnInit总是在OnLoad之前运行