Copied to clipboard

Flag this post as spam?

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


  • George 28 posts 71 karma points
    Mar 23, 2012 @ 01:33
    George
    0

    Custom field (IUsercontrolDataEditor) blanks out property on save

    I've written a custom field following the tutorial at http://www.nibble.be/?p=24. The property the field is rendering is an ID for a node (e.g. 1234), and the field renders the name of that node in a span - the field isn't user editable.

    Unfortunately, every time I save and publish a document with the custom field, the property backing that field is blanked out - e.g. "1234" becomes "".

    public partial class ReadonlyNodeReference : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (umbracoValue != null)
                {
                    string valStr = umbracoValue.ToString();
                    if (!String.IsNullOrWhiteSpace(valStr))
                    {
                        int val;
                        if (int.TryParse(valStr, out val))
                        {
                            nodeName.InnerText = getNodeNameFromId(val);
                        }
                        else
                        {
                            nodeName.InnerText = String.Format("Invalid node ID: '{0}'", valStr);
                        }
                    }
                }
            }
            private object umbracoValue;
            public object value
            {
                get
                {
                    return umbracoValue;
                }
                set
                {
                    umbracoValue = value;
                }
            }
            private string getNodeNameFromId(int nodeId)
            {
                if (nodeId > 0)
                {
                    var n = new Node((int)nodeId);
                    return n.Name;
                }
                else
                {
                    return String.Format("Invalid node ID: '{0}'", nodeId);
                }
            }
    }

    Does anyone know what I'm doing wrong?

  • George 28 posts 71 karma points
    Mar 27, 2012 @ 01:53
    George
    0

    Apologies for the bump - I've been looking at the database, and pre-save the value is set (3626), post-save the value is null (NULL).

    SELECT *
    FROM cmsPropertyData
    WHERE propertytypeid = 71
      AND contentNodeId = 7762
    ORDER BY id;

    Pre-save:

    id       contentNodeId  versionId                             propertytypeid  dataInt  dataDate  dataNvarchar  dataNtext
    1062396 7762    06490D36-FE58-4F67-8C22-C305864D034C  71    3626    NULL    NULL    NULL

    Post-save:

    id       contentNodeId  versionId                             propertytypeid  dataInt  dataDate  dataNvarchar  dataNtext
    1062396 7762    06490D36-FE58-4F67-8C22-C305864D034C  71    NULL    NULL    NULL    NULL
  • George 28 posts 71 karma points
    Mar 27, 2012 @ 04:22
    George
    0

    OK, fixed it - the problem was that the umbracoValue is cleared in between load and postback. The fix is to save the id in a hidden and then restore it on postback:

    if (!IsPostBack)
    {
    nodeId.Value = _idstr;
    }
    else
    {
    _idstr = nodeId.Value;
    }
  • George 28 posts 71 karma points
    Mar 27, 2012 @ 04:29
    George
    0
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies