Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Remove Dynamic User Controls from a collection

Reply
Thread Tools

Remove Dynamic User Controls from a collection

 
 
Stephan B
Guest
Posts: n/a
 
      01-03-2006
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".

 
Reply With Quote
 
 
 
 
Stephan B
Guest
Posts: n/a
 
      01-04-2006
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

 
Reply With Quote
 
 
 
 
Mike MacMillan
Guest
Posts: n/a
 
      01-04-2006
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[i] = someCollection[i];

//** 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[i]);
}

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


 
Reply With Quote
 
Stephan B
Guest
Posts: n/a
 
      01-11-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Dynamic Controls created by Dynamic Controls Ronald ASP .Net 2 01-17-2006 12:44 AM
Dynamic Events from Dynamic Controls on a User Control - Guess What? Broken! mytestemailaccount@gmail.com ASP .Net 5 03-23-2005 05:06 PM
Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls Denny Smolinski via .NET 247 ASP .Net Web Controls 1 05-04-2004 09:15 PM
How to handle Dynamic Controls within User Controls Michael Ramey ASP .Net 2 02-04-2004 06:46 PM



Advertisments