|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Remove Dynamic User Controls from a collectionother 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". 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,
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: Show quoteHide quote > 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 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.
Any hints on how to do frames in ASP.Net 2.0?
Code to select item in dropdown populated using TableAdapter Custom Treenodes in Treeview Problem in Setting SelectedValue for DropdownList in Form Would like to get two items from a dropdownlist DropDownList VS TextBox Gridview - Accessing a field value in current row controlToValidate property of compareValidator combo box customs TreeView |
|||||||||||||||||||||||