tags:

views:

1319

answers:

3

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
+9  A: 

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.

Joel Coehoorn
+2  A: 

Joel is correct that you need to add the control on every postback so you need a way to track what controls are stored on the page.

The Dynamic Controls Placeholder is an excellent control for helping manage dynamic controls on your page across postbacks. I'd recommend you check it out as I've used it on a number of projects and it's made life a lot easier for me.

lomaxx
A: 

what about in a update panel?