Dynamic UserControl - Postback Event not Firing.
I have a page where I add a dynamic user control in the Page_Load event based on criteria.
The control is a simple form with a save button. The problem is the _Click event never fires for the button. It will postback the main page the control is within, but not the code behind for the control...so I never get the button click even, and therefor can't execute the save code.
Tried it in the init, tried adding the control to a placeholder, tried a lot of various things but nothing seems to work. Any help is greatly appreciated. Just want it to fuction the same as if it wasn't added dynamically. Thanks.
Code:
If cb_business_profile.Items.Count = 0 Then
Dim u_pnl_content As UpdatePanel = Page.FindControl("u_pnl_content")
u_pnl_content.ContentTemplateContainer.Controls.Clear()
Dim business_profile_detail As Control = LoadControl("~/controls/business_profile_detail.ascx")
business_profile_detail.ID = "business_profile_detail"
u_pnl_content.ContentTemplateContainer.Controls.Add(business_profile_detail)
u_pnl_content.Update()
Else
load_dashboard()
End If
Re: Dynamic UserControl - Postback Event not Firing.
Hello,
The problem here is likely due to the fact that you are not "re-adding" the User Control to the page when the PostBack happens. And instead, you are re-creating the User Control each time the page loads.
I would strongly recommend that you take a look at this article:
http://www.4guysfromrolla.com/articles/092904-1.aspx
Gary
Re: Dynamic UserControl - Postback Event not Firing.
That article just discusses adding controls individually - not an entire form from an .ascx file. I wish that my problem was I wasn't re-adding it at postback, because that would mean that I was at least getting the form to post back. My problem is the button never clicks to post the user control code behind anyway. The other form posts...but since it's in an update planel I don't lose the control or anything like that.
I believe it has somethign to do w/ needing to add a delegate for the button (within ascx file), manually adding the event handler, etc...don't believe I can just use the built in "Handles".
Re: Dynamic UserControl - Postback Event not Firing.
Hello,
The principle of the ASP.Net Page Life Cycle is the same regardless of whether you are dynamically adding a User Control, or whether you are adding a Server Control.
Is it possible that you can upload the application that you are using, and I will take a look?
Gary