开发者

SharePoint Web Part Custom Properties Don't Take Effect Until Page Reload

I am developing a sharepoint 2007 web part that uses custom properties. Here is one:

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

The properties work fine except that the changes are not reflected in the web part until you leave the page and navigate back (or just click on the home page link). Simply refreshing the page doesn't work, which makes me think it has something to do with PostBacks.

My current theory is that the ViewState is not loading postback data early enough for the changes to take effect. At the very least, the ViewState is involved somehow with the issue.

Thanks, Michael

Here is more relevant code:

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
    po开发者_运维技巧licies_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));
    }
}


Do some reading up on the Sharepoint web part life cycle. Properties are not updated until the OnPreRender event.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜