Copied to clipboard

Flag this post as spam?

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


  • Eddie Foreman 215 posts 288 karma points
    Nov 06, 2012 @ 18:46
    Eddie Foreman
    0

    ContentPicker in Usercontrol - Value cannot be null

    Hi All,

    Been trying to create a custom datatype which contains two textbox controls and a content picker.

    My starting poiint was the excellent video 'create a custom datatype using the umbraco usercontrol wrapper.'  This part works fine.  But from here I've added the content picker which is rendering correctly.  However when I try and save the page a 'value cannot be null' error is displayed.

    The code I have is:

     public partial class LinkBox : System.Web.UI.UserControl, umbraco.editorControls.userControlGrapper.IUsercontrolDataEditor
    {
    ContentPicker mp = new ContentPicker();

    protected void Page_Load(object sender, EventArgs e)
    {
    ContentPicker mp = new ContentPicker();
    mp.AppAlias = "media";
    mp.TreeAlias = "media";
    mp.ID = "mediaPicker";

    mediaPickerPlaceHolder.Controls.Add(mp);

    }

    #region IUsercontrolDataEditor Members

    public object value
    {
    get
    {
    XDocument values = new XDocument(
    new XElement("textBoxes",
    new XElement("textBox",
    new XAttribute("id", linkText.ID),
    new XElement("value", new XCData(linkText.Text))
    ),
    new XElement("textBox",
    new XAttribute("id", linkUrl.ID),
    new XElement("value", new XCData(linkUrl.Text))
    )
    ),
    new XElement("mediaNode",
    new XAttribute("id", mp.ID),
    new XElement("value", new XCData(mp.Value))
    )
    );
    return values.ToString();
    }
    set
    {
    if (value != null)
    {
    try
    {
    XDocument savedValues = XDocument.Parse(value.ToString());
    IEnumerable<TextBox> textBoxes = linkPanel.Controls.OfType<TextBox>();
    foreach (TextBox textBox in textBoxes)
    {
    string inputId = textBox.ID;
    var vstr = (from xml in savedValues.Descendants("textBox") where xml.Attribute("id").Value == inputId select xml).Single().Element("value").Value;
    textBox.Text = vstr;
    }
    IEnumerable<ContentPicker> contentPickers = mediaPickerPlaceHolder.Controls.OfType<ContentPicker>();
    foreach (ContentPicker contentPicker in contentPickers)
    {
    string inputId = contentPicker.ID;
    var mvstr = (from xml in savedValues.Descendants("mediaNode") where xml.Attribute("id").Value == inputId select xml).Single().Element("value").Value;
    contentPicker.Value = mvstr;
    }
    }
    catch (System.Xml.XmlException e)
    {
    //No root node element
    }
    }
    }
    }

    #endregion

    Would be grateful if someone could point me in the right direction.

    Thanks,

    Eddie

Please Sign in or register to post replies

Write your reply to:

Draft