【问题标题】:SharePoint Web Part Custom Properties Don't Take Effect Until Page ReloadSharePoint Web 部件自定义属性在页面重新加载之前不会生效
【发布时间】:2011-10-04 20:39:03
【问题描述】:

我正在开发一个使用自定义属性的 sharepoint 2007 Web 部件。这是一个:

[Personalizable(PersonalizationScope.User), WebDisplayName("Policy Update List Name")]
[WebDescription("The name of the SharePoint List that records all the policy updates.\n Default value is Policy Updates Record.")]
public string PolicyUpdateLogName
{
    get { return _PolicyUpdateLogName == null ? "Policy Updates Record" : _PolicyUpdateLogName; }

    set { _PolicyUpdateLogName = value; }
}

这些属性工作正常,只是在您离开页面并返回(或仅单击主页链接)之前,更改不会反映在 Web 部件中。单纯刷新页面是不行的,这让我觉得跟 PostBacks 有关系。

我目前的理论是 ViewState 没有足够早地加载回发数据以使更改生效。至少,ViewState 以某种方式与问题有关。

谢谢, 迈克尔

这里是更相关的代码:

protected override void CreateChildControls()
{
    InitGlobalVariables();

    FetchPolicyUpdateLog_SPList();

    // This function returns true if the settings are formatted correctly
    if (CheckWebPartSettingsIntegrity())  
    {
        InitListBoxControls();

        InitLayoutTable();

        this.Controls.Add(layoutTable);

        LoadPoliciesListBox();
    }

    base.CreateChildControls();
 }

...

protected void InitGlobalVariables()
{
    this.Title = "Employee Activity Tracker for " + PolicyUpdateLogName;

    policyColumnHeader = new Literal();
    confirmedColumnHeader = new Literal();
    pendingColumnHeader = new Literal();

    employeesForPolicy = new List<SPUser>();
    confirmedEmployees = new List<SPUser>();
    pendingEmployees = new List<SPUser>();
}

...

// uses the PolicyUpdateLogName custom property to load that List from Sharepoint
private void FetchPolicyUpdateLog_SPList()
{
    site = new SPSite(siteURL);
    policyUpdateLog_SPList = site.OpenWeb().GetList("/Lists/" + PolicyUpdateLogName);
}

...

protected void InitListBoxControls()
{
    // Init ListBoxes
    policies_ListBox = new ListBox();  // This box stores the policies from the List we loaded from SharePoint
    confirmedEmployees_ListBox = new ListBox();
    pendingEmployees_ListBox = new ListBox();

    // Postback & ViewState
    policies_ListBox.AutoPostBack = true;
    policies_ListBox.SelectedIndexChanged += new EventHandler(OnSelectedPolicyChanged);
    confirmedEmployees_ListBox.EnableViewState = false;
    pendingEmployees_ListBox.EnableViewState = false;
}

...

private void LoadPoliciesListBox()
{
    foreach (SPListItem policyUpdate in policyUpdateLog_SPList.Items)
    {
        // Checking for duplicates before adding.
        bool itemExists = false;

        foreach (ListItem item in policies_ListBox.Items)

            if (item.Text.Equals(policyUpdate.Title))
            {
                itemExists = true;
                break;
            }
        if (!itemExists)
            policies_ListBox.Items.Add(new ListItem(policyUpdate.Title));
    }
}

【问题讨论】:

    标签: asp.net sharepoint-2007 web-parts viewstate custom-properties


    【解决方案1】:

    阅读一下 Sharepoint Web 部件生命周期。在 OnPreRender 事件之前不会更新属性。

    【讨论】:

    猜你喜欢
    • 2014-11-08
    • 2014-11-09
    • 2011-09-16
    • 2011-11-04
    • 2014-09-30
    • 2011-06-13
    • 2021-10-08
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多