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