Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Maintaining dynamic controls

Status
Not open for further replies.

ma77c

Programmer
Jan 27, 2006
28
CA
I'm trying to maintain user controls that are added dynamically when the user selects a menu item. To do this, I've created an ArrayList and I add the appropriate usercontrol depending on the menu item selected. I then call a loadControls() method on page load to add the controls to a placeholder. However, I am left with only the most recently added control.

Can anyone see what I'm doing wrong or missing?

Code:
Private Shared entryItemsArray As New ArrayList()
Private Shared itemCount As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    loadControls()
End Sub

Protected Sub loadControls()
    For Each obj As Object In entryItemsArray
        If (obj.GetType().FullName = "ASP.Paragraph_ascx") Then
            Dim p As ASP.Paragraph_ascx = CType(obj, ASP.Paragraph_ascx)
            Dim id As String = p.ID
            Dim txt As String = p.Text
            phEntryItems.Controls.Add(p)
        ElseIf (obj.GetType().FullName = "ASP.List_ascx") Then
            Dim l As ASP.List_ascx = CType(obj, ASP.List_ascx)
            phEntryItems.Controls.Add(l)
        End If
    Next
End Sub

Protected Sub menuEntryItems_MenuItemClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.MenuEventArgs) Handles menuEntryItems.MenuItemClick
    Select Case menuEntryItems.SelectedValue
        Case "addPara"
            Dim p As ASP.Paragraph_ascx
            p = CType(LoadControl("~/Paragraph.ascx"), ASP.Paragraph_ascx)
            p.ID = "p" & itemCount.ToString
            p.Text = "p" & itemCount.ToString 'for testing purposes
            entryItemsArray.Add(p)
            phEntryItems.Controls.Add(p)
            itemCount += 1
        Case "addList"
            Dim l As ASP.List_ascx
            l = CType(LoadControl("~/List.ascx"), ASP.List_ascx)
            l.ID = "l" & (itemCount + 1).ToString
            entryItemsArray.Add(l)
            phEntryItems.Controls.Add(l)
            itemCount += 1
    End Select
End Sub
 
The controls collection of the placeholder, phEntryItems, contains all of the user controls, but it only displays the control that was added last.

Can anyone explain this behaviour?
 
Is my description not good enough or am I simply beyond help.
 
Place the call to loadcontrols() in the Page_Init event, insted of page_load
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top