N
Nathan Sokalski
I have a page which I dynamically add several usercontrols (*.ascx files) to
using the following code:
Public Sub Refresh()
For Each section As DataRow In Me.GetSections().Rows
CType(Me.FindControl("admin" & CStr(section("buttontext")).Replace(" ",
"")), adminsection2).RefreshSection()
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim sections As DataTable = Me.GetSections()
For Each section As DataRow In sections.Rows
Me.AddAdminSection(CStr(section("buttontext")))
Next
Me.Refresh()
End Sub
Private Function GetSections() As DataTable
Dim sections As New DataTable
Dim dataadapterSelect As New OleDbDataAdapter("SELECT buttontext FROM
subnavigation WHERE buttontext<>'More.' AND category='" &
Request.QueryString("category") & "' ORDER BY buttonorder",
System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
dataadapterSelect.Fill(sections)
Return sections
End Function
Private Sub AddAdminSection(ByVal section As String)
Dim admin As adminsection2 =
CType(Page.LoadControl("~/usercontrols/adminsection2.ascx"), adminsection2)
admin.ID = "admin" & section.Replace(" ", "")
admin.Section = section
Me.form1.Controls.Add(admin)
End Sub
My problem is that the controls are reloaded every time, as you can see from
the Load event. If I place the following line
Me.AddAdminSection(CStr(section("buttontext")))
in an If Not Me.IsPostBack() statement, then it is only loaded the first
time and I recieve an object does not exist error every time the Refresh()
method is called (which is reasonably often, because this is for an
administration page where the user edits DB records). What can I do to
maintain the look of the dynamically added controls (in other words, how can
I avoid replacing them with every postback)? If there is any other code you
need to see that might help, let me know. Thanks.
using the following code:
Public Sub Refresh()
For Each section As DataRow In Me.GetSections().Rows
CType(Me.FindControl("admin" & CStr(section("buttontext")).Replace(" ",
"")), adminsection2).RefreshSection()
Next
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
Dim sections As DataTable = Me.GetSections()
For Each section As DataRow In sections.Rows
Me.AddAdminSection(CStr(section("buttontext")))
Next
Me.Refresh()
End Sub
Private Function GetSections() As DataTable
Dim sections As New DataTable
Dim dataadapterSelect As New OleDbDataAdapter("SELECT buttontext FROM
subnavigation WHERE buttontext<>'More.' AND category='" &
Request.QueryString("category") & "' ORDER BY buttonorder",
System.Configuration.ConfigurationManager.AppSettings("connectionstring"))
dataadapterSelect.Fill(sections)
Return sections
End Function
Private Sub AddAdminSection(ByVal section As String)
Dim admin As adminsection2 =
CType(Page.LoadControl("~/usercontrols/adminsection2.ascx"), adminsection2)
admin.ID = "admin" & section.Replace(" ", "")
admin.Section = section
Me.form1.Controls.Add(admin)
End Sub
My problem is that the controls are reloaded every time, as you can see from
the Load event. If I place the following line
Me.AddAdminSection(CStr(section("buttontext")))
in an If Not Me.IsPostBack() statement, then it is only loaded the first
time and I recieve an object does not exist error every time the Refresh()
method is called (which is reasonably often, because this is for an
administration page where the user edits DB records). What can I do to
maintain the look of the dynamically added controls (in other words, how can
I avoid replacing them with every postback)? If there is any other code you
need to see that might help, let me know. Thanks.