Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Peter Josefson 15 posts 37 karma points
    Nov 10, 2011 @ 13:20
    Peter Josefson
    2

    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!

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 10, 2011 @ 13:36
  • Peter Josefson 15 posts 37 karma points
    Nov 10, 2011 @ 13:48
    Peter Josefson
    0

    Ah... forgot one thing: The Value property should be set to "" if there is no valid ID in your data.

  • Peter Josefson 15 posts 37 karma points
    Nov 10, 2011 @ 13:51
    Peter Josefson
    0

    And... the setting of Value should only be done if (!PostBack)... of course.

  • Peter Josefson 15 posts 37 karma points
    Nov 10, 2011 @ 13:52
    Peter Josefson
    0

    @Jeroen: That is exactly the problem it solves... :)

  • Peter Josefson 15 posts 37 karma points
    Nov 10, 2011 @ 13:53
    Peter Josefson
    0

    I was deliberately avoiding DAMP although it seems excellent - I wanted a solution that was "package independent"... only umbraco standard code and my code.

Please Sign in or register to post replies

Write your reply to:

Draft