【问题标题】:SharePoint webparts requesting information prior to loadSharePoint Web 部件在加载之前请求信息
【发布时间】: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;
    }

【问题讨论】:

    标签: sharepoint-2010 web-parts


    【解决方案1】:

    因此,在处理了很多时间之后,我发现问题实际上在于 Microsoft 建议的最佳做法(他们说始终使用 CreateChildControls 将控件加载到页面上)。

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

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

    【讨论】:

      猜你喜欢
      • 2011-07-07
      • 2012-06-14
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-04
      • 1970-01-01
      • 2021-07-18
      相关资源
      最近更新 更多