Remove Dynamic User Controls from a collection

Discussion in 'ASP .Net Web Controls' started by Stephan B, Jan 3, 2006.

  1. Stephan B

    Stephan B Guest

    Hi, for some reason I cannot find my yesterdays post, so here is an
    other try. I have got some Dynamic User Controls (UC's) on my page.
    The user can add as many as he likes by clicking a button. This all
    works fine. Every UC has also a button to remove itself from the page.
    Here is were the trouble starts. I can remove the control by using the
    remove function of the placeholder on which the control is situated. I
    also keep track of the number of controls in this placeholder by using
    an arraylist containing the ID's of the control. Of course, I also
    remove the control from this arralist. I know that on every postback I
    have to recreate the control and there is the problem. Because the
    delete event fires after the page_load. So on a postback the deleted
    control shouldn't be there nomore. But removing it from the
    collection, doesn't remove it from the viewstate. So, when I build my
    page again. There are viewer controls, but the the content of the
    textboxes (which are on my UC) isn't right anymore. i.e. let's say I
    have 4 UC on my page (all created dynamically by a buttonclick)
    UC 1 with a textbox. Value = aa
    UC 2 with a textbox. Value = bb
    UC 3 with a textbox. Value = cc
    UC 4 with a textbox. Value = dd
    Now, if I remove let say UC 3. This works fine. There are only 3 UC's
    left.But if I remove an other control for example UC 2, the page show's
    up with again 3 UC's. UC1 with the right value and UC 4 with the right
    value + an empty UC. The longer the list of UC's becomes the stanger
    the behavior. I have been struggeling for twee weeks now, but know one
    seems to have the right answer. I read all about page life-cicles,
    viewstate. The only conclusion I have right now is that when I remove
    the UC from a collection, it isn't removed from the viewstate (on
    postback). The viewstate keeps the orginial ID from the Controls, so
    when removing UC 3 (with an internal ID of _Ctrl2, on a postback the
    last element of these controls, which is UC 4 with _Ctrl3 is removed.
    So it builds up 3 controls, but not the right ones. Does anybody have
    an idea. I can post some code, but it's a lot. Thats because I use
    nested controls to make is "easier".
    Stephan B, Jan 3, 2006
    #1
    1. Advertising

  2. Stephan B

    Stephan B Guest

    After reading tons of articles on this newsgroups, the solution for me
    was to toggle the visibility of the control. This works fine, so I'll
    stick to that. Using the remove funtion of the controlcollection is no
    option because on a post-back the index of the viewstate and the
    contorlID don't match anymore. This is why some values couldn't be
    found on a postback
    Stephan B, Jan 4, 2006
    #2
    1. Advertising

  3. Stephan,
    if you need more strict control over exactly what goes in and comes
    out of the ViewState, you should try overriding the SaveViewState() and
    LoadViewState() methods of your control/page. for ex:

    this is assuming a theoretical collection of something called
    someCollection...

    protected override object SaveViewState() {
    object[] arStuff;

    //** create our viewstate collection of "stuff"
    arStuff = new Object[someCollection.Length];

    //** put our "stuff" in the viewstate array
    for(int i=0;i<someCollection.Length;i++)
    arStuff = someCollection;

    //** return the array of stuff we want persisted
    return arStuff;
    }

    protected override void LoadViewState(object state) {
    object[] arStuff;

    if(state==null)
    return;

    //** cast the viewstate object to our object array
    arStuff = (object[])state;

    //** add all our "stuff" back to our in page collection. this can
    add
    //** controls to a control collection, add values to an arraylist,
    etc...
    for(int i=0;i<arStuff.Length;i++)
    someCollection.Add(arStuff);
    }

    LoadViewState happens after OnInit, before OnLoad...so you can
    recreate any dynamic controls and restore their viewstate value here,
    so they exist in time to be relevant to the event being fired.


    let me know if this helps,
    Mike MacMillan



    Stephan B wrote:
    > After reading tons of articles on this newsgroups, the solution for me
    > was to toggle the visibility of the control. This works fine, so I'll
    > stick to that. Using the remove funtion of the controlcollection is no
    > option because on a post-back the index of the viewstate and the
    > contorlID don't match anymore. This is why some values couldn't be
    > found on a postback
    Mike MacMillan, Jan 4, 2006
    #3
  4. Stephan B

    Stephan B Guest

    Hi Mike,

    thanks for the tip. I read about this option too, but was not sure how
    to use it. As said before, hiding the control works fine for me, but
    just out of cursiousity, I'm going to try this one too. As far as I can
    think of it now, it should work for my case. I'll let you know.

    Thanks again.
    Stephan B, Jan 11, 2006
    #4
    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. Michael Ramey
    Replies:
    2
    Views:
    682
    Michael Ramey
    Feb 4, 2004
  2. Replies:
    5
    Views:
    5,578
  3. Ronald
    Replies:
    2
    Views:
    835
    Robbe Morris [C# MVP]
    Jan 17, 2006
  4. Øyvind Isaksen
    Replies:
    1
    Views:
    960
    Øyvind Isaksen
    May 18, 2007
  5. 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:
    459
    Eidolon
    May 4, 2004
Loading...

Share This Page