| using System; |
| using System.Collections; |
| using System.Collections.Generic; |
| using System.ComponentModel; |
| using System.Data; |
| using System.Drawing; |
| using System.Web; |
| using System.Web.SessionState; |
| using System.Web.UI; |
| using System.Web.UI.WebControls; |
| using System.Web.UI.HtmlControls; |
| using Telerik.Web.UI; |
| using System.Data.SqlClient; |
| using System.Configuration; |
| using System.Text; |
| |
| namespace Telerik.Web.Examples.Dock.MyPortal |
| { |
| public partial class DefaultCS : System.Web.UI.Page |
| { |
| private int _userID = 2; |
| private SqlConnection _conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DockConnectionString"].ConnectionString); |
| |
| private List<DockState> CurrentDockStates |
| { |
| get |
| { |
| //Get saved state string from the database - set it to dockState variable for example |
| string dockStatesFromDB = ""; |
| |
| |
| _conn.Open(); |
| SqlCommand command = new SqlCommand("select State from States where id='" + _userID + "' ", _conn); |
| dockStatesFromDB = command.ExecuteScalar().ToString(); |
| _conn.Close(); |
| |
| List<DockState> _currentDockStates = new List<DockState>(); |
| string[] stringStates = dockStatesFromDB.Split('|'); |
| foreach (string stringState in stringStates) |
| { |
| if (stringState.Trim() != string.Empty) |
| { |
| _currentDockStates.Add(DockState.Deserialize(stringState)); |
| } |
| } |
| return _currentDockStates; |
| } |
| } |
| |
| protected void Page_Load(object sender, EventArgs e) |
| { |
| if (!IsPostBack) |
| { |
| DropDownZone.DataBind(); |
| } |
| } |
| |
| public ArrayList GetZones() |
| { |
| ArrayList zones = new ArrayList(); |
| zones.Add(RadDockZone1); |
| zones.Add(RadDockZone2); |
| |
| return zones; |
| } |
| |
| 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]); |
| |
| //We will just add the RadDock control to the RadDockLayout. |
| // You could use any other control for that purpose, just ensure |
| // that it is inside the RadDockLayout control. |
| // The RadDockLayout control will automatically move the RadDock |
| // controls to their corresponding zone in the LoadDockLayout |
| // event (see below). |
| RadDockLayout1.Controls.Add(dock); |
| if (!IsPostBack) |
| { |
| UpdatePanel1.ContentTemplateContainer.Controls.Add(dock); |
| } |
| //We want to save the dock state every time a dock is moved. |
| CreateSaveStateTrigger(dock); |
| //Load the selected widget |
| LoadWidget(dock); |
| } |
| } |
| } |
| |
| 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) |
| { |
| |
| List<DockState> stateList = RadDockLayout1.GetRegisteredDocksState(); |
| StringBuilder serializedList = new StringBuilder(); |
| int i = 0; |
| |
| while (i < stateList.Count) |
| { |
| serializedList.Append(stateList[i].ToString()); |
| serializedList.Append("|"); |
| i++; |
| } |
| |
| string dockState = serializedList.ToString(); |
| if (dockState.Trim() != String.Empty) |
| { |
| _conn.Open(); |
| SqlCommand command = new SqlCommand(String.Format("update States set State='{0}' where id='" + _userID + "'", dockState), _conn); |
| command.ExecuteNonQuery(); |
| _conn.Close(); |
| } |
| } |
| |
| private RadDock CreateRadDockFromState(DockState state) |
| { |
| RadDock dock = new RadDock(); |
| dock.ID = string.Format("RadDock{0}", state.UniqueName); |
| dock.DockMode = DockMode.Docked; |
| |
| dock.ApplyState(state); |
| dock.Command += new DockCommandEventHandler(dock_Command); |
| dock.Commands.Add(new DockCloseCommand()); |
| dock.Commands.Add(new DockExpandCollapseCommand()); |
| |
| return dock; |
| } |
| |
| private RadDock CreateRadDock() |
| { |
| int docksCount = CurrentDockStates.Count; |
| |
| RadDock dock = new RadDock(); |
| dock.UniqueName = Guid.NewGuid().ToString().Replace('-', 'a'); |
| dock.ID = string.Format("RadDock{0}", dock.UniqueName); |
| dock.Title = "Dock"; |
| dock.Text = string.Format("Added at {0}", DateTime.Now); |
| dock.DockMode = DockMode.Docked; |
| |
| dock.Commands.Add(new DockCloseCommand()); |
| dock.Commands.Add(new DockExpandCollapseCommand()); |
| dock.Command += new DockCommandEventHandler(dock_Command); |
| |
| 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; |
| dock.Style.Add("zIndex", "0"); |
| |
| 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; |
| } |
| Control widget = LoadControl(dock.Tag); |
| dock.ContentContainer.Controls.Add(widget); |
| } |
| |
| |
| protected void ButtonAddDock_Click(object sender, EventArgs e) |
| { |
| RadDock dock = CreateRadDock(); |
| //In order to optimize the execution speed we are adding the dock to a |
| // hidden update panel and then register a script which will move it |
| // to RadDockZone1 after the AJAX request completes. If you want to |
| // dock the control in other zone, modify the script according your needs. |
| UpdatePanel1.ContentTemplateContainer.Controls.Add(dock); |
| |
| ScriptManager.RegisterStartupScript( |
| dock, |
| this.GetType(), |
| "AddDock", |
| 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, DropDownZone.SelectedValue), |
| true); |
| |
| //Right now the RadDock control is not docked. When we try to save its state |
| // later, the DockZoneID will be empty. To workaround this problem we will |
| // set the AutoPostBack property of the RadDock control to true and will |
| // attach an AsyncPostBackTrigger for the DockPositionChanged client-side |
| // event. This will initiate second AJAX request in order to save the state |
| // AFTER the dock was docked in RadDockZone1. |
| CreateSaveStateTrigger(dock); |
| |
| //Load the selected widget in the RadDock control |
| dock.Tag = DroptDownWidget.SelectedValue; |
| LoadWidget(dock); |
| } |
| } |
| } |