Collection Property in web custom control

Discussion in 'ASP .Net Building Controls' started by Class, Jan 16, 2007.

  1. Class

    Class Guest

    Hi all,

    I'm building a custom control. I would like that on the control you can set
    some 'rights'
    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
    time)
    So I put them in a generic list:

    [Category("Rights")]
    [DefaultValue("")]
    [Localizable(true)]
    [Bindable(true)]
    public List<GroupRights> ControlRights
    {
    get
    {
    List<GroupRights> temp = (List<GroupRights>)ViewState["ControlRights"];
    return ((object)temp == null) ? new List<GroupRights>() : temp;
    }

    set
    {
    ViewState["ControlRights"] = value;
    }}

    In desgin time I get the controls window with the GrouRights enum to choose
    from.
    But when I add one right in the collection window I get the message:

    Cannot create an object of type
    'System.Collections.Generic.List`1[[GroupRights, MaxControls,
    Version=1.0.0.0, 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?

    tia,

    Class
    Class, Jan 16, 2007
    #1
    1. Advertising

  2. Your class must be serializible. Or use Session instead of ViewState.

    Sincerely yours,
    Michael B. Tkachev.


    "Class" <NoSpam@Class> wrote in message
    news:...
    > Hi all,
    >
    > I'm building a custom control. I would like that on the control you can
    > set some 'rights'
    > 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 time)
    > So I put them in a generic list:
    >
    > [Category("Rights")]
    > [DefaultValue("")]
    > [Localizable(true)]
    > [Bindable(true)]
    > public List<GroupRights> ControlRights
    > {
    > get
    > {
    > List<GroupRights> temp = (List<GroupRights>)ViewState["ControlRights"];
    > return ((object)temp == null) ? new List<GroupRights>() : temp;
    > }
    >
    > set
    > {
    > ViewState["ControlRights"] = value;
    > }}
    >
    > In desgin time I get the controls window with the GrouRights enum to
    > choose from.
    > But when I add one right in the collection window I get the message:
    >
    > Cannot create an object of type
    > 'System.Collections.Generic.List`1[[GroupRights, MaxControls,
    > Version=1.0.0.0, 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?
    >
    > tia,
    >
    > Class
    >
    >
    >
    Michael Tkachev, Jan 17, 2007
    #2
    1. Advertising

  3. Class

    Class Guest

    Hi Michael,

    Thanks for your response.

    > Your class must be serializible. Or use Session instead of ViewState.


    Which class do you mean, that needs to be serializable?
    The enum? or The List class?
    The list is a generic .NET collection class which is serializable already,
    right?
    Session?? There is no Session object in a web custom control, what do you
    mean??

    Thanks,
    Class

    "Michael Tkachev" <> wrote in message
    news:...
    > Your class must be serializible. Or use Session instead of ViewState.
    >
    > Sincerely yours,
    > Michael B. Tkachev.
    >
    >
    > "Class" <NoSpam@Class> wrote in message
    > news:...
    >> Hi all,
    >>
    >> I'm building a custom control. I would like that on the control you can
    >> set some 'rights'
    >> 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 time)
    >> So I put them in a generic list:
    >>
    >> [Category("Rights")]
    >> [DefaultValue("")]
    >> [Localizable(true)]
    >> [Bindable(true)]
    >> public List<GroupRights> ControlRights
    >> {
    >> get
    >> {
    >> List<GroupRights> temp = (List<GroupRights>)ViewState["ControlRights"];
    >> return ((object)temp == null) ? new List<GroupRights>() : temp;
    >> }
    >>
    >> set
    >> {
    >> ViewState["ControlRights"] = value;
    >> }}
    >>
    >> In desgin time I get the controls window with the GrouRights enum to
    >> choose from.
    >> But when I add one right in the collection window I get the message:
    >>
    >> Cannot create an object of type
    >> 'System.Collections.Generic.List`1[[GroupRights, MaxControls,
    >> Version=1.0.0.0, 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?
    >>
    >> tia,
    >>
    >> Class
    >>
    >>
    >>

    >
    >
    Class, Jan 17, 2007
    #3
  4. Take the "Page" property and there you can find Session

    "Class" <NoSpam@Class> wrote in message
    news:...
    > Hi Michael,
    >
    > Thanks for your response.
    >
    >> Your class must be serializible. Or use Session instead of ViewState.

    >
    > Which class do you mean, that needs to be serializable?
    > The enum? or The List class?
    > The list is a generic .NET collection class which is serializable already,
    > right?
    > Session?? There is no Session object in a web custom control, what do you
    > mean??
    >
    > Thanks,
    > Class
    >
    > "Michael Tkachev" <> wrote in message
    > news:...
    >> Your class must be serializible. Or use Session instead of ViewState.
    >>
    >> Sincerely yours,
    >> Michael B. Tkachev.
    >>
    >>
    >> "Class" <NoSpam@Class> wrote in message
    >> news:...
    >>> Hi all,
    >>>
    >>> I'm building a custom control. I would like that on the control you can
    >>> set some 'rights'
    >>> 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 time)
    >>> So I put them in a generic list:
    >>>
    >>> [Category("Rights")]
    >>> [DefaultValue("")]
    >>> [Localizable(true)]
    >>> [Bindable(true)]
    >>> public List<GroupRights> ControlRights
    >>> {
    >>> get
    >>> {
    >>> List<GroupRights> temp = (List<GroupRights>)ViewState["ControlRights"];
    >>> return ((object)temp == null) ? new List<GroupRights>() : temp;
    >>> }
    >>>
    >>> set
    >>> {
    >>> ViewState["ControlRights"] = value;
    >>> }}
    >>>
    >>> In desgin time I get the controls window with the GrouRights enum to
    >>> choose from.
    >>> But when I add one right in the collection window I get the message:
    >>>
    >>> Cannot create an object of type
    >>> 'System.Collections.Generic.List`1[[GroupRights, MaxControls,
    >>> Version=1.0.0.0, 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?
    >>>
    >>> tia,
    >>>
    >>> Class
    >>>
    >>>
    >>>

    >>
    >>

    >
    >
    Michael Tkachev, Jan 18, 2007
    #4
  5. 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
    this.



    "Michael Tkachev" wrote:

    > Take the "Page" property and there you can find Session
    >
    > "Class" <NoSpam@Class> wrote in message
    > news:...
    > > Hi Michael,
    > >
    > > Thanks for your response.
    > >
    > >> Your class must be serializible. Or use Session instead of ViewState.

    > >
    > > Which class do you mean, that needs to be serializable?
    > > The enum? or The List class?
    > > The list is a generic .NET collection class which is serializable already,
    > > right?
    > > Session?? There is no Session object in a web custom control, what do you
    > > mean??
    > >
    > > Thanks,
    > > Class
    > >
    > > "Michael Tkachev" <> wrote in message
    > > news:...
    > >> Your class must be serializible. Or use Session instead of ViewState.
    > >>
    > >> Sincerely yours,
    > >> Michael B. Tkachev.
    > >>
    > >>
    > >> "Class" <NoSpam@Class> wrote in message
    > >> news:...
    > >>> Hi all,
    > >>>
    > >>> I'm building a custom control. I would like that on the control you can
    > >>> set some 'rights'
    > >>> 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 time)
    > >>> So I put them in a generic list:
    > >>>
    > >>> [Category("Rights")]
    > >>> [DefaultValue("")]
    > >>> [Localizable(true)]
    > >>> [Bindable(true)]
    > >>> public List<GroupRights> ControlRights
    > >>> {
    > >>> get
    > >>> {
    > >>> List<GroupRights> temp = (List<GroupRights>)ViewState["ControlRights"];
    > >>> return ((object)temp == null) ? new List<GroupRights>() : temp;
    > >>> }
    > >>>
    > >>> set
    > >>> {
    > >>> ViewState["ControlRights"] = value;
    > >>> }}
    > >>>
    > >>> In desgin time I get the controls window with the GrouRights enum to
    > >>> choose from.
    > >>> But when I add one right in the collection window I get the message:
    > >>>
    > >>> Cannot create an object of type
    > >>> 'System.Collections.Generic.List`1[[GroupRights, MaxControls,
    > >>> Version=1.0.0.0, 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?
    > >>>
    > >>> tia,
    > >>>
    > >>> Class
    > >>>
    > >>>
    > >>>
    > >>
    > >>

    > >
    > >

    >
    >
    >
    Nathaniel Greene, Jan 20, 2007
    #5
  6. Class

    Class Guest

    Hi Nathaniel,

    Thank you for your response.
    When I try your suggestion and I try to add a right in the colleciton
    Editor:
    'Unable to cast object of type
    'System.Collections.Generic.List`1[GroupRights]' to type
    'System.Collections.Generic.List`1[GroupRights]''

    This is the code I have now:
    public List<GroupRights> Rights
    {
    get
    {
    if (ViewState["Rights"] != null)
    {
    List<GroupRights> s = new
    List<GroupRights>((IEnumerable<GroupRights>)ViewState["Rights"]);
    return ((s == null) ? new List<GroupRights>() : s);
    }
    else
    {
    return null;
    }
    }
    set
    {
    if (value != null)
    ViewState["Rights"] = value.ToArray();
    else
    {
    List<GroupRights> s = new List<GroupRights>();
    s.Add((GroupRights)1);
    ViewState["LikMe"] = s;
    }
    }
    }

    Regards,
    Maarten

    "Nathaniel Greene" <> wrote in message
    news:...
    > 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
    > this.
    >
    >
    >
    > "Michael Tkachev" wrote:
    >
    >> Take the "Page" property and there you can find Session
    >>
    >> "Class" <NoSpam@Class> wrote in message
    >> news:...
    >> > Hi Michael,
    >> >
    >> > Thanks for your response.
    >> >
    >> >> Your class must be serializible. Or use Session instead of ViewState.
    >> >
    >> > Which class do you mean, that needs to be serializable?
    >> > The enum? or The List class?
    >> > The list is a generic .NET collection class which is serializable
    >> > already,
    >> > right?
    >> > Session?? There is no Session object in a web custom control, what do
    >> > you
    >> > mean??
    >> >
    >> > Thanks,
    >> > Class
    >> >
    >> > "Michael Tkachev" <> wrote in message
    >> > news:...
    >> >> Your class must be serializible. Or use Session instead of ViewState.
    >> >>
    >> >> Sincerely yours,
    >> >> Michael B. Tkachev.
    >> >>
    >> >>
    >> >> "Class" <NoSpam@Class> wrote in message
    >> >> news:...
    >> >>> Hi all,
    >> >>>
    >> >>> I'm building a custom control. I would like that on the control you
    >> >>> can
    >> >>> set some 'rights'
    >> >>> 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 time)
    >> >>> So I put them in a generic list:
    >> >>>
    >> >>> [Category("Rights")]
    >> >>> [DefaultValue("")]
    >> >>> [Localizable(true)]
    >> >>> [Bindable(true)]
    >> >>> public List<GroupRights> ControlRights
    >> >>> {
    >> >>> get
    >> >>> {
    >> >>> List<GroupRights> temp =
    >> >>> (List<GroupRights>)ViewState["ControlRights"];
    >> >>> return ((object)temp == null) ? new List<GroupRights>() : temp;
    >> >>> }
    >> >>>
    >> >>> set
    >> >>> {
    >> >>> ViewState["ControlRights"] = value;
    >> >>> }}
    >> >>>
    >> >>> In desgin time I get the controls window with the GrouRights enum to
    >> >>> choose from.
    >> >>> But when I add one right in the collection window I get the message:
    >> >>>
    >> >>> Cannot create an object of type
    >> >>> 'System.Collections.Generic.List`1[[GroupRights, MaxControls,
    >> >>> Version=1.0.0.0, 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?
    >> >>>
    >> >>> tia,
    >> >>>
    >> >>> Class
    >> >>>
    >> >>>
    >> >>>
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>
    >>
    Class, Jan 22, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Dupont
    Replies:
    0
    Views:
    462
    Lloyd Dupont
    Jul 27, 2003
  2. -=Chris=-
    Replies:
    2
    Views:
    5,584
    -=Chris=-
    May 23, 2004
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    965
    Øyvind Isaksen
    May 18, 2007
  4. Donald Simpson

    Web Custom Control & Custom Collection problem

    Donald Simpson, Jul 11, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    129
    Donald Simpson
    Jul 11, 2003
  5. Class

    Collection Property in web custom control

    Class, Jan 16, 2007, in forum: ASP .Net Web Controls
    Replies:
    5
    Views:
    165
    Class
    Jan 22, 2007
Loading...

Share This Page