Click to See Complete Forum and Search --> : Loading and Destroying web user controls into a panel
see07
February 2nd, 2005, 12:20 PM
Hello:
I’ve a web form containing a web user control and a panel, this web user control have 2 buttons, when user push 1st button I need load a 2nd web user control into panel of web form. When user push 2nd button of 1st web user control I need destroy 2nd web user control which is into panel and place therein a 3rd web user control.
I’ll appreciate some ideas to attain it.
A.L. :wave:
see07
February 3rd, 2005, 09:50 AM
Just I tried with
Panel1.Controls.RemoveAt(0);
but it's not working.
Please some ideas... :cry:
A.L.
cmiskow
February 3rd, 2005, 12:47 PM
Since the user control on your form has no knowledge of other controls on the form, I would suggest adding a couple of custom events to your user control that are raised when the buttons are clicked, and in the event handlers on the web form you can add/remove controls from the panel.
Controls can be loaded into the panel using something like:
Panel1.Controls.Add(Page.LoadControl(p));
where p is a virtual path to the control's .ascx file.
However, dynamically loaded controls will not automatically persist between postbacks, so great effort must be put into re-adding them and restoring their state. If the user controls are fairly small, I would recommend simply adding them all at design time and toggling their Visible properties when the appropriate buttons are clicked.
see07
February 3rd, 2005, 04:58 PM
Thanks you cmiskow:
I'm already loadig web user control with
Panel1.Controls.Add(Page.LoadControl(p));
as you told me.
However as you told me when I show 3rd. web user control (which has a button, and when user push this button, web user control simply vanish). What would be the way you'd use to to do web user control persist?
Greetings :wave:
A.L.
cmiskow
February 4th, 2005, 09:57 AM
The problem I have encountered with this issue is that in order for the viewstate to work on dynamically added controls, they must be re-added each time on the Page.Init event. It seems that ASP.NET reloads control values in between Page.Init and Page.Load. So, controls must be added during Page.Init in order to be repopulated automatically. However, the ViewState object is not available during Page.Init, so how do you know which control to add at that time?
I have never found a reasonable solution to this problem. If your user controls do not require viewstates, then they can easily be re-added during Page.Load without problems.
Perhaps there is someone more enlightened who has a solution, but personally, since it is only 3 controls, I would just add them all to the panel at design time and change their Visible properties based on which button is clicked. This way you let ASP.NET handle all their states at a fairly small cost.
see07
February 8th, 2005, 09:45 AM
Well, about you wrote:
________________________________________________________________
Perhaps there is someone more enlightened who has a solution, but personally, since it is only 3 controls, I would just add them all to the panel at design time and change their Visible properties based on which button is clicked. This way you let ASP.NET handle all their states at a fairly small cost.
________________________________________________________________
This is way I am turning-around problem, now I am using only 3 web user controls but I am looking forward when I'll need more than 3 web user control. I am researching now to solve it when this day arrives.
Somebody with some ideas?
:wave:
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.