Usercontrols programatically created are being lost on post back

6 posts, 0 answers
  1. Mike
    Mike avatar
    9 posts
    Member since:
    Nov 2013

    Posted 20 Nov 2013 Link to this post

    I have a user control with 2 rad buttons and a rad ajax manager. one ajaxs the other. Because of the user control it is the ajaxmanagerproxy
    the main page has a multipage and I load several of the user controls in codeadding them to the pageview in the multipage programatically. I have a regular ajaxmanager on the page set to ajax the panel. However. When the page loads I see all the controls I am adding and when I click the page reloads and they are gone.

    What is the point of using ajax if a postback still happens. Any assistance would be appreciated.
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Nov 2013 Link to this post

    Hi Mike,

    Please have a look into the sample code I tried to achieve your scenario.

    UserControl Page:
    <%@ Control ClassName="UserControl" Language="C#" AutoEventWireup="true" CodeFile="2RadBtn.ascx.cs" Inherits="GeneralDiscussions_2RadBtn" %>
    <telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadButton1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadButton2" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>
    <telerik:RadButton ID="RadButton1" runat="server" Text="Button1">
    </telerik:RadButton>
    <telerik:RadButton ID="RadButton2" runat="server" Text="Button2">
    </telerik:RadButton>

    ASPX:
    <%@ Reference Control="2RadBtn.ascx" %>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadButton1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="Panel1" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <asp:Panel ID="Panel1" runat="server" Style="border: 1px solid black; height: 200px;
        width: 200px;">
        <telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="0">
            <telerik:RadPageView ID="RadPageView1" runat="server">
            </telerik:RadPageView>
        </telerik:RadMultiPage>
    </asp:Panel>
    <telerik:RadButton ID="RadButton1" runat="server" Text="PanelButton">
    </telerik:RadButton>
    <br />
    <telerik:RadButton ID="RadButton2" runat="server" Text="PostBack Button">
    </telerik:RadButton>

    ASPX Page C# :
    public partial class GeneralDiscussions_MainPage2radbtn : System.Web.UI.Page
    {
        private ASP.UserControl usercontrol;
        protected void Page_Load(object sender, EventArgs e)
        {
            usercontrol = (ASP.UserControl)LoadControl("2RadBtn.ascx");
            RadPageView1.Controls.Add(usercontrol);
        }
    }

    Please let me know if you have any concern.
    Thanks,
    Shinu.
  3. Mike
    Mike avatar
    9 posts
    Member since:
    Nov 2013

    Posted 21 Nov 2013 Link to this post

    I am sorry I dont know what you were attempting on your sample but its a little off from mine. 
    here is my code. Perhaps that will help. This sample exhibits the exact same problem.


    User Control:

    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

    <telerik:RadAjaxManagerProxy ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadButton1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadButton2" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
        
    </telerik:RadAjaxManagerProxy>
    <telerik:RadButton ID="RadButton1" runat="server" Text="Button1">
    </telerik:RadButton>
    <telerik:RadButton ID="RadButton2" runat="server" Text="Button2">
    </telerik:RadButton>





    Aspx Page:
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>

    <body>
        <form id="form1" runat="server">
        <div>
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server"></telerik:RadAjaxManager>
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"></telerik:RadScriptManager>
            <asp:Panel ID="Panel1" runat="server" Style="border: 1px solid black; height: 200px;  width: 200px;">
                        <telerik:RadMultiPage ID="RadMultiPage1" runat="server" SelectedIndex="0">
                            <telerik:RadPageView ID="RadPageView1" runat="server">
                            </telerik:RadPageView>
                        </telerik:RadMultiPage>
            </asp:Panel>


        </div>
        </form>
        

    </body>
    </html>

    Code Behind for aspx page:
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Dim mynewcontrol As New Control
                mynewcontrol = LoadControl("/usercontrols/tester.ascx")
                RadPageView1.Controls.Add(mynewcontrol)

                Dim mynewcontrol2 As New Control
                mynewcontrol2 = LoadControl("/usercontrols/tester.ascx")
                RadPageView1.Controls.Add(mynewcontrol2)
            End If

        End Sub


    -------------------------------------
    As you can see I am only interested in the pages being loaded once on the page load. Then I have ajaxed on the user control using the proxy like I am supposed to but the page cases post back. Please try and you will see.
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 22 Nov 2013 Link to this post

    Hi Mike,

    The dynamically created controls should not persist after a postback. Please note that ASP.NET do not recreate the dynamic controls and you should recreate them. In your code you are trying to ajaxify the controls inside the UserControl it will not ajaxify the UserControl. Please have a look into the following VB code snippet which works fine at my end. 

    VB:
    Protected Sub Page_Load(sender As Object, e As EventArgs)
        Dim mynewcontrol As New Control()
        mynewcontrol = LoadControl("WebUserControl.ascx")
        RadPageView1.Controls.Add(mynewcontrol)
     
        Dim mynewcontrol2 As New Control()
        mynewcontrol2 = LoadControl("2RadBtn.ascx")
        RadPageView1.Controls.Add(mynewcontrol2)
    End Sub

    Thanks,
    Shinu.
  5. Mike
    Mike avatar
    9 posts
    Member since:
    Nov 2013

    Posted 22 Nov 2013 Link to this post

    You said "The dynamic controls should not persist thru postback" YES!! Thats exactly the problem. I am using AJAX to avoid the postback How do I get this to work using AJAX?
  6. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2283 posts

    Posted 27 Nov 2013 Link to this post

    Hi Mike,

    Ajax enables part of the content to be updated, however the page goes through the same life cycle. This means that Page_Load is executed every time a request is made.

    In the provided sample code UserControls are added only on initial page load. When a button is clicked a request is sent to the server, page events are triggered, however IsPostBack is true and the controls are not added to the page. This is expected behavior.

    If you would like to load UserControls dynamically and use Ajax you would find this online demo helpful. It illustrates an approach for adding controls to the page programmatically.

    Also when Ajax-enabling RadMultiPage you should follow the guidelines described in this article.

    Regards,
    Viktor Tachev
    Telerik
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Back to Top