Search code examples
c#asp.netvb.netasp.net-ajaxupdatepanel

Update panel not adding new controls but shows the new ones


Well when working with a simple example it works but when I load a control from other panel and build and then add it to update panel it doesn't update the controls of it. The example here is working :

<asp:UpdatePanel ID="updatePanelRepeater" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
    <ContentTemplate>
        <asp:Panel ID="allAccResults" runat="server">
        </asp:Panel>
        <asp:Label ID="Label21" runat="server" Text="Label"></asp:Label>
        <asp:ListBox ID="lbLog" runat="server"></asp:ListBox>
        <asp:Button ID="loadMoreAccResultsButton" CssClass="loadMoreAccResultsButton" runat="server" Text="load More" OnClick="loadMoreAccResultsOnPanel" />
    </ContentTemplate>
</asp:UpdatePanel>

When async post back happends it triggers below the function and it works fine by adding controls

        lbLog.Items.Add(Guid.NewGuid().ToString())
        allAccResults.Controls.Add(lbLog)

When I load a control and build it and then add it to panel it only adds the new ones not every new one control to the previous added so it ends up adding only new controls every time while I need all controls from beggining to end

        For icount As Integer = TenResultsDataStart To TenResultsDataEnd
            m_rbNum = m_rbNum + m_resPerPage
            m_accsingleresult = New AccomodationSingleResult
            m_accsingleresult = LoadControl("AccomodationSingleResult.ascx")
            m_accsingleresult.BuiltControl(m_accResults(icount), m_rbNum)
            allAccResults.Controls.Add(m_accsingleresult)
        Next

Also when debugging allAccResults.Controls.Count is not increasing from 10 to 20 to 30 it just clears it every time without any line code doing Clear of controls and keeps doing it 10 then again from 0 10 and again and again


Solution

  • Ok I have found out how it can be done by adding a new panel every time and the new controls in each new panel. So I just add a counter in page load in a session and if it's async postback then it will increment.

       Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (Page.IsPostBack) Then
            Session.Item("IndexOfAll") = 0
            loadMoreAccResultsOnPanel()
        End If
        If (ScriptManager.GetCurrent(Page).IsInAsyncPostBack) Then
            Session.Item("IndexOfAll") += 1
        End If
    End Sub
    

    And the function that is triggered from button with async postback

    Protected Sub loadMoreAccResultsOnPanel()
    
        For i As Integer = 0 To Session.Item("IndexOfAll")
            Dim pnl As Panel = New Panel()
            pnl.ID = "PAnel" & i.ToString()
            Dim m_accsingleresult As WebUserControl1
            m_accsingleresult = New WebUserControl1
            m_accsingleresult = LoadControl("WebUserControl1.ascx")
            m_accsingleresult.ID = "m_accsingleresult" & i.ToString()
            m_accsingleresult.builtcontrol()
            allAccResults.Controls.Add(pnl)
            pnl.Controls.Add(m_accsingleresult)
        Next
    End Sub
    

    As for the WebUserControl1

    Public Sub builtcontrol()
        lbLog.ID = Guid.NewGuid().ToString()
        For i As Integer = 1 To 10
            lbLog.Items.Add(i.ToString() + "___" + Guid.NewGuid().ToString())
        Next
    End Sub