c# - 确定sharepoint页面的显示方式

标签 c# asp.net sharepoint-2010

我多次遇到这个问题并且在尝试找到好的解决方案时感到无聊。 不明白为什么微软不包括可以轻松确定显示页面模式的方法:“正常显示”或“设计模式”。 它有很多检查不同变量的建议,但它不能唯一地说明页面在不同类型的页面(webpart 页面和 wiki 页面)上的设计以及是否在回发上。

我终于累了,我写了这个:

    public static bool IsDesignTime()
    {
        if (SPContext.Current.IsDesignTime) return true;

        if (HttpContext.Current.Request.QueryString["DisplayMode"] != null)
            return true;

        var page = HttpContext.Current.Handler as Page;

        if(page == null) return false;

        var inDesign = page.Request.Form["MSOLayout_InDesignMode"];
        var dispMode = page.Request.Form["MSOSPWebPartManager_DisplayModeName"];
        var wikiMode = page.Request.Form["_wikiPageMode"];
        var we = page.Request.Form["ctl00$PlaceHolderMain$btnWikiEdit"];

        if (inDesign == null & dispMode == null) return false; //normal display

        if (we == "edit") return true; //design on wiki pages

        if (page is WikiEditPage & page.IsPostBack & inDesign == "" & dispMode == "Browse" & wikiMode == "") return false; //display wiki on postback


        if (inDesign == "" & dispMode == "Browse" & (wikiMode == null | wikiMode == "")) return false; //postback in webpart pages in display mode

        if (inDesign == "0" & dispMode == "Browse") return false; //exiting design on webpart pages

        return true;
    }

有没有人有更好的解决方案?

最佳答案

你有两种情况来检测页面模式:

如果您使用的是团队网站:

    if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Edit)
    {
        ltr.Text = "EditMode2";
    }
    else
    {
        ltr.Text = "ViewMode";
    }

如果您使用的是发布网站:

if (Microsoft.SharePoint.SPContext.Current.FormContext.FormMode == SPControlMode.Display)
  {
   // your code to support display mode
  }
  else // Microsoft.SharePoint.SPContext.Current.FormContext.FormMode = SPControlMode.Edit
  {
   // your code to support edit mode
  }

关于c# - 确定sharepoint页面的显示方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9478913/

相关文章:

c# - 测试 WCF URL 的可用性

c# - 为什么我的流在我自己解锁之前就被解锁了?

c# - 这些针对 Nullable Type 的测试是否等效?

c# - 在SharePoint 2010中自定义功能区控件

c# - 从 C# Windows 窗体中的 DataGridView 获取单个主键值

asp.net - jqGrid、ASP.NET、JSON 让我抓狂。请帮忙

javascript - 将属性添加到 TextBox 后,ListView 不会触发 ItemInserting

c# - ASP.Net Web 部件 : How to hide a webpart?

jquery - 如何在 Sharepoint 2010 中使用 HTTP 处理程序发出 jQuery AJAX 请求?

java - 如何使用java中的rest api从共享点列表中获取文件内容