Collection Property in web custom control - 01-16-2007 , 11:07 AM
I'm building a custom control. I would like that on the control you can set
For that I have an enum:
public enum GroupRights
Group0 = 0,
Group1 = 1,
Group2 = 2,
Group3 = 3,
Group4 = 4,
Group5 = 5,
In design time I want to set these right (can be multiple rights at the same
So I put them in a generic list:
public List<GroupRights> ControlRights
List<GroupRights> temp = (List<GroupRights>)ViewState["ControlRights"];
return ((object)temp == null) ? new List<GroupRights>() : temp;
ViewState["ControlRights"] = value;
In desgin time I get the controls window with the GrouRights enum to choose
But when I add one right in the collection window I get the message:
Cannot create an object of type
Version=126.96.36.199, Culture=neutral, PublicKeyToken=null]]' from its string
representation '(Collection)' for the 'ControlRights' property.
Also the property can not be read out during runtime.
What am I doing wrong or forgetting?
Re: Collection Property in web custom control - 01-17-2007 , 07:27 AM
Your class must be serializible. Or use Session instead of ViewState.
Michael B. Tkachev.
m_tkachev (AT) hotmail (DOT) com
"Class" <NoSpam@Class> wrote
Re: Collection Property in web custom control - 01-17-2007 , 07:50 AM
Thanks for your response.
The enum? or The List class?
The list is a generic .NET collection class which is serializable already,
Session?? There is no Session object in a web custom control, what do you
"Michael Tkachev" <m_tkachev (AT) hotmail (DOT) com> wrote
Re: Collection Property in web custom control - 01-18-2007 , 06:56 AM
Take the "Page" property and there you can find Session
"Class" <NoSpam@Class> wrote
Re: Collection Property in web custom control - 01-20-2007 , 01:10 AM
Whoa, Whoa, Whoa, Whoa.
Remember that a session should only be used to store information when this
information is stored on a "per" user basis. What if you have 30 of these
controls on a single web page and the user is browsing 2 different web pages
at the same time ont he same site under the same session. That's 60 controls
overwriting each other.
Class, what is happening is the fact that you are trying to serialize Enums.
Your first step should be to convert your List<EnumRights> to a enumrights,
this is done through the .ToArray() method.
Do this when you save your items in the viewstaet. When you load your
viewstaet you can create a new List<EnumRights> by using the
MyEnumRights = new List<EnumRights>((List<Enumrights>) viewstate["bleah"]);
The Constructor overload for a List<> accepts Ienumerable which a type
implements so this works. This should solve all of your serialization
problems. Worst case scenario - you may have to, assuming your enums have
values, convert it to an array of integers - but I don't think you have to do
"Michael Tkachev" wrote:
Re: Collection Property in web custom control - 01-22-2007 , 04:22 AM
Thank you for your response.
When I try your suggestion and I try to add a right in the colleciton
'Unable to cast object of type
'System.Collections.Generic.List`1[GroupRights]' to type
This is the code I have now:
public List<GroupRights> Rights
if (ViewState["Rights"] != null)
List<GroupRights> s = new
return ((s == null) ? new List<GroupRights>() : s);
if (value != null)
ViewState["Rights"] = value.ToArray();
List<GroupRights> s = new List<GroupRights>();
ViewState["LikMe"] = s;
"Nathaniel Greene" <Nathaniel.Greene (AT) community (DOT) nospam> wrote
|Thread Tools||Search this Thread|