How to use mediaChooser in custom section - answer
I've seen many questions and no (good, or simple) answers to this... so... I used the source.
The reason that the mediaChooser ignores any value you set in your Page_Load or OnLoad method is that the standard parts of the umbraco backend - for some bizarre reason - loads all its data in OnInit - not OnLoad. This is contrary to convention, and very unexpected uinless you know it.
So, at OnInit, the mediaChooser loads its internal copy of its value to the public Value from its IData object. And... on OnLoad it overwrites any value you set Value to (which you would normally do in OnLoad) with the internal copy.
Workaround:
When you would normally set the value of the chooser, don't. Save it somewhere instead. Then, override OnPreRender on your page (it gets fired before the choosers OnPreRender, which is where it finally uses its value) and set the choosers Value property there, Works like a charm. You don't even need to build a class that implements IData (a data extractor). Just pass null for that in the construnctor.
So...
In your OnInit (myChooser is a private mediaChooser on your page):
myChooser = new mediaChooser(null, true, true) // or whatever you need
myPropertyPanel.Controls.Add(myChooser) // and so on
In your OnLoad (myChooserValue is a private string):
myChooserValue = myObject.ImageId.ToString();
In your OnPreRender:
myChooser.Value = myChooserValue; // should be "" if no item is selected
I was deliberately avoiding DAMP although it seems excellent - I wanted a solution that was "package independent"... only umbraco standard code and my code.
How to use mediaChooser in custom section - answer
I've seen many questions and no (good, or simple) answers to this... so... I used the source.
The reason that the mediaChooser ignores any value you set in your Page_Load or OnLoad method is that the standard parts of the umbraco backend - for some bizarre reason - loads all its data in OnInit - not OnLoad. This is contrary to convention, and very unexpected uinless you know it.
So, at OnInit, the mediaChooser loads its internal copy of its value to the public Value from its IData object. And... on OnLoad it overwrites any value you set Value to (which you would normally do in OnLoad) with the internal copy.
Workaround:
When you would normally set the value of the chooser, don't. Save it somewhere instead. Then, override OnPreRender on your page (it gets fired before the choosers OnPreRender, which is where it finally uses its value) and set the choosers Value property there, Works like a charm. You don't even need to build a class that implements IData (a data extractor). Just pass null for that in the construnctor.
So...
In your OnInit (myChooser is a private mediaChooser on your page):
myChooser = new mediaChooser(null, true, true) // or whatever you need
myPropertyPanel.Controls.Add(myChooser) // and so on
In your OnLoad (myChooserValue is a private string):
myChooserValue = myObject.ImageId.ToString();
In your OnPreRender:
myChooser.Value = myChooserValue; // should be "" if no item is selected
In your save routine:
int.tryParse(myChooser.Value, out myValue);
myObject.ImageId = myValue;
Nice and simple!
Hello,
Could it also solve this problem? http://umbraco.codeplex.com/workitem/29009
I don't use the mediaChooser anymore in a custom section. Instead DAMP can also be used in a custom section. See these topics:
http://our.umbraco.org/forum/developers/extending-umbraco/24065-Using-DAMP-in-a-custom-section
http://our.umbraco.org/projects/backoffice-extensions/digibiz-advanced-media-picker/digibiz-advanced-media-picker/23517-DAMP-10-in-custom-page
Jeroen
Ah... forgot one thing: The Value property should be set to "" if there is no valid ID in your data.
And... the setting of Value should only be done if (!PostBack)... of course.
@Jeroen: That is exactly the problem it solves... :)
I was deliberately avoiding DAMP although it seems excellent - I wanted a solution that was "package independent"... only umbraco standard code and my code.
is working on a reply...