WebControl, CssStyleCollection and ViewState

Discussion in 'ASP .Net Building Controls' started by Marri Suliez, Jan 23, 2004.

  1. Marri Suliez

    Marri Suliez Guest

    Does anyone know how the Style property of the WebControl class persists
    between calls as if it's added to the ViewState? CssStyleCollection is
    not serializable.

    The following scenario leads me to believe it is being stored in
    ViewState somehow:
    1. Add a TextBox and a Button to a form
    2. Have the Button.Click event add value to the TextBox.Style collection
    3. Add a breakpoint in the Button.Click event handler immediately before
    the code in step #2 is executed
    4. Run the page
    5. Click the button
    6. When the code hits the breakpoint, note the number of TextBox.Style
    items (you'll get 4, for example)
    7. Allow the code in step #2 to run
    8. Check the number of TextBox.Style items again (you'll get 5)
    9. Click the button again
    10. Repeat step 6 and you'll get 5, leading me to believe ViewState is
    being used.

    Any ideas? Thanks.
    Marri Suliez, Jan 23, 2004
    #1
    1. Advertising

  2. Hi Marri,

    It is stored in viewstate. The ctor for CssStyleCollection takes a StateBag which is used for storing the style settings.

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    Victor Garcia Aprea [MVP], Jan 23, 2004
    #2
    1. Advertising

  3. Marri Suliez

    Marri Suliez Guest

    But don't objects need to be serializable in order? Maybe the code is just
    going through the collection's key-value pairs and saving those to the
    StateBag?

    If that's the case why didn't they just make the collection serializable?
    Have any ideas?

    Thanks for your help!


    "=?Utf-8?B?VmljdG9yIEdhcmNpYSBBcHJlYSBbTVZQXQ==?=" <>
    wrote in news::

    > Hi Marri,
    >
    > It is stored in viewstate. The ctor for CssStyleCollection takes a
    > StateBag which is used for storing the style settings.
    >
    Marri Suliez, Jan 23, 2004
    #3
  4. In this case CssStyleCollection doesn't need to be serializable because they're not serializing an instance of it. What it is doing is just serializing a simple string containing the defined style (and yes it does so by iterating through the attributes name/value and properly adding ':' and ';' separators). Let me know if you want to know any other related detail

    --
    Victor Garcia Apre
    Microsoft MVP | ASP.NE
    Looking for insights on ASP.NET? Read my blog
    http://obies.com/vga/blog.asp
    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    Victor Garcia Aprea [MVP], Jan 23, 2004
    #4
  5. Marri Suliez

    Teemu Keiski Guest

    Hi,

    If Style is Control's default style, created in CreateControlStyle and
    accessed via ControlStyle (and typed style members, like BackColor for
    example), it uses control's own ViewState collection to store the needed
    values. In WebControl CreateControlStyle is implemented as follows.

    protected virtual Style CreateControlStyle()
    {
    return new Style(base.ViewState);

    }

    If control exposes additional styles in addition to the ControlStyle, their
    state management needs to be done customized by overriding TrackViewState,
    oadViewState and SaveViewState methods of the control and manually
    specifying how state is tracked, loaded and saved.


    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist


    > "Marri Suliez" <> wrote in message
    > news:Xns94794EBB9822EMarriSuliezMarriSuli@24.168.128.90...
    >> Does anyone know how the Style property of the WebControl class persists
    >> between calls as if it's added to the ViewState? CssStyleCollection is
    >> not serializable.
    >>
    >> The following scenario leads me to believe it is being stored in
    >> ViewState somehow:
    >> 1. Add a TextBox and a Button to a form
    >> 2. Have the Button.Click event add value to the TextBox.Style collection
    >> 3. Add a breakpoint in the Button.Click event handler immediately before
    >> the code in step #2 is executed
    >> 4. Run the page
    >> 5. Click the button
    >> 6. When the code hits the breakpoint, note the number of TextBox.Style
    >> items (you'll get 4, for example)
    >> 7. Allow the code in step #2 to run
    >> 8. Check the number of TextBox.Style items again (you'll get 5)
    >> 9. Click the button again
    >> 10. Repeat step 6 and you'll get 5, leading me to believe ViewState is
    >> being used.
    >>
    >> Any ideas? Thanks.
    Teemu Keiski, Jan 23, 2004
    #5
    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. Marri Suliez
    Replies:
    1
    Views:
    2,131
    Teemu Keiski
    Jan 23, 2004
  2. Arthur Mnev
    Replies:
    0
    Views:
    532
    Arthur Mnev
    Feb 2, 2004
  3. Shawn B.

    CssStyleCollection As Property

    Shawn B., Sep 9, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    242
    Shawn B.
    Sep 15, 2003
  4. Marri Suliez

    WebControl, CssStyleCollection and ViewState

    Marri Suliez, Jan 23, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    207
    Teemu Keiski
    Jan 23, 2004
  5. Gary K

    Using CssStyleCollection (or Style class)

    Gary K, Dec 8, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    370
    Gary K
    Dec 8, 2004
Loading...

Share This Page