Answered by:
Loading usercontrol in placeholder

Question
-
User-1235801825 posted
Hello All,
I have a masterpage , an .aspx page and many usercontrols.
In .aspx page I have taken a placeholder where I am loading user controls. My problem is that the first user control is loading fine. I am also having a button in that user control and on click of that button I want to load other user control dynamically through code behind in the placeholder.
But what I am finding is its not firing the button event at all. The control goes to masterpage then .aspx page but never comes to this user controls button click event.
sample of my Codes are :
.aspx
protected void Page_Load(object sender, EventArgs e){
if (!Page.IsPostBack){
Control cnt = LoadControl("../Usercontrols/PersonalInfo.ascx");UC_placeHolder.Controls.Add(cnt);
lnkPersonal.Text = "Personal info I";}
}
.ascx
// this button is in the user control. On click of this it should load other user control in placeholder which is in .aspx. But problem is this event never fires.
protected void btnSaveNext_Click(object sender, EventArgs e){
Control cnt = LoadControl("../Usercontrols/Experience.ascx");PlaceHolder plcH = (PlaceHolder)Page.FindControl("UC_placeHolder");plcH.Controls.Add(cnt);
//lnkPersonal.Text = "Personal info A";}
What must be the problem here ?? Any Idea ?
your help will be highly appreciated.
Please let me know your views .
Thanks.
Regards
Thursday, July 10, 2008 1:19 AM
Answers
-
User481221548 posted
Hi there
Aviod (!Page.IsPostBack)" and load your control on every page request. Then it works.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 11, 2008 7:04 AM
All replies
-
User-1235801825 posted
Hello All,
I found that problem is only when I add the user control dynamically. If I hard code the usercontrol into the page then the event fires...
Really eating my head...
Regards
Friday, July 11, 2008 1:27 AM -
User481221548 posted
Hi there
Aviod (!Page.IsPostBack)" and load your control on every page request. Then it works.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, July 11, 2008 7:04 AM -
User-16411453 posted
You have to add a handler somewhere
If you make a control dynamically, you have to a add a handler dynamically (VB Example)
Dim txtTextBox
txtTextBox = New TextBox
addHandler txtTextBox.Click, AddressOf txtTextbox_Click
controls.Add(txtTextBox)
Protected Sub btnSaveNext_Click(object sender, EventArgs e)
End Sub
On a webform with code behind page
Protected Sub btnSaveNext_Click(object sender, EventArgs e) handles btnSaveNext_Click
End Sub
Friday, July 11, 2008 2:32 PM