Am I in Edit or View mode

by: Jacob Khan

My first blog post as graduated will be a short one, but I promise more is coming. I recently got a question about how do you know if you are in Edit or Admin mode. Say you have a usercontrol that is used in both an Admin plugin and on a Template page. I now want to do something special if it is in the UI. I had a theory, discussed it with my team and got this solution:

Cast the page as a SystemPageBase and see if it is null. Since all pages in the UI inherit from SystemPageBase if we are in there it wont be null.

 

   1: protected override void OnLoad(EventArgs e)
   2:         {
   3:             SystemPageBase ed = Page as SystemPageBase;
   4:  
   5:             if (ed != null)
   6:             {
   7:                 Status.Text = "UI mode";
   8:             }else
   9:                 Status.Text = "View mode";
  10:  
  11:  
  12:             DataBind();
  13:             base.OnLoad(e);
  14:         }

 

If you have a better solution for it please let me know.

/Jacob

21 August 2009

Tags:


    Comments

    1. Hi Jacob, for page templates we use a control adapter to override the render method of the EPiServer.UI.WebContols.SystemIFrame control. The override appends an additional querystring parameter such as previewmode=true to the iframe url this allows us to query this within the template code. not completly failsafe but seems to work ok. - Nick
    2. Nice one Nick. I've always been looking for a good way to know if a page is being reviewed from inside edit mode, and this seems like a good way to do it. You should share the code in a blog post :-) EPiServer does this itself, as the right click menu is different when inside edit mode. I suspect that to be done in Javascript, but haven't looked into it.
    3. Hi, I don't know why but that trick didn't work for me. The thing I ended doing was to get the id from the querystring, and parsing it to a PageReference to check if it contains a workID. The ID in the QueryString has both the pages ID and the current version beeing edited, for example 1563_1234 where 1563 is the pages ID and 1234 is the current version. If the QueryString contains a work page ID it means that I'm currently looking at a draft, not the published page, hence I´m in Edit mode. When getting the WorkID of a PageData object that's published you will get 0 as a result, and that's what I'm testing against. Here is the code public bool IsPageInEditMode { get{ PageReference pageVersionReference = PageReference.Parse(Request.QueryString["id"]); if (pageVersionReference.WorkID > 0) return true; return false; } }
    4. Hi, all we do is check to see if there is a querysting parameter of 'idkeep=true'. I understand from EPiServer, that this is only present while in edit mode.
    5. Test
    Post a comment    
    User verification Image for user verification  
    Jacob Khan

    About me

    I am a researcher at EPiServer based in Stockholm. I started working in the Research department in May 2008.
    I also blog on world.episerver.com
    See my profile card on world

     

    Syndications


    Archive


    Tag cloud

    EPiTrace logger