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?
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