Copied to clipboard

Flag this post as spam?

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


  • Niek 5 posts 25 karma points
    Jan 25, 2012 @ 16:37
    Niek
    0

    Getting DataType settings within event handler

    Hey all,

    I've created an custom AbstractDataEditor DataType that looks like this (without all the other code):

    public class ImageResizerDataType : AbstractDataEditor
    {
        [DataEditorSetting("Width", description = "Max image width", type = typeof(TextField))]
        public string Width { get; set; }
    }

    This works fine but now I want to access the setting in an ApplicationBase event handler like this:

    if (property.PropertyType.DataTypeDefinition.DataType.Id == ImageResizerDataType.DataTypeId)
    {
        var dataType = (ImageResizerDataType) property.PropertyType.DataTypeDefinition.DataType;
       
        if (dataType.Width.Contains("."))
        {
            /* Do something */
        }
    }

    This throws a "Object references not set to an instance of an object." error because the dataType.Width is not initialized.

    So how can I access the DataType settings within an event handler?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 25, 2012 @ 16:48
    Jeroen Breuer
    0

    In what event are you calling this code? 

    In this wiki and topic you can see how a datatype is used on a custom page. Maybe you can use that to get your datatype in the event.

    Jeroen

  • Niek 5 posts 25 karma points
    Jan 26, 2012 @ 11:57
    Niek
    0

    I'm calling this code in a Media.AfterSave event but I found a solution (all settings are stored as prevalues):

     

    // Get datatype prevalues
    var prevalues = PreValues.GetPreValues(property.PropertyType.DataTypeDefinition.Id);

    // Get datatype settings
    var width = Convert.ToInt32(((PreValue)prevalues[1]).Value);

    I just hope the prevalues array indexes will be consistent because the AbstractDataEditor saves the DataEditorSetting instances automatically.

    Thanks for your response!

     

  • Jeroen Breuer 4908 posts 12265 karma points MVP 4x admin c-trib
    Jan 26, 2012 @ 12:09
    Jeroen Breuer
    0

    Yes I think the array is consistent. In DAMP we also assume the array index will stay the same.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft