Try our conversational search powered by Generative AI!

Unable to serialize a LinkItemCollection in custom property

Vote:
 

Hello,

 

I'm trying to create a custom property that contains a collection of controls. One of the m is a LinkItemCollection. I originally started with Intergen's MultipageLinkItemCollection but it seemed much more complicated. Anyhow, I got most of it working, the only part missing is being able to save the values selected with the control in ApplyEditChanges(). The value is simply ignored although the LinkItemCollection is serializable. 

 Any clues anyone??

 Thanks.

Victor

 

Code:

[Serializable]
public class CustomPageListDetails
{
public string ListFormat;
public PageReference Link;
public LinkItemCollection LinkCollection;



#region Helper Methods

///
/// Gets the promo from string.
///
///

The XML.
///
public static CustomPageListDetails GetCustomPageListDetailsFromString(string xml)
{
return SerializeUtils.DeserializeObject(xml);
}
///
/// Gets the promo from the CurrentPage.
///
/// Name of the property.
///
public static CustomPageListDetails GetCustomPageListDetailsFromPage(string PropertyName)
{
return GetCustomPageListDetailsFromPage(PropertyName, PageDateHelper.CurrentPage);
}
///
/// Gets the promo from page.
///
/// Name of the property.
/// The page.
///
public static CustomPageListDetails GetCustomPageListDetailsFromPage(string PropertyName, PageData page)
{
//Add Some caching functionality by placing in the
if ((page[PropertyName] != null) && !string.IsNullOrEmpty(page[PropertyName].ToString()))
{
string XMLstr = string.Empty;
var propname = page.PageLink.ID.ToString() + ":" + PropertyName;
if (System.Web.HttpContext.Current.Items[PropertyName] != null)
{
XMLstr = (string)System.Web.HttpContext.Current.Items[propname];
}
else
{
XMLstr = (string)page[PropertyName];
System.Web.HttpContext.Current.Items[propname] = XMLstr;
}
return GetCustomPageListDetailsFromString(XMLstr);

 

}
return new CustomPageListDetails();
}
#endregion
}

 

///
/// Custom PropertyData implementation
///
[Serializable]
[PageDefinitionTypePlugIn(DisplayName = "Custom Page List", Description = "Allows editing of a complex element.")]
public class CustomPageList : EPiServer.Core.PropertyData
{
public override EPiServer.Core.PropertyData ParseToObject(string str)
{
// TODO: Write code that converts the string passed to the method into an instance of this type.
CustomPageList customPageList = new CustomPageList();
customPageList.ParseToSelf(str);
return customPageList;
}

public override void ParseToSelf(string str)
{
// TODO: Write code that converts the string passed to the method into property data and populate the current instance with this data.
this.String = str;
}

public override Type PropertyValueType
{
get
{
// TODO: Write code that returns the underlying type
return this.GetType();
}
}

protected override void SetDefaultValue()
{
// TODO: Write code that resets the value of this property to its default.
this.String = "";
}

public override EPiServer.Core.PropertyDataType Type
{
get
{
// TODO: Write code that returns the PropertyDataType this property is based on.
return PropertyDataType.LongString;
}
}

string _string;
///
/// Gets or sets the string value of this property.
///
[System.Xml.Serialization.XmlIgnore]
protected virtual string String
{
get
{
return this._string;
}
set
{
base.ThrowIfReadOnly();
if (PropertyData.QualifyAsNullString(value))
{
base.Clear();
}
else if ((this._string != value) || this.IsNull)
{
this._string = value;
base.Modified();
}
}
}

public override object Value
{
get
{
if (this.IsNull)
{
return null;
}
return this.String;
}
set
{
base.ThrowIfReadOnly();
base.SetPropertyValue(value,
delegate
{
this.String = value.ToString();
});
}
}

public override EPiServer.Core.IPropertyControl CreatePropertyControl()
{
return new CustomPageListControl();
}

}public class CustomPageListControl : EPiServer.Web.PropertyControls.PropertyStringControl
{
#region Controls
protected System.Web.UI.WebControls.DropDownList listFormat;
protected EPiServer.Web.WebControls.InputPageReference link;
protected Control multiPageCollection;
//protected var multiPageCollection;// as IPropertyLinkCollectionEditControl = new PropertyLinkCollectionEditControl();
//protected EPiServer.Web.PropertyControls.p PropertyLinkCollectionEditControl multiPageCollection;
protected HtmlGenericControl divListControl = new HtmlGenericControl("div");
protected HtmlGenericControl divListFormat = new HtmlGenericControl("div");
protected HtmlGenericControl divLink = new HtmlGenericControl("div");
protected HtmlGenericControl divMulti = new HtmlGenericControl("div");
protected HtmlGenericControl brControl = new HtmlGenericControl("br");
#endregion

///
/// Gets a value indicating whether [supports on page edit].
///
/// true if [supports on page edit]; otherwise, false.
public override bool SupportsOnPageEdit
{
get
{
return false;
}
}


///
/// Inherited. Initialize the value of the TextBox control.
///
protected override void SetupEditControls()
{
var customPageList = SerializeUtils.DeserializeObject(this.ToString());

if (this.listFormat != null && customPageList.ListFormat != null)
{
this.listFormat.SelectedValue = customPageList.ListFormat.ToString();
}
if (this.link.PageLink != null && customPageList.Link != null)
{
this.link.PageLink = customPageList.Link;
}

if (this.multiPageCollection != null && customPageList.LinkCollection != null)
{
((IPropertyLinkCollectionEditControl)multiPageCollection).OriginalLinkCollection = customPageList.LinkCollection;
//this.multiPageCollection = customPageList.PropertyLinkCollection.Links;
}
}

///
/// Inherited. Applies changes for the posted data to the page's properties when the RenderType property
/// is set to Edit.
///
public override void ApplyEditChanges()
{
CustomPageListDetails customPageList = new CustomPageListDetails();

customPageList.ListFormat = (this.listFormat.SelectedValue != null) ? this.listFormat.SelectedValue : null;
customPageList.Link = (this.link.PageLink != null) ? this.link.PageLink : null;
//try
//{
//if (customPageList.LinkCollection == null)
//{
// customPageList.LinkCollection = new EPiServer.SpecializedProperties.PropertyLinkCollection();
//}

//customPageList.PropertyLinkCollection.Links = ((IPropertyLinkCollectionEditControl)this.multiPageCollection).NewLinkCollection;
//}
//catch (Exception exception)
//{
// this.AddErrorValidator(exception.Message);
//}


customPageList.LinkCollection = (this.multiPageCollection != null || ((IPropertyLinkCollectionEditControl)this.multiPageCollection).NewLinkCollection != null) ? SerializeUtils.DeserializeObject(((IPropertyLinkCollectionEditControl)this.multiPageCollection).NewLinkCollection.ToString()) : null;
//customPageList.MultiPageCollection = (this.multiPageCollection.PropertyMultiPage.SelectedLinkItems != null) ? this.multiPageCollection.PropertyMultiPage.SelectedLinkItems : null;

string obj = SerializeUtils.SerializeObject(customPageList);
base.SetValue(obj);
}


///
/// Creates the edit controls.
///
public override void CreateEditControls()
{
var customPageList = SerializeUtils.DeserializeObject(this.ToString());

// create the table
Panel panel = new Panel();
panel.BorderStyle = BorderStyle.Solid;
panel.BorderWidth = 1;
this.Controls.Add(panel);
divListControl.Attributes.Add("style","padding:5px");
divListControl.Attributes.Add("id", "divListFormat");
panel.Controls.Add(divListControl);
divListControl.Controls.Add(divListFormat);

// create a cells
listFormat = new DropDownList() { ID = "listFormat"};
listFormat.Attributes.Add("onchange", "javascript:OnChange(this);");
HtmlGenericControl js = new HtmlGenericControl("script");
js.Attributes["type"] = "text/javascript";
js.Attributes["src"] = "/javascript/listformat.js";
this.Page.Header.Controls.Add(js);

string rawValues = System.Configuration.ConfigurationManager.AppSettings.Get("ListFormats");
string[] appSettingsValues = rawValues.Split(Convert.ToChar("|"));

for (int i = 0; i < appSettingsValues.Length; i++)
{
string[] appValues = appSettingsValues[i].Split(Convert.ToChar(";"));
listFormat.Items.Add(new ListItem(appValues[0], appValues[1]));
}

listFormat.SelectedValue = customPageList.ListFormat;

var labellistFormat = new System.Web.UI.WebControls.Label() { Text = "List Format" };
divListFormat.Controls.Add(labellistFormat);
divListFormat.Controls.Add(brControl);
divListFormat.Controls.Add(listFormat);

labellistFormat.AssociatedControlID = listFormat.ID;

// create a row Link ************************************
divLink.Attributes.Add("id", "divLink");
link = new EPiServer.Web.WebControls.InputPageReference() { ID = "link" };
var labellink = new System.Web.UI.WebControls.Label() { Text = "Link" };
divLink.Controls.Add(labellink);
divLink.Controls.Add(link);
divListControl.Controls.Add(divLink);
labellink.AssociatedControlID = link.ID;

// create a row Multi ************************************

divMulti.Attributes.Add("id", "divMulti");
multiPageCollection = new Control();
multiPageCollection = this.Page.LoadControl(UriSupport.ResolveUrlFromUIBySettings("Edit/PropertyLinkCollectionEditControl.ascx"));
if (customPageList.LinkCollection != null)
{
((IPropertyLinkCollectionEditControl)multiPageCollection).OriginalLinkCollection = customPageList.LinkCollection;
}
else
{
((IPropertyLinkCollectionEditControl)multiPageCollection).OriginalLinkCollection = new EPiServer.SpecializedProperties.LinkItemCollection();
}
//multiPageCollection = new PropertyLinkCollectionControl();// { ID = "multi" };

// Is something missing here like?
// multiPageCollection.SetupControl();

var labelMulti = new System.Web.UI.WebControls.Label() { Text = "Multi Page Collection" };
divMulti.Controls.Add(labelMulti);
divMulti.Controls.Add(brControl);
divMulti.Controls.Add(multiPageCollection);
divListControl.Controls.Add(divMulti);
//labelMulti.AssociatedControlID = multiPageCollection.ID;
//((IPropertyLinkCollectionEditControl)multiPageCollection).OriginalLinkCollection = customPageList.PropertyLinkCollection.Links;

SetupEditControls();
}

///
/// Gets the CustomPageList instance for this IPropertyControl.
///
/// The property that is to be displayed or edited.
public CustomPageList CustomPageList
{
get
{
return PropertyData as CustomPageList;
}
}
}

 

#31884
Aug 13, 2009 16:11
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.