Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > How to persist an arraylist property of controls in a custom control.

Reply
Thread Tools

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

 
 
Jeremy Chapman
Guest
Posts: n/a
 
      02-22-2006
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.WebContro ls", "myasp")]
namespace MyDotNet.Web.UI.WebControls
{
public class TabListBuilder : ControlBuilder
{
[System.Security.Permissions.PermissionSet(System.S ecurity.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.S ecurity.Permissions.SecurityAction.Demand,
Name="FullTrust")]
public override void AppendLiteralString(string s)
{
//Ignore
}
}

public class TabListItemBuilder : ControlBuilder
{
[System.Security.Permissions.PermissionSet(System.S ecurity.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.S ecurity.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(DesignerS erializationVisibility.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.S ecurity.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.S ecurity.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.ITypeDescriptorCon text context,
IServiceProvider provider, object value)
{
try
{
IWindowsFormsEditorService pSvc = null;

if (context != null &&
provider != null &&
context.Instance != null)
{
pSvc =
(IWindowsFormsEditorService)provider.GetService(ty peof(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.ITypeDescriptor Context context)
{
return UITypeEditorEditStyle.Modal;
}
}

}



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Does the clone() method of ArrayList<> make a copy of the objects in the ArrayList? xz Java 16 08-04-2007 10:33 PM
Custom Control : One custom property to array of controls anon ASP .Net Web Controls 0 01-15-2005 11:32 AM
a class inherited from ArrayList, is saved to ViewState, why the type of the object read from ViewSate is not the class, but the parent, ArrayList leal ting ASP .Net 1 02-10-2004 07:45 PM
writeObject with ArrayList of ArrayList? Kaidi Java 4 01-03-2004 08:16 PM
Iterate through ArrayList using an another ArrayList Saravanan Rathinavelu ASP .Net 3 08-19-2003 07:03 AM



Advertisments