0

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.

3
  • Did you ever get this working? Commented Aug 18, 2014 at 15:39
  • Yes but i will have to review my implementation.
    – GoldBishop
    Commented Sep 25, 2014 at 4:28
  • 1
    @SteveLineberry Posted my eventual answer below.
    – GoldBishop
    Commented Sep 26, 2014 at 15:12

2 Answers 2

1

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);                
    }
}
3
  • that would be great if i was doing a full client side mode change. But in my situation, it is all server side post back behavior. I have tried using SPContext, WebPartManager and a mix between the two and still requires an intial double click, with the second click getting me into the Mode i need. But the first click is almost like a ViewState postback update and then the second click actually changes the ViewState. Is there a similar method like SetPersonalizationDirty to make sure the Mode State has changed?
    – GoldBishop
    Commented Apr 8, 2014 at 23:49
  • Why can't you just output the JS to the page and have your button call your JS. Your page will post back anyway, so if you need to do some serverside stuff as well, you can. Commented Apr 9, 2014 at 4:35
  • was pondering that thought, after my comment. Get back to you on that. Im sure the Ribbon is basically executing JS based on the way it acts visually.
    – GoldBishop
    Commented Apr 9, 2014 at 10:59
0

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();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.