我正在将功能区与我的 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);
}

最佳答案

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

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

webPartRibbonContextualGroup.Id = "Ribbon.MyContextualTabGroup." + ID;
// ...
webPartRibbonTab.Id = "Ribbon.MyTab." + ID;
// etc.

关于sharepoint - SharePoint 中的多个 Web 部件和功能区,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6379938/

10-13 05:47