3

カスタム プロパティを使用する SharePoint 2007 Web パーツを開発しています。これが1つです:

[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 に答える