确定共享点页面的显示模式

dev*_*evi 5 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;
    }
Run Code Online (Sandbox Code Playgroud)

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

小智 13

你有2个案例来检测页面模式:

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

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

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

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
  }
Run Code Online (Sandbox Code Playgroud)


Jig*_*put 5

如果您在WebpartPage中的工作比下面的代码适合我

 WebPartManager mgr = this.WebPartManager;
 if (mgr.DisplayMode == WebPartManager.EditDisplayMode)
    {
        // logic when in Edit Mode
    }
 else
    {

    }
Run Code Online (Sandbox Code Playgroud)


Ron*_* SP 2

请尝试这个代码..

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
  }
Run Code Online (Sandbox Code Playgroud)