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

Dynamically adding docks & persist the statee

3 Answers 77 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Suraj
Top achievements
Rank 1
Suraj asked on 12 Dec 2011, 08:44 AM
Hi,

I have a situation where I need to add usercontrols dynamically & in those usercontrols add RadDocks dynamically which itself contains usercontrol.

Following is the code I am using to add RadDocks in parent usercontrol

in .ascx
<telerik:RadDockLayout runat="server" ID="QuestionsDockLayout">
                   <telerik:RadDockZone ID="RadDockZone1" runat="server"/>
                      </telerik:RadDockLayout>
 
in .ascx.cs
private bool _shouldSavesState = false;
 
      protected void Page_Init(object sender, EventArgs e)
      {
          QuestionsDockLayout.SaveDockLayout += QuestionsDockLayout_SaveDockLayout;       
 
              LoadQuestions();
      }
 
      private void LoadQuestions()
      {
 
          foreach (var zone in QuestionsDockLayout.RegisteredZones)
          {
              zone.Docks.Clear();
              zone.Controls.Clear();
          }
 
          if (!string.IsNullOrEmpty(XmlNode_Page))
          {
              XmlDocument xmldoc_page = new XmlDocument();
 
              xmldoc_page.Load(new StringReader(XmlNode_Page));
 
              
              XmlNodeList nodePages = xmldoc_page.SelectNodes("//Question");
 
              if (nodePages != null && nodePages.Count > 0)
              {
                  for (int i = 0; i < nodePages.Count; i++)
                  {
                      RadDock radDock = new RadDock();
                      radDock.ID = "dockPanel_" + this.ID + "_Q" + i;
                       
                     radDock.AutoPostBack = true;
                       
                      radDock.Resizable = false;
                      radDock.DockMode = DockMode.Docked;                     
                      radDock.DockHandle = DockHandle.None;
                      radDock.OnClientInitialize = "SetHandleDock";
                      QuestionsDockLayout.RegisteredZones[0].Controls.Add(radDock);
 
                      XmlNode XnodeQuestion = nodePages[i];
                      UC_SurveyQuestion SQ = (UC_SurveyQuestion)LoadControl("~/UserControls/UC_SurveyQuestion.ascx");
                      SQ.ID = XnodeQuestion.Attributes["id"].Value;
                      SQ.XmlNode_Question = XnodeQuestion.OuterXml;
                      SQ.Page_ID = this.ID;
 
                      HtmlImage img = SQ.FindControl("Handle") as HtmlImage;
                      img.ClientIDMode = System.Web.UI.ClientIDMode.Static;
                      img.ID = "Handle_" + radDock.ClientID;
 
                      radDock.ContentTemplate = new CompiledTemplateBuilder(parent =>
                      parent.Controls.Add(SQ)
                          );
 
                      radDock.DockPositionChanged += dock_DockPositionChanged;
                      
                       
                  }
              }
             
          
      }
 
    
      private void dock_DockPositionChanged(object sender, DockPositionChangedEventArgs e)
      {
          //if dock position was changed set a flag for saving dock state
          _shouldSavesState = true;
      }
 
      private void QuestionsDockLayout_SaveDockLayout(object sender, DockLayoutEventArgs e)
      {
          //Save dock panels state in user's profile
          if (_shouldSavesState)
          {
              List<DockState> lst = QuestionsDockLayout.GetRegisteredDocksState();
          }
      }


Now the problem is when I change the positions of RadDock Panels changes to state doesn't persist.
In SaveDockLayout event GetRegisteredDockState method returns the same state of docks as when they are added.

I am not using LoadDockLayout event as indices of RadDocks are same as they are added.

I must be making some silly mistake.

Please help me asap.

Regards,
Suraj Patil

3 Answers, 1 is accepted

Sort by
0
Slav
Telerik team
answered on 14 Dec 2011, 04:10 PM
Hello Suraj,

I would suggest checking the article Dynamically Creating RadDock Controls, which explains in detail the steps that should be executed in order to persist the layout of dynamically created RadDocks.

There are several suggestions that  will help you resolve the encountered problem:

1. On every page load the current list of dock states is collected from the method of RadDockLayout GetRegisteredDocksState and it is saved in a storage medium of your choosing, so that it can be used for restoring the docks layout. I noticed that you have obtained the state data, so please make sure that it is successfully saved and can be accessed on the next page load.

2. The dock controls state that you saved in the previous step, should be used to recreate the RadDocks on the Init event of the page. Also, the state of every dock should be applied by passing it to the ApplyState method of RadDock and every dock should be added in the Controls collection of the RadDockLayout.

3. It is necessary to add the RadDockLayout event handler in order to restore the RadDockZones and the Indices of every RadDock from the saved layout, as these settings are changed every time a dock control is moved to a new position. If this step is skipped, the RadDocks will return to their original positioning, as in your description.

In the linked article you can find information and sample code for implementing the needed steps.

I hope the provided information will help your resolve the issue at hand. Feel free to contact us again if your run into more difficulties.

Best wishes,
Slav
the Telerik team
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 their blog feed now
0
Suraj
Top achievements
Rank 1
answered on 16 Dec 2011, 11:42 AM
Hello Slav,

Thanks for your assistance.

As earlier said I am adding usercontrols to page dynamically & in those usercontrols adding RadDocks dynamically.The problem I was getting was because I was adding RadDocks in PageInit event but it's parent usercontrol in PageLoad event.  :)

Now I am getting correct states of RadDocks.But I am facing another problem now.

When  user changes the order of RadDocks I have to reload all the usercontrol inside the RadDocks for that I am clearing all the docks inside the DockLayout
& adding it again but I am getting following  error.

Script controls may not be registered after PreRender.

Regards,
Suraj.
0
Slav
Telerik team
answered on 20 Dec 2011, 05:50 PM
Hi Suraj,

Please check the schema in the help article RadDock Lifecycle for more information on the proper moments of the page lifecycle that should be used when creating RadDock controls dynamically. If the provided steps aren't followed, you will most probably run into unexpected behavior.

Note that at the current state of affairs I am mostly guessing as to what your setup is. If you are still experiencing difficulties please open a regular support ticket and send us a simple, runnable project that isolates your issue so that we can examine it locally and provide a working solution.

All the best,
Slav
the Telerik team
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 their blog feed now
Tags
Dock
Asked by
Suraj
Top achievements
Rank 1
Answers by
Slav
Telerik team
Suraj
Top achievements
Rank 1
Share this question
or