Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > Docking > Saving Dynamically Created Dock Problem
RadControls for ASP.NET are no longer supported (see this page for reference). In case you have inquiries about the Telerik ASP.NET AJAX controls, post them in the pertinent ASP.NET AJAX forums.

Not answered Saving Dynamically Created Dock Problem

Feed from this thread
  • David Moore avatar

    Posted on Jan 28, 2010 (permalink)

    I have a list of web parts thats users can add to their raddocklayout, It adds it fine, but when the page is reloaded, the dock is there, but it doesn't save the position state.  It's fine right after you add it.  I am serializing the data and storing it in the database.  Is there a way to programmatically call the save method?

    foreach (ListItem li in cbxWebParts.Items) 
                { 
                    if (li.Selected) 
                    { 
                        RadDock dock = CreateRadDock(li.Text); 
                        UpdatePanel1.ContentTemplateContainer.Controls.Add(dock); 
     
                        ScriptManager.RegisterStartupScript( 
                        dock, 
                        this.GetType(), 
                        "AddDock" + dock.ClientID, 
                        string.Format(@"function _addDock() {{
        Sys.Application.remove_load(_addDock);
        $find('{1}').dock($find('{0}'));
        $find('{0}').doPostBack('DockPositionChanged');
    }};
    Sys.Application.add_load(_addDock);", dock.ClientID, chooseZone()), 
                        true); 
     
                        CreateSaveStateTrigger(dock); 
                        //Load the selected widget in the RadDock control 
                        dock.Tag = li.Value; 
                        //dock.Title = DroptDownWidget.SelectedItem.Text; 
                        LoadWidget(dock); 
                    } 
                } 

  • Pero Pero admin's avatar

    Posted on Feb 2, 2010 (permalink)

    Hi David,

    Everything seems OK with the source code that you have sent. Please check that you are creating the docks and applying their state in the right way:

    • The docks should be recreated in the OnInit server-side event of the page, before the LoadDockLayout event is fired.
    • You need to handle the LoadDockLayout event and populate the event args with the state information. The RadDockLayout control will automatically move the docks according to the information.
    • Make sure that you call the dock.ApplyState(DockState) method so that the state is actually applied

    If you think that you have the right setup, please send us a running project that reproduces the problem and we will do our best to provide a working solution.


    Sincerely yours,
    Pero
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.

  • David Moore avatar

    Posted on Feb 2, 2010 (permalink)

    That makes perfect sense, it appears that it's not setting my dockstates before the init event so it's not saving it correctly.  Here is the entirety of my code, i don't fully understand the lifecycle of the dock events so I'm having a tough time here. 

    The method that needs to save the state when it's complete is: ButtonAddDock_Click

    Thanks for the help.

    string cacheKey = "UserPreferences.PortalSession1." + HttpContext.Current.User.Identity.Name.ToString(); 
     
     
        private List<DockState> CurrentDockStates 
            { 
                get 
                { 
     
                    List<DockState> _currentDockStates = (List<DockState>)Cache[cacheKey]; 
                    if (Object.Equals(_currentDockStates, null)) 
                    { 
                        //Make an empty configuration list   
                        _currentDockStates = new List<DockState>(); 
     
                        //string strLoadedConfig = GetUserPreference("PortalConfiguration"); 
     
                        using (insidefbDataContext db = new insidefbDataContext()) 
                        { 
                            var dashboardItems = (from x in db.aspnet_Users 
                                                  where x.aspnet_Application.ApplicationName == "/Insidefb" 
                                                  select x).SingleOrDefault(c => c.UserName.Equals(User.Identity.Name.ToString())).dashboardItems; 
     
                            //If we found nothing, leave the current state empty   
                            if (!string.IsNullOrEmpty(dashboardItems)) 
                            { 
                                string strLoadedConfig = dashboardItems.ToString(); 
                                try 
                                { 
                                    //Use the stored config   
                                    //Convert the stored config to an array, splitting at the *s   
                                    string[] aConfig = strLoadedConfig.Split('*'); 
                                    DockState dsWorking = default(DockState); 
                                    //Parse through the array, turning the dockstate strings into dockstates   
                                    //Then add the dockstates to the dockstate list   
                                    foreach (var strConfigItem in aConfig) 
                                    { 
                                        //Start a fresh dockstate   
                                        dsWorking = new DockState(); 
                                        //Parse the string back into dock settings   
                                        dsWorking = DockState.Deserialize(strConfigItem); 
                                        //Add the settings back into the current session   
                                        _currentDockStates.Add(dsWorking); 
                                    } 
                                } 
                                catch 
                                { 
                                } 
     
                                Cache[cacheKey] = _currentDockStates; 
                            } 
                        } 
     
                    } 
                    return _currentDockStates; 
                } 
                set 
                { 
                    Cache[cacheKey] = value; 
     
                    System.Text.StringBuilder sbListConverter = new System.Text.StringBuilder(); 
                    foreach (DockState dockState in value) 
                    { 
                        sbListConverter.AppendFormat("{0}*", dockState.ToString()); 
                    } 
                    string strDockConfig = sbListConverter.ToString().TrimEnd(Convert.ToChar("*")); 
                    using (insidefbDataContext db = new insidefbDataContext()) 
                    { 
     
                        aspnet_User activeUser = (from x in db.aspnet_Users 
                                                  where x.aspnet_Application.ApplicationName == "/Insidefb" 
                                                  select x).SingleOrDefault(c => c.UserName.Equals(User.Identity.Name.ToString())); 
     
                        activeUser.dashboardItems = strDockConfig; 
                        db.SubmitChanges(); 
                    } 
                } 
            } 
     
            protected void Page_Init(object sender, EventArgs e) 
            { 
                //Recreate the docks in order to ensure their proper operation 
                for (int i = 0; i < CurrentDockStates.Count; i++) 
                { 
                    if (CurrentDockStates[i].Closed == false
                    { 
                        RadDock dock = CreateRadDockFromState(CurrentDockStates[i]); 
                        RadDockLayout1.Controls.Add(dock); 
                        CreateSaveStateTrigger(dock); 
                        LoadWidget(dock); 
                    } 
                } 
            } 
            protected void Page_Load(object sender, EventArgs e) 
            { 
                if (!IsPostBack) 
                { 
                    LoadControlsCheckbox(); 
                } 
            } 
     
            protected void LoadControlsCheckbox() 
            { 
     
                IFBF.AspNet.Membership.IFBFMembership xEntity = new IFBF.AspNet.Membership.IFBFMembership(); 
                int entityid = xEntity.getEntityid(HttpContext.Current.User.Identity.Name); 
     
                using (insidefbDataContext db = new insidefbDataContext()) 
                { 
     
                    countysecurity userCounty = (from z in db.countysecurities 
                                                 where z.entityid.Equals(entityid) 
                                                 select z).FirstOrDefault<countysecurity>(); 
                    //if (userCounty != null) 
                    if (true
                    { 
                        cbxWebParts.Items.Clear(); 
                        ListItem itAlerts = new ListItem(); 
                        itAlerts.Text = "Alerts"
                        itAlerts.Value = "~/admin/dashboard/Alerts.ascx"
                        cbxWebParts.Items.Add(itAlerts); 
                        ListItem itTask = new ListItem(); 
                        itTask.Text = "Tasks"
                        itTask.Value = "~/admin/dashboard/Task.ascx"
                        cbxWebParts.Items.Add(itTask); 
                    } 
                    if (User.IsInRole("MembershipAdmin") || User.IsInRole("membershipreadonly")) 
                    { 
                        ListItem itReports = new ListItem(); 
                        itReports.Text = "Reports"
                        itReports.Value = "~/admin/dashboard/Reports.ascx"
                        cbxWebParts.Items.Add(itReports); 
                    } 
                    if (User.IsInRole("webAdmin")) 
                    { 
                        ListItem itWebAdmin = new ListItem(); 
                        itWebAdmin.Text = "Web Admin"
                        itWebAdmin.Value = "~/admin/dashboard/webadmin.ascx"
                        cbxWebParts.Items.Add(itWebAdmin); 
                    } 
                    if (User.IsInRole("employee")) 
                    { 
                        ListItem employee = new ListItem(); 
                        employee.Text = "Employee"
                        employee.Value = "~/admin/dashboard/Employee.ascx"
                        cbxWebParts.Items.Add(employee); 
                    } 
                } 
     
                //ListItem itQuotes = new ListItem(); 
                //itQuotes.Text = "Quotes"; 
                //itQuotes.Value = "~/admin/dashboard/Quotes.ascx"; 
                //cbxWebParts.Items.Add(itQuotes); 
                //ListItem itWeather = new ListItem(); 
                //itWeather.Text = "Weather"; 
                //itWeather.Value = "~/admin/dashboard/Weather.ascx"; 
                //cbxWebParts.Items.Add(itWeather); 
            } 
     
            protected void RadDockLayout1_LoadDockLayout(object sender, DockLayoutEventArgs e) 
            { 
                //Populate the event args with the state information. The RadDockLayout control 
                // will automatically move the docks according that information. 
                foreach (DockState state in CurrentDockStates) 
                { 
                    e.Positions[state.UniqueName] = state.DockZoneID; 
                    e.Indices[state.UniqueName] = state.Index; 
                } 
            } 
     
            protected void RadDockLayout1_SaveDockLayout(object sender, DockLayoutEventArgs e) 
            { 
                //Save the dock state in the session. This will enable us 
                // to recreate the dock in the next Page_Init. 
                CurrentDockStates = RadDockLayout1.GetRegisteredDocksState(); 
            } 
     
            private RadDock CreateRadDockFromState(DockState Dstate) 
            { 
                RadDock dock = new RadDock(); 
                dock.ID = string.Format("RadDock{0}", Dstate.UniqueName); 
                dock.DockMode = DockMode.Docked; 
                dock.ApplyState(Dstate); 
                dock.EnableAnimation = true
                dock.Command += new DockCommandEventHandler(dock_Command); 
                dock.Commands.Add(new DockCloseCommand()); 
                dock.Commands.Add(new DockExpandCollapseCommand()); 
     
                return dock; 
            } 
     
            private RadDock CreateRadDock(string title) 
            { 
                int docksCount = CurrentDockStates.Count; 
     
                RadDock dock = new RadDock(); 
                dock.UniqueName = Guid.NewGuid().ToString().Replace('-', 'a');  
                dock.ID = string.Format("RadDock{0}", dock.UniqueName); 
                dock.DockMode = DockMode.Docked; 
                dock.EnableAnimation = true
                dock.Title = title; 
                dock.Text = string.Format("Added at {0}", DateTime.Now); 
                dock.Width = Unit.Pixel(300); 
                 
                dock.Command += new DockCommandEventHandler(dock_Command); 
                dock.Commands.Add(new DockCloseCommand()); 
                dock.Commands.Add(new DockExpandCollapseCommand()); 
     
                return dock; 
            } 
     
            void dock_Command(object sender, DockCommandEventArgs e) 
            { 
                if (e.Command.Name == "Close"
                { 
                    ScriptManager.RegisterStartupScript( 
                    UpdatePanel1, 
                    this.GetType(), 
                    "RemoveDock"
                    string.Format(@"function _removeDock() {{
        Sys.Application.remove_load(_removeDock);
        $find('{0}').undock();
        $get('{1}').appendChild($get('{0}'));
        $find('{0}').doPostBack('DockPositionChanged');
    }};
    Sys.Application.add_load(_removeDock);", ((RadDock)sender).ClientID, UpdatePanel1.ClientID), 
                    true); 
     
                } 
            } 
     
            private void CreateSaveStateTrigger(RadDock dock) 
            { 
                //Ensure that the RadDock control will initiate postback 
                // when its position changes on the client or any of the commands is clicked. 
                //Using the trigger we will "ajaxify" that postback. 
                dock.AutoPostBack = true
                dock.CommandsAutoPostBack = true
     
                AsyncPostBackTrigger saveStateTrigger = new AsyncPostBackTrigger(); 
                saveStateTrigger.ControlID = dock.ID; 
                saveStateTrigger.EventName = "DockPositionChanged"
                UpdatePanel1.Triggers.Add(saveStateTrigger); 
     
                saveStateTrigger = new AsyncPostBackTrigger(); 
                saveStateTrigger.ControlID = dock.ID; 
                saveStateTrigger.EventName = "command"
                UpdatePanel1.Triggers.Add(saveStateTrigger); 
            } 
     
            private void LoadWidget(RadDock dock) 
            { 
                if (string.IsNullOrEmpty(dock.Tag)) 
                { 
                    return
                } 
                try 
                { 
                    Control widget = LoadControl(dock.Tag); 
                    dock.ContentContainer.Controls.Add(widget); 
                } 
                catch 
                { 
                } 
            } 
     
     
            protected void ButtonAddDock_Click(object sender, EventArgs e) 
            { 
                foreach (ListItem li in cbxWebParts.Items) 
                { 
                    if (li.Selected) 
                    { 
                        RadDock dock = CreateRadDock(li.Text); 
                        UpdatePanel1.ContentTemplateContainer.Controls.Add(dock); 
     
                        ScriptManager.RegisterStartupScript( 
                        dock, 
                        this.GetType(), 
                        "AddDock" + dock.ClientID, 
                        string.Format(@"function _addDock() {{
        Sys.Application.remove_load(_addDock);
        $find('{1}').dock($find('{0}'));
        $find('{0}').doPostBack('DockPositionChanged');
    }};
    Sys.Application.add_load(_addDock);", dock.ClientID, chooseZone()), 
                        true); 
     
                        CreateSaveStateTrigger(dock); 
                      
                        //Load the selected widget in the RadDock control 
                        dock.Tag = li.Value; 
                        //dock.Title = DroptDownWidget.SelectedItem.Text; 
                        LoadWidget(dock); 
                    } 
                } 
     
                selectModule_MPE.Hide(); 
            } 
     
            protected string chooseZone() 
            { 
                int zoneOne = RadDockZone1.Docks.Count(); 
                int zoneTwo = RadDockZone2.Docks.Count(); 
     
                if (zoneOne > zoneTwo) 
                { return "ctl00_ctl00_masterContent_mainContent_RadDockZone2"; } 
                else 
                { return "ctl00_ctl00_masterContent_mainContent_RadDockZone1"; } 
            } 

  • Pero Pero admin's avatar

    Posted on Feb 5, 2010 (permalink)

    Hi David,

    Thank you for the source code. I made a couple of modifications to able to run it locally. I did not experience any problems with the positioning of the docks. You are creating the docks in the right way and applying their state on every postback. A possible cause for the problem might be a conflict between the DockStates saved in the Cache and the DB. If the Cache is not empty you are always loading the states from the Cache and not from the DB. Unless I am able to observe the problem locally, I really can't do much to find a solution. Would you please open a support ticket and send a fully running sample project that demonstrates the issue? Once the problem can be debugged locally it would be easier to fix it.


    Regards,
    Pero
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
    Attached files

  • David Moore avatar

    Posted on Feb 5, 2010 (permalink)

    That did it, cleared the cache and re-saved to the database after adding a dock and it works great.

    Thanks a bunch.

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / ASP.NET > Docking > Saving Dynamically Created Dock Problem