3

我正在开发一个使用自定义属性的 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));
    }
}
4

1 回答 1

1

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

于 2011-07-21T12:06:24.403 回答