Changing Mode from BrowseDisplayMode to Edit Mode
-
10-12-2019 - |
Question
I have a custom webpart, i am trying to get to do a one-click transition from WebPartManager.BrowseDisplayMode
to SPControlMode.Edit
. Unfortunately, there is no direct means of transitioning between these two very different modes. But i have built, maybe a successful process but need some help, as the behavior is kind of silly.
So i have a Link Button, with the text "Edit", in the event that the Properties are not set. This situation would be the case where someone comes in and drops the controls on a page and then later on fills in the properties for execution.
Link Button Click event:
void lbtnViewEdit_Click ( object sender , EventArgs e ) {
if ( WebPartManager.DisplayMode == WebPartManager.BrowseDisplayMode) {
WebPartManager.DisplayMode = WebPartManager.DesignDisplayMode;
//SPContext.Current.FormContext.SetFormMode( SPControlMode.Edit , false );
}
}
And then i started playing with the FormContext DisplayMode settings. Currently it is in the CreateChildControls()
event, as i was playing with the event models.
protected override void CreateChildControls () {
if ( WebPartManager.DisplayMode == WebPartManager.DesignDisplayMode ) {
SPContext.Current.FormContext.SetFormMode( SPControlMode.Edit , false );
}
base.CreateChildControls();
}
The behavior i am currently experiencing, maybe a postback behavior, but i am unsure.
From the BrowseDisplayMode
(Published Page), i see the Edit LinkButton. But when i click the Edit button, i have to click it twice to get to the SPControlMode.Edit
Mode. The second click of the LinkButton gets me to the display i want to be in, but i would like for it to be a single click transition from a Published Page view to a Edit display, similar to clicking the Edit
from the Ribbon.
Any help on which process i should use, would be greatly appreciated.
Solution 2
Here was my final implementation without using the JavaScript
model to perform the evaluation
if ( WebPartManager.DisplayMode == WebPartManager.BrowseDisplayMode ) {
} else if (WebPartManager.DisplayMode == WebPartManager.DesignDisplayMode) {
}
I do this in my PageLoad, but theoretically you could put this control block anywhere where you need to perform conditional logic based on the current mode the Page/Webpart is in.
Then i overrode some Events:
protected override void CreateChildControls () {
if ( WebPartManager.DisplayMode == WebPartManager.DesignDisplayMode ) {
SPContext.Current.FormContext.SetFormMode( SPControlMode.Edit , false );
}
base.CreateChildControls();
}
protected override void OnPreRender ( EventArgs e ) {
if ( ProviderPart != null ) {
ProviderPart.GetParametersData( new ParametersCallback( SetProviderData ) );
}
base.OnPreRender( e );
}
protected override void OnInit ( EventArgs e ) {
base.OnInit( e );
InitializeControl();
}
OTHER TIPS
There is some extra stuff you need to do if you are on a publishing page. This is the JavaScript code I've had success with in the past doing what you are asking:
function ChangeToEditMode(){
if (window.location.search.match("[?&]PageView=Shared")){
var inDesignMode = document.forms[MSOWebPartPageFormName].MSOLayout_InDesignMode.value;
if (inDesignMode == "1")
{
// page is in edit mode
}
}
else{
if (document.forms["aspnetForm"]["MSOLayout_InDesignMode"] != null)
document.forms["aspnetForm"]["MSOLayout_InDesignMode"].value = 1;
if (document.forms["aspnetForm"]["MSOAuthoringConsole_FormContext"] != null)
document.forms["aspnetForm"]["MSOAuthoringConsole_FormContext"].value = 1;
if (document.forms["aspnetForm"]["MSOSPWebPartManager_DisplayModeName"] != null)
document.forms["aspnetForm"]["MSOSPWebPartManager_DisplayModeName"].value = "Design";
ChangeLayoutMode(false);
}
}