Property persistence in derivated control

O

Ornette

Hello,

I have some trouble to get my custom complex (collection) property to
persist in a derivated control (from Panel).
That works great when inherits from "Control" but doesn't work when inherits
from "Panel". I search all the web... I'm not sure if there is a restriction
or any solution.

I'm working with VS 2003, .NET 3.5.

To reproduce the problem, try to add some "AllowedRoles" : this is not
serialized in the page and if you close and re-open, you loose your data.

Do you have any idea ?

Thanks for any clue !!
Ornette


This is the isolated code :

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Linq;

using System.Text;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Collections;

namespace MyControl

{

[ToolboxData("<{0}:MyPanel runat=server></{0}:MyPanel>")]

[ParseChildren(true, "AllowedRoles")]

[PersistChildren(false)]

public class MyPanel : System.Web.UI.WebControls.Panel // <=== // Works
with public class MyPanel : Control !!!!!

{

public MyPanel() : base()

{


}


[Category("MyProperties")]

[NotifyParentProperty(true)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]

[PersistenceMode(PersistenceMode.InnerProperty)]

public RoleCollection AllowedRoles

{

get { return roles; }

}

private RoleCollection roles = new RoleCollection();



// Required to be able to properly deal with the Collection object ??



protected override void AddParsedSubObject(object obj)

{

if (obj is RoleControl)

{

this.AllowedRoles.Add((RoleControl)obj);

return;

}

}

}





////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////





[ToolboxData("<{0}:RoleControl runat=server></{0}:RoleControl>")]

public class RoleControl : Control

{

public RoleControl()

{

}

[NotifyParentProperty(true)]

[Browsable(true), Description("The allowed role")]

public string RoleName

{

get { return roleType; }

set { roleType = value; }

}

string roleType = "";


}



////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////////


public class RoleCollection : CollectionBase

{

public RoleCollection()

{

}

/// <summary>

/// Indexer property for the collection that returns and sets an item

/// </summary>

public RoleControl this[int index]

{

get

{

return (RoleControl)this.List[index];

}

set

{

this.List[index] = value;

}

}

public void Add(RoleControl role)

{

this.List.Add(role);

}



// NOT USED :

public void Insert(int index, RoleControl item)

{

this.List.Insert(index, item);

}

public void Remove(RoleControl role)

{

List.Remove(role);

}

public bool Contains(RoleControl role)

{

return this.List.Contains(role);

}

public int IndexOf(RoleControl role)

{

return List.IndexOf(role);

}

public void CopyTo(RoleControl[] array, int index)

{

List.CopyTo(array, index);

}

}

}
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top