Usercontrol on dynamically created dock return Null Reference Exception

2 posts, 0 answers
  1. Indicia
    Indicia avatar
    15 posts
    Member since:
    Mar 2012

    Posted 23 Sep 2013 Link to this post


    I'm creating a 'dashboard' on which the user can dynamically add dock controls. Each dock control contains a certain usercontrol.

    Using the examples from the Telerik site, I already have created the dashboard and the functionality to add dock controls. But when I add a usercontrol, I get a null reference exception form the usercontrol. The usercontrol itself contains several buttons, which are null when I call dockLayout.Controls.Add(dock);
    I'm not sure, but I think it has to do with the state of the page lifecycle.

    The dock-widget example ( I've using is probably the way to solve this problem, but the example doesn't explain how LoadControl() should be implemented. Is this the missing link? If so, can you give an example code, based on the provided example?

    <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
        <telerik:RadAjaxLoadingPanel runat="server" ID="ralpConfiguration">
        <telerik:RadAjaxPanel runat="server" ID="rapConfiguration" LoadingPanelID="ralpConfiguration">
              <telerik:RadWindow ID="modalPopup" runat="server" Width="360px" Height="360px" Modal="true">
                        <asp:DropDownList runat="server" ID="ddlControls" ></asp:DropDownList>
                        <asp:Button runat="server" ID="btnAddRadDock" Text="Add" />
        <telerik:RadDockLayout ID="dockLayout" runat="server">

    protected override void OnPreInit(EventArgs e)
      btnAddControl.Click += new EventHandler(btnAddControl_Click);
      btnAddRadDock.Click += new EventHandler(btnAddRadDock_Click);
      dockLayout.SaveDockLayout += new DockLayoutEventHandler(dockLayout_SaveDockLayout);
      dockLayout.LoadDockLayout += new DockLayoutEventHandler(dockLayout_LoadDockLayout);
      for (int i = 0; i < CurrentDockStates.Count; i++)
        RadDock dock = new RadDock();
        dock.ID = string.Format("RadDock{0}", i);
    protected void dockLayout_SaveDockLayout(object sender, DockLayoutEventArgs e)
      CurrentDockStates = dockLayout.GetRegisteredDocksState();
    protected void dockLayout_LoadDockLayout(object sender, DockLayoutEventArgs e)
      foreach (DockState state in CurrentDockStates)
        e.Positions[state.UniqueName] = state.DockZoneID;
        e.Indices[state.UniqueName] = state.Index;
    private void btnAddRadDock_Click(object sender, EventArgs e)
      RadDock dock = CreateRadDock();
      dock.Tag = ddlControls.SelectedValue;
    // the following line will trigger the page load of the user control, which in turn returns the null ref exception on a button within the user control
    private void LoadWidget(RadDock dock)
      if (string.IsNullOrEmpty(dock.Tag))
      GisMapControl gisMapControl = new GisMapControl();
      gisMapControl.IsFilterPanelVisible = false;
      gisMapControl.LocationLimit = 100;
      gisMapControl.FacilityLimit = 100;
    private RadDock CreateRadDock()
      int docksCount = CurrentDockStates.Count;
      RadDock dock = new RadDock();
      dock.ID = string.Format("RadDock{0}", docksCount);
      dock.Title = string.Format("Dock {0}", docksCount);
      dock.Text = string.Format("Added at {0}", DateTime.Now);
      dock.UniqueName = Guid.NewGuid().ToString();
      dock.Width = Unit.Pixel(300);
      return dock;
  2. Slav
    Slav avatar
    1359 posts

    Posted 26 Sep 2013 Link to this post


    The problem you are describing could be related to accessing controls too early on the page life cycle. It is expected that a control would not be found on Page_Init as described here thus the null reference exception you have encountered. Please try accessing the button at a later stage, for example on Page_Load.

    LoadControl is an ASP.NET method and you could find its definition in this MSDN article.

    I would suggest checking the online demo My Portal as a reference for achieving the desired functionality, if you have not done so already.

    In case you are still having difficulties, please send a simple, fully runnable project that isolates the issue so that I can examine it locally and provide a more to the point answer.

    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