How to persist an arraylist property of controls in a custom control.

J

Jeremy Chapman

I have included below virtually all the code to a control I'm trying to
build. My issue is that an array list property in my control does not get
persisted properly to the aspx page code in design time. If I type the code
in the aspx manually it does get parsed correctly though.

This is an example of the aspx code that gets parsed correctly. For some
reason, if I changed update the Tab property of the control through the GUI
at design time, the changes are not persisted in the aspx page. As well,
if I change one of the other properties of the TabList control itself, all
the inner html of the control is removed from the aspx page. My control
consists of an arraylist property called tabs which contains instances of
TabListItem objects. The TabListItem object can contain various web
controls as child controls in it's Controls property. Any help here would
be greatly appreciated. Thanks.

<myasp:TabList id="TabList2g" runat="server" Width="328px" Height="48px"
BorderWidth="4px" CssClass="test23">
<myasp:TabListItem>
<asp:Button ID="test1" Text="Tab 1" Font-Bold="True"></asp:Button>
</myasp:TabListItem>
<myasp:TabListItem>
<asp:Button ID="test2" Text="Tab 2"></asp:Button>
</myasp:TabListItem>
<myasp:TabListItem>
<asp:Button ID="test3" Text="Tab 3 part1"></asp:Button>
<asp:Button ID="test4" Text="Tab 3 part2"></asp:Button>
</myasp:TabListItem>
<myasp:TabListItem>
<asp:Button ID="test5" Text="Tab 4 part1"></asp:Button>
<asp:Button ID="test6" Text="Tab 4 part2"></asp:Button>
</myasp:TabListItem>
</myasp:TabList>


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Security;

[assembly: System.Web.UI.TagPrefix("MyDotNet.Web.UI.WebControls", "myasp")]
namespace MyDotNet.Web.UI.WebControls
{
public class TabListBuilder : ControlBuilder
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
public override Type GetChildControlType(string tagName,
System.Collections.IDictionary attribs)
{
if (tagName.ToLower().EndsWith("tablistitem"))
{
return typeof(TabListItem);
}
return null;
}

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
public override void AppendLiteralString(string s)
{
//Ignore
}
}

public class TabListItemBuilder : ControlBuilder
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
public override Type GetChildControlType(string tagName,
System.Collections.IDictionary attribs)
{
if (tagName.ToLower().EndsWith("label"))
{
return typeof(Label);
}
else if (tagName.ToLower().EndsWith("button"))
{
return typeof(Button);
}
else if (tagName.ToLower().EndsWith("linkbutton"))
{
return typeof(LinkButton);
}
else if (tagName.ToLower().EndsWith("imagebutton"))
{
return typeof(ImageButton);
}
else if (tagName.ToLower().EndsWith("hyperlink"))
{
return typeof(HyperLink);
}
else if (tagName.ToLower().EndsWith("image"))
{
return typeof(Image);
}
else if (tagName.ToLower().EndsWith("literal"))
{
return typeof(Literal);
}

return null;
}

[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
public override void AppendLiteralString(string s)
{
//Ignore
}
}


[//ControlBuilderAttribute(typeof(TabListBuilder)), /*TabListBuilder will
parse child controls */
ParseChildren(true, "Tabs"),/*true - child elements are parsed as
properties, not controls*/
PersistChildren(false) /*false - persist child controls only as child
elements*/]
public class TabList : WebControl, INamingContainer
{
const string strCATEGORY_TABS = "Tabs";
const string strCATEGORY_APPEARANCE = "Appearance";

private Table pTable_m;
private ArrayList pTabs_m = new ArrayList();

private int iSelectedTabIndex_m = -1;

[EditorAttribute(typeof(TabListItemArrayListEditor),
typeof(System.Drawing.Design.UITypeEditor)), /*Custom editor for the
TabListItem objects in the ArrayList */
PersistenceMode(PersistenceMode.InnerProperty),
MergableProperty(false),
DesignerSerializationVisibilityAttribute(DesignerSerializationVisibility.Content)]
public ArrayList Tabs
{
get
{
return pTabs_m;
}
}

[Bindable(true),
Category(strCATEGORY_TABS),
DefaultValue(-1)]
public int SelectedTabIndex
{
get
{
return iSelectedTabIndex_m;
}
set
{
iSelectedTabIndex_m = value;
}
}

// Called at runtime when a child object is added to the collection.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
protected override void AddParsedSubObject(object obj)
{
TabListItem pItem = obj as TabListItem;
if (pItem != null)
{
pTabs_m.Add(pItem);
}
}

protected override void CreateChildControls()
{
pTable_m = new Table();
pTable_m.Rows.Add(new TableRow());
foreach(TabListItem pItem in pTabs_m)
{
TableCell pCell = new TableCell();
pTable_m.Rows[0].Cells.Add(pCell);

pCell.Controls.Add(pItem);
}
Controls.Clear();
Controls.Add(pTable_m);
}
}

/// <summary>
/// Represents 1 tab in the TabList control
/// </summary>
[ControlBuilderAttribute(typeof(TabListItemBuilder)),/*TabListItemBuilder
will parse child controls */
ToolboxItem(false),/*Do not show this control in the toolbox*/
ParseChildren(false)/*false - child elements are parsed as controls, not
properties*/,
PersistChildren(true) /*false - persist child controls only as child
elements*/]
public class TabListItem : WebControl, INamingContainer
{
private ArrayList pTabControls_m = new ArrayList();

public TabListItem()
{

}

// Called at runtime when a child object is added to the collection.
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand,
Name="FullTrust")]
protected override void AddParsedSubObject(object obj)
{
Control pItem = obj as Control;
if (pItem != null)
{
Controls.Add(pItem);
}
}

}


public class TabListItemArrayListEditor :
System.Drawing.Design.UITypeEditor
{
public TabListItemArrayListEditor() : base()
{
}

public override object
EditValue(System.ComponentModel.ITypeDescriptorContext context,
IServiceProvider provider, object value)
{
try
{
IWindowsFormsEditorService pSvc = null;

if (context != null &&
provider != null &&
context.Instance != null)
{
pSvc =
(IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
}

if (pSvc != null)
{
frmTabEditor pForm = new frmTabEditor();

ArrayList pCollection = (ArrayList)value;

pForm.Tabs = pCollection;

if (pForm.ShowDialog() == DialogResult.OK)
{
value = pCollection;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
return value;
}

// Indicates whether the UITypeEditor provides a form-based (modal)
dialog,
// drop down dialog, or no UI outside of the properties window.
public override System.Drawing.Design.UITypeEditorEditStyle
GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top