RadAjax for ASP.NET

Load user controls Send comments on this topic.
See Also
How-to > Load user controls

Glossary Item Box

The dynamic loading of user controls follows the same logic as in normal postback application with small specifics regarding the usage of either of the Telerik RadAjax controls. The only difference between the standard loading of controls and AJAX loading is that you should add the control in an asp:Panel instead of a Placeholder. This is due to the fact that the Placeholder does not render any HTML output and therefore cannot be updated with our ajax control. You can load custom user controls via AJAX requests by following the steps below:

  1. You need to have an appropriate container for it - usually that will be a specially designated asp:panel.
  2. Instantiate your user control in Page_Init or Page_Load and always recreate the last loaded User Control:
    VB.NET Copy Code
    Dim MyControl As UserControl = Me.LoadControl(controlName)
    Me.LoadMyUserControl( Me.CurrentControl, Panel1)

    C# Copy Code
    UserControl MyControl = (UserControl)LoadControl(controlName);
    LoadMyUserControl(CurrentControl, Panel1);
     
  3. Make sure you assign unique ID to the dynamically loaded User Control:

    VB.NET Copy Code
    Dim userControlID As String = controlName.Split("."c)(0)
    MyControl.ID = userControlID.Replace("/", "").Replace("~", "")

    C# Copy Code
    string userControlID = controlName.Split('.')[0];
    MyControl.ID = userControlID.Replace(
    "/", "").Replace("~", "");
  4. Place the instance inside the controls collection of the container:

    VB.NET Copy Code
    parent.Controls.Add(MyControl)

    C# Copy Code
    parent.Controls.Add(MyControl);
  5. Here is the code from a sample project:

    ASPX/ASCX Copy Code
       <div>
          
    <asp:Button ID="Button1" runat="server" Text="Load WebUserControl1.ascx"  />
          
    <asp:Button ID="Button2" runat="server" Text="Load WebUserControl2.ascx" />
          
    <br />
          
    <asp:Panel ID="Panel1" runat="server"></asp:Panel>
          
    <rad:RadAjaxManager ID="RadAjaxManager1" runat="server">
              
    <AjaxSettings>
                  
    <rad:AjaxSetting AjaxControlID="Button1">
                      
    <UpdatedControls>
                           
    <rad:AjaxUpdatedControl ControlID="Panel1" />
                      
    </UpdatedControls>
                  
    </rad:AjaxSetting>
                  
    <rad:AjaxSetting AjaxControlID="Button2">
                      
    <UpdatedControls>
                           
    <rad:AjaxUpdatedControl ControlID="Panel1" />
                      
    </UpdatedControls>
                  
    </rad:AjaxSetting>
              
    </AjaxSettings>
          
    </rad:RadAjaxManager>
       
    </div>

    And in the code-behind:

    VB.NET Copy Code
        Private Property LatestLoadedControlName() As String
            Get
                Return CStr(ViewState("LatestLoadedControlName"))
            End Get
            Set(ByVal value As String)
                ViewState("LatestLoadedControlName") = value
            End Set
        End Property
        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If Not (LatestLoadedControlName Is Nothing) Then
                LoadUserControl(LatestLoadedControlName)
            Else
                LoadUserControl("WebUserControl1.ascx")
            End If
        End Sub
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            LoadUserControl("WebUserControl1.ascx")
        End Sub
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            LoadUserControl("WebUserControl2.ascx")
        End Sub
        Public Sub LoadUserControl(ByVal controlName As String)
            If Not (LatestLoadedControlName Is Nothing) Then
                Dim previousControl As Control = Panel1.FindControl(LatestLoadedControlName.Split("."c)(0))
                If Not (previousControl Is Nothing) Then
                    Me.Panel1.Controls.Remove(previousControl)
                End If
            End If
            Dim userControlID As String = controlName.Split("."c)(0)
            Dim targetControl As Control = Panel1.FindControl(userControlID)
            If targetControl Is Nothing Then
                Dim userControl As UserControl = CType(Me.LoadControl(controlName), UserControl)
                'slashes and tildes are forbidden
                userControl.ID = userControlID.Replace("/", "").Replace("~", "")
                Me.Panel1.Controls.Add(userControl)
                LatestLoadedControlName = controlName
            End If
        End Sub

     

    C# Copy Code
       private void Page_Load(object sender, System.EventArgs e)
       {
           
    if (LatestLoadedControlName != null)
           {
               LoadUserControl(LatestLoadedControlName);
           }
           
    else
           {
               
    LoadUserControl("WebUserControl2.ascx");
           }
       }
       
    protected void Button1_Click(object sender, EventArgs e)
       {
           LoadUserControl(
    "WebUserControl1.ascx");
       }
       
    protected void Button2_Click(object sender, EventArgs e)
       {
           LoadUserControl(
    "WebUserControl2.ascx");
       }

       
    public void LoadUserControl(string controlName)
       {
           
    if (LatestLoadedControlName != null)
           {
               Control previousControl = Panel1.FindControl(LatestLoadedControlName.Split(
    '.')[0]);
               
    if (previousControl != null)
               {
                   
    this.Panel1.Controls.Remove(previousControl);
               }
           }
           
    string userControlID = controlName.Split('.')[0];
           Control targetControl = Panel1.FindControl(userControlID);
           
    if (targetControl == null)
           {
               UserControl userControl = (UserControl)
    this.LoadControl(controlName);
               
    //slashes and tildes are forbidden
               
    userControl.ID = userControlID.Replace("/", "").Replace("~", "");
               
    this.Panel1.Controls.Add(userControl);
               LatestLoadedControlName = controlName;
           }
       }
       
    private string LatestLoadedControlName
       {
           get
           {
               
    return (string)ViewState["LatestLoadedControlName"];
           }
           set
           {
               ViewState[
    "LatestLoadedControlName"] = value;
           }
       }

     

For full code working application you can check this example in Telerik RadAjax Quick Start Framework:

http://www.telerik.com/demos/aspnet/Ajax/Examples/Common/LoadingUserControls/DefaultCS.aspx

See Also