Dynamic controls and viewstate (mis)management

Discussion in 'ASP .Net' started by Scott Roberts, Feb 20, 2008.

  1. I always thought that the viewstate "keys" included the control ID. As long
    as the control IDs were unique, there shouldn't be any conflicts. Well, it
    appears that that may not be the case with dynamic controls. Our application
    was having a problem very similar to the one described here:

    http://weblogs.asp.net/alessandro/a...typical-problem-with-an-obvious-solution.aspx

    Unfortunately, the "fix" isn't quite so simple. Our problem exists with
    dynamically loaded user controls, and simply turning off the viewstate is
    not an option. Basically, what we are doing is dynamically loading different
    user controls into an update panel depending on the user's selection in an
    adjacent treeview. So if the user is viewing uc1 then clicks the tree node
    for uc2, during postback we simply create and display uc2. The problem,
    then, is that the viewstate from uc1 *may* stomp on the newly added uc2. The
    "fix" is to re-create uc1 during postback (even though we have no use for
    it), add it to the control tree, remove it from the control tree, then
    create uc2.

    I guess I'm just wondering if there is a way to avoid having to re-create a
    user control that we are not going to display just to clean up the
    viewstate?

    Here is a small code snippet that demonstrates the problem:

    aspx snippet:

    <form id="form1" runat="server">
    <div>
    <asp:Button ID="PostbackButton" runat="server" Text="Post Back"
    /></div>
    </form>

    aspx.cs snippet:

    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    Button b = new Button();
    b.ID = "button1";
    Page.Form.Controls.Add(b);

    // Modify text *after* adding to Page.Form.Controls
    // so that the Text property ends up in viewstate.
    b.Text = "Button1";
    }
    else
    {
    // Uncomment this block to make it work.
    //Button b = new Button();
    //Page.Form.Controls.Add(b);
    //Page.Form.Controls.Remove(b);

    Label l = new Label();
    l.ID = "label1";

    // Set text *before* adding to Page.Form.Controls
    // so that the value gets overwritten by the
    // viewstate value (if any).
    l.Text = "label1";

    Page.Form.Controls.Add(l);
    }
    }

    When you click the PostbackButton you will notice that the Label will
    display "Button1". It's getting the viewstate property from the Button.Text
    created on the previous page_load (!IsPostBack). If you uncomment the
    specified code block, the Button created during IsPostBack will "eat" the
    viewstate property and the label will behave "properly".

    My question is: Why isn't the control.id utilized as part of the viewstate
    key for dynamic controls?
     
    Scott Roberts, Feb 20, 2008
    #1
    1. Advertisements

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. Abel

    dynamic controls and viewstate?

    Abel, Jun 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    557
    Axel Dahmen
    Jul 1, 2003
  2. =?Utf-8?B?SmVycnkgU3BlbmNlcg==?=

    Viewstate and dynamic controls

    =?Utf-8?B?SmVycnkgU3BlbmNlcg==?=, Nov 9, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,994
    Steve C. Orr [MVP, MCSD]
    Nov 9, 2004
  3. John
    Replies:
    8
    Views:
    3,824
    contour
    Dec 1, 2008
  4. =?Utf-8?B?VFdFQg==?=

    IIS mis-Configuration? SSI referencing .aspx file

    =?Utf-8?B?VFdFQg==?=, Jul 27, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    3,664
    Steven Cheng[MSFT]
    Jul 29, 2005
  5. Ronald
    Replies:
    2
    Views:
    1,055
    Robbe Morris [C# MVP]
    Jan 17, 2006
  6. =?Utf-8?B?RGlmZmlkZW50?=

    Dynamic controls and viewstate

    =?Utf-8?B?RGlmZmlkZW50?=, Mar 17, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    3,379
    Teemu Keiski
    Mar 18, 2006
  7. Replies:
    4
    Views:
    440
    Alf P. Steinbach
    Feb 21, 2007
  8. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    658
    Eidolon
    May 4, 2004
Loading...