Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Hello there.
I would like to add a MultiPageControl to a Custom Property next to other properties that make up the control. I'm having trouble in the CreateEditControls() override. I think a SetupControl() or some other method of the MultiPageControl needs to be called up. All I get is a "Object reference not set to an instance of an object".
There is little documentation on this. Does any one have a better idea of how to do this?
Thanks
Here's the code:
CustomPageListControl ***************************************
public class CustomPageListControl : EPiServer.Web.PropertyControls.PropertyStringControl
{
#region Controls
protected System.Web.UI.WebControls.DropDownList listFormat;
protected EPiServer.Web.WebControls.InputPageReference link;
protected MultiPageControl 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.MultiPageCollection != null)
{
this.multiPageCollection.PropertyMultiPage.SelectedLinkItems = customPageList.MultiPageCollection;
}
}
///
/// 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;
customPageList.MultiPageCollection = (this.multiPageCollection.PropertyMultiPage != null) ? this.multiPageCollection.PropertyMultiPage.SelectedLinkItems : 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()
{
// 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]));
}
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 MultiPageControl() { 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;
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;
}
}
}
CustomPageList *******************************************
[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();
}
}
CustomPageListDetails ***************************************
public class CustomPageListDetails
{
public string ListFormat { get; set; }
public PageReference Link { get; set; }
public MultipageLinkItemCollection MultiPageCollection { get; set; }
#region Helper Methods
///
The XML./// Gets the promo from string.
///
///
///
public static CustomPageListDetails GetSummaryFromString(string xml)
{
return SerializeUtils.DeserializeObject(xml);
}
///
/// Gets the promo from the CurrentPage.
///
/// Name of the property.
///
public static CustomPageListDetails GetSummaryFromPage(string PropertyName)
{
return GetSummaryFromPage(PropertyName, PageDateHelper.CurrentPage);
}
///
/// Gets the promo from page.
///
/// Name of the property.
/// The page.
///
public static CustomPageListDetails GetSummaryFromPage(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 GetSummaryFromString(XMLstr);
}
return new CustomPageListDetails();
}
#endregion
}