3

我正在将功能区与我的 webpart 相关联。我需要在一个页面中添加两个以上的 webpart。

我不想为每个 webpart 设置一个单独的上下文组/选项卡。有没有办法检查页面上当前功能区中是否存在特定组/选项卡?

此时,当我向页面添加多个 webpart 时,我收到以下错误:

Item has already been added. Key in dictionary: 'Ribbon.MyContextualTabGroup' Key being added: 'Ribbon.MyContextualTabGroup'

这是我的代码供您参考:

/// <summary>
/// Gets the web part contextual info.
/// </summary>
public WebPartContextualInfo WebPartContextualInfo
{
    get
    {
        var webPartContextualInfo = new WebPartContextualInfo();
        var webPartRibbonContextualGroup = new WebPartRibbonContextualGroup();
        var webPartRibbonTab = new WebPartRibbonTab();

        webPartRibbonContextualGroup.Id = "Ribbon.MyContextualTabGroup";
        webPartRibbonContextualGroup.Command = "MyContextualTab.EnableContextualGroup";
        webPartRibbonContextualGroup.VisibilityContext = "MyContextualTab.CustomVisibilityContext";

        webPartRibbonTab.Id = "Ribbon.MyTab";
        webPartRibbonTab.VisibilityContext = "MyContextualTab.CustomVisibilityContext";

        webPartContextualInfo.ContextualGroups.Add(webPartRibbonContextualGroup);
        webPartContextualInfo.Tabs.Add(webPartRibbonTab);
        webPartContextualInfo.PageComponentId = SPRibbon.GetWebPartPageComponentId(this);

        return webPartContextualInfo;
    }
}

/// <summary>
/// Adds the contextual tab.
/// </summary>
private void AddContextualTab()
{
    SPRibbon spRibbon = SPRibbon.GetCurrent(Page);

    if (spRibbon == null) return;

    var ribbonExtensions = new XmlDocument();

    ribbonExtensions.LoadXml(_contextualTab);
    spRibbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.ContextualTabs._children");

    ribbonExtensions.LoadXml(_contextualTabTemplate);
    spRibbon.RegisterDataExtension(ribbonExtensions.FirstChild, "Ribbon.Templates._children");
}

/// <summary>
/// The event handler for the System.Web.UI.Control.PreRender event that occurs immediately before the Web Part is rendered to the Web Part Page it is contained on.
/// </summary>
/// <param name="e">A System.EventArgs that contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    AddContextualTab();

    ClientScriptManager clientScriptManager = Page.ClientScript;
    clientScriptManager.RegisterClientScriptBlock(GetType(), "MyWebPart", DelayScript);
}
4

1 回答 1

2

不能在 Web 部件的不同实例之间共享上下文功能区。因为仅当您的 Web 部件实例在页面上具有“焦点”时才会显示功能区。因此,Web 部件的多个实例必须创建自己的上下文组。

为避免功能区 ID 重复,将特定于 Web 部件实例的部件附加到功能区 ID。您可以使用 Web 部件的ID

webPartRibbonContextualGroup.Id = "Ribbon.MyContextualTabGroup." + ID;
// ...
webPartRibbonTab.Id = "Ribbon.MyTab." + ID;
// etc.
于 2011-06-17T07:58:12.560 回答