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

Usercontrol on dynamically created dock return Null Reference Exception

1 Answer 303 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Indicia
Top achievements
Rank 1
Indicia asked on 23 Sep 2013, 11:11 AM

Hello,

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 (http://www.telerik.com/help/aspnet-ajax/dock-widgets.html) 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:RadAjaxLoadingPanel>
 
    <telerik:RadAjaxPanel runat="server" ID="rapConfiguration" LoadingPanelID="ralpConfiguration">
          <telerik:RadWindow ID="modalPopup" runat="server" Width="360px" Height="360px" Modal="true">
               <ContentTemplate>
                    <asp:DropDownList runat="server" ID="ddlControls" ></asp:DropDownList>
                    <asp:Button runat="server" ID="btnAddRadDock" Text="Add" />
               </ContentTemplate>
          </telerik:RadWindow>
     </telerik:RadAjaxPanel>
 
    <telerik:RadDockLayout ID="dockLayout" runat="server">
    </telerik:RadDockLayout>
</asp:Content>

protected override void OnPreInit(EventArgs e)
{
  base.OnPreInit(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);
    dock.ApplyState(CurrentDockStates[i]);
    dockLayout.Controls.Add(dock);
  }
}
 
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;
  LoadWidget(dock);
// 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
  dockLayout.Controls.Add(dock);
}
 
private void LoadWidget(RadDock dock)
{
  if (string.IsNullOrEmpty(dock.Tag))
    return;
 
  GisMapControl gisMapControl = new GisMapControl();
  gisMapControl.IsFilterPanelVisible = false;
  gisMapControl.LocationLimit = 100;
  gisMapControl.FacilityLimit = 100;
 
  dock.ContentContainer.Controls.Add(gisMapControl);
}
 
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;
}

1 Answer, 1 is accepted

Sort by
0
Slav
Telerik team
answered on 26 Sep 2013, 03:08 PM
Hi,

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.

Regards,
Slav
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
Dock
Asked by
Indicia
Top achievements
Rank 1
Answers by
Slav
Telerik team
Share this question
or