I've got an XHTML-string (e.g. MainBody) that is displayed in edit mode by using <EPiServer:Property PropertyName="MainBody" runat="server" ID="mainBody" EditMode="true" />. This will of course then be used to update the MainBody-property on that page. My question is what I use to get the changes from the property that needs to be saved. How do I get a hold of what the WYSIWYG contains? How do I complete the second row of the code below?
PageData updatedPage = oPage.GetPage(new PageReference(CurrentPage.PageLink.ID)).CreateWritableClone();updatedPage.Property["MainBody"].Value = mainBody...DataFactory.Instance.Save(updatedPage, SaveAction.Publish, AccessLevel.NoAccess);
In my experience this can be rather tricky to accomplish. A good start is if you only want to edit pages, not create new ones.
Judging by your property control declaration it looks like you are editing the property on the page itself, are you? In that case you should be able to just call SavePage (inherited from PageBase) to save all property changes.
If you are trying to edit a property from a different page than the one displayed, please reply so and I will try to go through the steps needed (I can't from the top of my head right now).
I am trying to edit the property on the current page. I read something about DOPE mode? Could this be it?
If your goal is to edit the current page, DOPE could be for you. But if you want to use a more customized mode of editing the current page, using property controls is one way to go.
What is the diffrence?
DOPE is a predefined mode (quick-edit in the EPiServer context menu) which edits ALL property controls visible on the page. Sometimes you might only want to edit some properties this way, or you don't use property controls to render the properties, in which case they won't be possible to edit in DOPE mode.
Edit: The individual properties can also render in a different way in DOPE mode than in edit mode
I would like to use the property controls to edit two properties, by adding EditMode="True" to these two properties. What would be the best way to go about and how do I do this?
All you have to do is to declare the properties like you did in the aspx and then call SavePage() in your click event handler.
I can't figure out how to declare to MainBody.
updatedPage.Property["MainBody"].Value = mainBody...
Don't. You dont have to do any of that. SavePage() will automatically scan your page for property edit controls, create a writeable clone of the current page, save the properties and save a new page version with the properties changed.
I can't get it to work. Is this all my "Save"-button should contain?
PageBase pb = (PageBase)this.Page;pb.SavePage();
Does it PostBack before it saves? At first I got an error message because I set CausesValidation="False" but when setting it to True the code runs but the changes are ignored.
Are you changing the control collection depending on wheter it is a postback or not, is that why you ask? For the property controls to retain their values after postback (which is caused by the click event) they have to be loaded with the same ID:s etc. That shouldn't be a problem if you added them to the aspx.
Anyway, any more luck if you try to do something like this?
var clone = CurrentPage.CreateWriteableClone();[idOfPropertyControl].PropertyData = clone.Property["[PropertyName]"];SetValuesForPropertyControls([idOfPropertyControl]);DataFactory.Instance.Save(writeableClone....
I can't get that code to compile... :/
If this is how you ment it then no, same problem:
var clone = CurrentPage.CreateWritableClone();mainBody.PropertyValue.Equals(clone.Property["MainBody"]);SetValuesForPropertyControls(mainBody);DataFactory.Instance.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
Yes, that's how.
My guess is that there is something in the page cycle which prevents the framework from setting the posted values to the controls. It could be that you do some manipulation of where controls are placed, when they are rendered and not or something else which prevents the framework from finding the same controls after postback. Or some Databind statement somwhere which puts the old values back into the property controls before you try to save the page.
I have a button on the page that says "Edit" that performs a PostBack and changes the MainBody-property to an editable one. Is this what causes it?
I just tried removing that function to always displaying it in EditMode and still the same problem. It ignores the changes.
I have no further suggestions then... I would have to try to do it myself and I don't have the time right now, sorry... :(
I understand! I'm going to look into the whole PostBack-thing some more. Thanks!