 |
|
|
 |
|
|
 |
|
 |
Hi,
I am working on Dynamic User controls that is .ascx on a single aspx page.
I have aspx where i have link buttons on that when i click on a link it has to load ascx control on that page. It is working properly but the main problem is when i am using back button and performing action on that usercontrol it is giving error . will plz help me for that .
|
|
|
|
 |
|
 |
I have several usercontrols that I load dynamically. They are all now piling up on the page..showing them all. Do I need to do a Remove?
My Application is like a navigatin step thru. Each step is a new usercontrol. Navigation is controlled by Dropdown.
With each click of drop down, the controls accumulate on the page.
modified on Wednesday, February 27, 2008 2:03 PM
|
|
|
|
 |
|
 |
Hi Wesam and others,
I like the way you have presented this very common yet not much explored issue of loading dynamic usercontrols.
I have tested several times with your code and have yet to use it in my application. There are still some things in my test that go wrong, mostly errors of viewstate tree not being created.
I was wondering if you could provide some more examples of some real world use, like adding user controls dynamically in an ASP.Net Ajax Tab and make the tab add and edit data.
Also, is this a sure shot way of loading user controls in a production environment?
Thanks.
|
|
|
|
 |
|
 |
Hello,
I was wondering how you can pass values between the dynamically loaded user controls.
Say I have a static user control (UC1) on my page which has a Dropdownlist.
I add another control dynamically (UC2) into a placeholder on the page.
On the selected index changed for the Dropdownlist in UC1 I want to fire an event which will make a panel visible in UC2.
An ideas on how I can do this?
Thanks
|
|
|
|
 |
|
 |
In such cases the best practice is to have the client page control its UserControls, i.e. to have UserControls publish their desired properties/routines and have the client page do the work.
Back to your question, the solution would be:
1. Create a public event & handler that suits your needs, if no specific needs are required you may simply use the standard System.EventHandler
public event EventHandler OnDropDownSelectedChanged;
2. Make UC1.DropDownList.AutoPostBack = True
3. Create an event handler for UC1 that handles SelectedIndexChange
private void DropDownList_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (OnDropDownSelectedChanged != null)
{
OnDropDownSelectedChanged(sender, e);
}
}
4. In UC2, create a public property to access the Panel’s Visible property
public Boolean PanelVisible
{
get
{
return Panel.Visible;
}
set
{
Panel.Visible = value;
}
}
5. In your main page, create an event handler of type System.EventHandler
private void uc1_OnDropDownSelectedChanged(object sender, EventArgs e)
{
uc2.PanelVisible = !uc2.PanelVisible;
}
6. In your main page, upon loading UC1, set its public event handler to the one you’ve just created
private void uc1_OnDropDownSelectedChanged(object sender, EventArgs e)
{
uc2.PanelVisible = !uc2.PanelVisible;
}
7. We’re done
Hope this helps,
Wesam
|
|
|
|
 |
|
 |
Thanks for the explanation Wesam. But somehow I still am not sure how to access a public property in a dynamically loaded UserControl. Could you kindly post an example.
Thanks.
|
|
|
|
 |
|
 |
Hello Wesam,
Thanks for the great example.
I had a question about how you would unload or remove the previously added controls from the placeholder when you add a new one.
I have 5 UserControls that a user can toggle through on my ASPX page, I want to be able to remove the previous ones when I click to load a new instance of one of the 5 UserControls.
I am assuming that by unloading it will make the page load faster since the unwanted UserControl is not loaded.
Thanks
|
|
|
|
 |
|
 |
I am not sure I undersnatd your question quite well? You can unload the control simple by calling: PlaceHoledr.Controls.Remove(ascx_control)? I'm sure this is not your question
Wesam
|
|
|
|
 |
|
 |
I have a usercontrol that I created with certain fields on, I added a panel and then a nother usercontrol. I then do some reading and repeat the second usercontrol page down depends on a certain value. I need to replicate the second usecontrol over the the first user control to the right hand side.
Can someone please help me.
|
|
|
|
 |
|
 |
I am not sure I understand your question Could you elaborate a little bit borther Loius
Wesam
|
|
|
|
 |
|
 |
"Hi dear, I have also tried this class for getting user control after postback but there is some problem in inetealizing controle events that are on user controls Like button events. Plese give me the solution if u have. Thanks in advance Regards: Manish Srivastava"
- manish.2004
|
|
|
|
 |
|
 |
Hmm ... you're right! Perhaps I'll need to modify the helper class again The best solution is to have the events added from by the calling routine! I.e. you add the control and then add the event.
I'll modify the helper and place the new version.
|
|
|
|
 |
|
 |
Hello,
When are you planning to add the new version?
Thanks
|
|
|
|
 |
|
 |
Hi Wessam,
Marhaba
First of all let me start by thanking you for this post. I created a user control that will be used to execute search queries against an XML datasource and the control works fine. Now I want to dynamically load several instances of that control on the page (via an add/remove button embdedded within the control) but whenver the page is posted back the data is lost, the user control instances are recreated but no data is reloaded knowing that using the concept metioned in ur atricle other tests worked fine.. Could you please send me an email to see how we can resolve this issue knowing that i dont mind sharing the source code.
Thanks,
Mohamad - Lebanon Beirut
|
|
|
|
 |
|
 |
I might be missing something here, but I don't think this implementation is thread safe.
If two people are browsing the same page, they are both sharing the same collection of controls added to a container..(because the collection is static).
I think the collection has to be per user and either stored in the viewstate or session.
Marco M.
|
|
|
|
 |
|
 |
I haven't looked at the code, but yes, A static variable on the page class will be shared amongst ALL instances of the page!
I imagine very weird things will happen if you have two users on the page at the same time!
A perfect example of something that can work well in test (when only one person is using it at a time) and can be a complete train-wreck when you go live.
|
|
|
|
 |
|
 |
You're right! I have changed the code not to use static variables but instances of the Helper class. I have also modified the sample code as well.
Thank you for your remarks.
Wesam
|
|
|
|
 |
|
 |
No problem. I did exactly the same thing once, used a static variable to hack in a last-minute fix. Lucky two testers hit the page at the same time, and one of the testers was diligent enough to not ignore a brief moment of weirdness when their search results were intermingled with someone elses...
|
|
|
|
 |
|
 |
Hi,
Your approach works very well, nice work.
But if I start your code in VS2005 with its internal web server, the number of dynamic controls
is restored from the last run.
any idea?
regards
Christian
|
|
|
|
 |
|
 |
Perhaps this is due to the static variable I use to store the controls. For some reason .Net Framework maintains all data stored as long as you don't change the code (don't re-build the project). I have added a new method Reset() which should be invoked where appropriate. For example, in the demo program you will see that I'd placed a call to this function when there is no PostBack.
Hope this helps
Wesam
|
|
|
|
 |
|
 |
Hi Wesam,
Thanks for your help it works well for me.
regards
Christian
|
|
|
|
 |
|
 |
Will this help from recreating the UCs on a postback? I currently hava 60+ datadriven usercontrols dynamically added to a page from a config table and have to re-add then on postbacks. The selected/entered data is retained, but I have to do a lot of db calls. Thanks
Ryan
|
|
|
|
 |
|
 |
Yes, indeed. I had a similar problem and I had solved it this way. As I mentioned, the basic idea behind all this is to re-create the UserControls alongside their ID's (to make the available to Framework just as they were before the postback).
.Net Framework saves the viewstate of every Server control (including UserControls), so they are not lost! Upon data postback it will try to find those controls again by their ID's: if it finds them, it will reload the value otherwise nothing happens
The helper class I made will take care of all all this - all what you need to do is add the control to helper class when you create it (the helper class) and one line in Page_Init which will re-create all controls already added. Hope this helps!
|
|
|
|
 |