This is a migrated thread and some comments may be shown as answers.

Usercontrols programatically created are being lost on post back

5 Answers 306 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 1
Mike asked on 21 Nov 2013, 02:23 AM
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.

5 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 21 Nov 2013, 03:48 AM
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.
0
Mike
Top achievements
Rank 1
answered on 22 Nov 2013, 12:39 AM
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.
0
Shinu
Top achievements
Rank 2
answered on 22 Nov 2013, 09:59 AM
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.
0
Mike
Top achievements
Rank 1
answered on 22 Nov 2013, 01:40 PM
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?
0
Viktor Tachev
Telerik team
answered on 27 Nov 2013, 11:36 AM
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.
Tags
General Discussions
Asked by
Mike
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Mike
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or