2012-02-28 65 views
5

我有这个问题很多次,无聊,而试图找到很好的解决方案。 不明白为什么微软不包括可以轻松确定显示页面模式的方法:“正常显示”或“设计模式”。 它有很多关于检查不同变量的建议,但它不能唯一地在不同类型的页面(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; 
    } 

是否有人有更好的解决办法?

+1

在发布访问page.Request属性的页面失败并出现异常。 (HttpContext.Current.Request可以用来代替 – 2013-06-19 12:20:43

+0

对于维基页面,只有wiki页面,你的代码给了我解决方案:page.Request.Form [“ctl00 $ PlaceHolderMain $ btnWikiEdit”] ==“edit”。FormMode采用非wiki页面的照顾。 – CigarDoug 2016-05-17 12:14:17

回答

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 
    } 
+0

它不能在WebPartPage – devi 2012-02-29 10:04:56

+0

检查工作这个环节 http://www.codeproject.com/Articles/24019/Switch-a-SharePoint-WebPart-page-display-mode-to -E – 2012-03-01 06:30:46

5

如果你的WebpartPage的工作比下面的代码为我工作

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

    } 
+0

是的,它适用于webpart页面,但不适用于wiki页面 – devi 2012-03-02 07:17:04

+1

本文来自WebPartPage,这是一个很好的开始,这是[SPTechBytes](http://sptechbytes.blogspot.com/2013/ 11/sharepoint-2013-handling-page-display.html)文章显示了3种主要模式之间的区别 – GoldBishop 2014-04-08 16:05:56

+0

page.Request.Form [“ctl00 $ PlaceHolderMain $ btnWikiEdit”] ==“edit”适用于维基页面。上面的代码工作r非维基页面。 – CigarDoug 2016-05-17 12:14:53

12

你有2个的情况下,检测页面模式:

在如果您正在使用团队网站:

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 
    } 
3

我有一个很难得到这些答案在Sharepoint 2013年的工作给予所有的场景。我也无法获得EditModePanel一致的工作。我在this article中发现了一个片段,这似乎适用于迄今为止我尝试过的每种场景。

var isPublishing = SPContext.Current.FormContext.FormMode != SPControlMode.Invalid; 
var wpDMode = WebPartManager.GetCurrentWebPartManager(Page).DisplayMode.Name; 
var isEditing = isPublishing 
    ? SPContext.Current.FormContext.FormMode != SPControlMode.Display 
    : (wpDMode.Equals("Edit") || wpDMode.Equals("Design")); 

然后,你可以简单地检查isEditing您的条件。