2
Answers

Dynamic user controls

hector_busto

hector_busto

19y
2.2k
1
Hello. I have a web C# project with a form with 3 buttons and 3 placeholders, each placeholder contains a Web User Control, when I click over button 1 it shows the data of WUC1, when I click over button 2 it shows the data of WUC2 and when I click over button 3 it shows the data of WUC3. My solution was to make initially invisible the 3 placeholders and when a button is clicked then I made visible the corresponding placeholder, as the following code from button 1 shows: this.PlaceHolder1.Visible = true; this.PlaceHolder2.Visible = false; this.PlaceHolder3.Visible = false; This works well, but I think it's not too eficient to have loaded the 3 WUCs, so I'm trying to make a dynamic load of each WUC depending the button clicked, example of button 1: private void btn1_Click(object sender, System.EventArgs e){ this.PlaceHolder1.Controls.Clear(); this.PlaceHolder2.Controls.Clear(); this.PlaceHolder3.Controls.Clear(); this.PlaceHolder1.Controls.Add(LoadControl("WUC1.ascx")); //Only load the WUC1 } This code load well the WUCs, show the data, but it can't execute the events of the WUCs, because in each WUC I have buttons that perform some operations and this events don't execute, i put a breakpoint and show that the page_load of the page that contains the WUCs, I think it's a problem of dynamic load of controls, any idea would be appreciated. Thanks a lot.

Answers (2)