I have a asp.net web page with place holder control and Menu control when user select an item from the menu. It will dynamically load the control based on menu item’s value.
I got the control loaded but if i click on the a link button or anything on the web user control (.ascx) The web user control (.ascx) will disappear. I do not know what is causing this. Can someone take a look at my code and see what i’m missing?
Protected Sub Menu1_Click(ByVal sender As Object, ByVal e As EventArgs) Select Case Me.Menu1.SelectedValue Case 'CustMasterMain' Dim ccCustMasterMaint As UserControl = CType(Page.LoadControl('~/Controls/Franchise/CustMasterMaintControl.ascx'), UserControl) Me.phHolder1.Controls.Add(ccCustMasterMaint) Case 'AcctRecInq' Dim ccAcctRecInq As UserControl = CType(Page.LoadControl('~/Controls/Franchise/custAccountsReceivableInquiry.ascx'), UserControl) Me.phHolder1.Controls.Add(ccAcctRecInq) End Select End Sub
Remember that each time you do a postback you are working with a brand new instance of your page class. If you added the control to the controls collection of a previous instance of your page class, you need to add it again for every postback that follows.
Additionally, if you want ViewState to be restored for the control then it needs to be added to the page before the Load event. That means adding the control during Init or PreInit.