【问题标题】:Display/hide control in Webpart based on edit/browse mode基于编辑/浏览模式在 Webpart 中显示/隐藏控件
【发布时间】:2015-12-08 00:35:36
【问题描述】:

我有一个 Visual Studio 项目,我在其中创建了一个 Visual Webpart。在用户控件中,我有一个面板,我想在编辑模式下显示并在浏览模式下隐藏。

ASP.NET 代码 sn-p:

<asp:panel runat="server" ID="myControl">

用户控制代码后面的C#代码sn-p:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    WebPartManager wpm = WebPartManager.GetCurrentWebPartManager(this.Page);
    if (wpm.DisplayMode == WebPartManager.BrowseDisplayMode)
    {
        myControl.Attributes.Add("style", "display: none");
    }
    else if (wpm.DisplayMode == WebPartManager.EditDisplayMode)
    {
        myControl.Attributes.Add("style", "display: block");
    }
}

这可行,但如果我在 same 页面上有两个 same webpart 并将一个 webpart 设置为编辑模式,它会在两个 webpart 中显示面板。似乎OnPreRender 事件代码为页面上的两个 Web 部件运行。

我什至尝试将代码写成this.myControl.Attributes.Add("style", "display: block");,但还是不行。

我希望 OnPreRender 代码仅在其 webpart 上运行,而不是修改页面上的其他 webaprt。我该如何解决这个问题?有没有更好(或首选)的方法?

注意:我需要使用display: none,因为该面板将通过 JavaScript 访问。

PS:这是来自here 的交叉帖子,因为我没有得到任何满意的答案

【问题讨论】:

    标签: c# asp.net sharepoint sharepoint-2010 web-parts


    【解决方案1】:

    我在页面加载时做同样的事情,它对我来说很好..(页面大约有 10 个 WP,只有 1 个在编辑模式下表现不同。)

    也许您的意思是页面上有 2 个相同的 Web 部件?然后我猜这两个 webpart 的行为会一样,因为 WebPartManager.DisplayMode 返回页面的模式,而不是 webpart。 (见msdn)。

    【讨论】:

    • 也许是 wpm.SelectedWebPart 并以某种方式提供帮助。
    • 是的。我的意思是相同的 Web 部件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-03
    • 2013-08-07
    • 1970-01-01
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多