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

How to Save /Load Raddock state to Database

4 Answers 329 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Priya
Top achievements
Rank 1
Priya asked on 18 Oct 2008, 05:48 AM
Sir,

I tried with session.But i need to save the location,position of each dock to database.

plz help me..
Thanks...

4 Answers, 1 is accepted

Sort by
0
Obi-Wan Kenobi
Top achievements
Rank 1
answered on 20 Oct 2008, 09:07 AM
Please take a look at the following forum thread:
http://www.telerik.com/community/forums/thread/b311D-bkdgdg.aspx

"I prepared a sample project which demonstrates how you could modify the Dynamically Created Docks example so that it saves the state into Database. Please, take a look at the attached archive and let us know in case you still experience any problems.

Best regards,
Sophy
"

Hope this helps!
0
Ricky
Top achievements
Rank 2
answered on 24 Oct 2008, 06:23 AM
Hi Priya
Use the RadDockLayout1_SaveDockLayout and Create a connection to your database and store it there.

as

 

Protected Sub RadDockLayout1_SaveDockLayout(ByVal sender As Object, ByVal e As DockLayoutEventArgs)
' Create a connection
' Save or Update  Session("UserPreferences.PortalConfiguration").ToString  In Database field if you using this session variable.

  

End Sub
 
And fill the Session() variable again on next load from database.

Cheers
Rick

 

 

 

 

 

 

 

0
Priya
Top achievements
Rank 1
answered on 05 Nov 2008, 12:07 PM
Sir,
The below code works for me when i create the radDockzones in aspx page itself.But In my project ,I created the dockzones dynamically.then d below code wont work.
In page1.aspx

<

form id="Form1" method="post" runat="server">

 

 

 

<asp:scriptmanager id="ScriptManager" runat="server" />

 

 

<asp:button runat="server" CssClass="button" id="ButtonAddDock" text="Add Dock" onclick="ButtonAddDock_Click" />

 

 

<asp:button runat="server" id="ButtonPostback" CssClass="button" text="Make Postback" />

 

 

<br/><br/>

 

 

<telerik:raddocklayout runat="server" id="RadDockLayout1"

 

 

onsavedocklayout="RadDockLayout1_SaveDockLayout"

 

 

onloaddocklayout="RadDockLayout1_LoadDockLayout">

 

<%

--<telerik:raddockzone runat="server" id="RadDockZone1" Width="300" MinHeight="200" Style="float:left;margin-right:20px;">

 

</telerik:raddockzone>--

%>

 

 

<telerik:raddockzone Width="300" MinHeight="200" runat="server" id="RadDockZone2" Style="float:left;">

 

 

</telerik:raddockzone>

 

 

<div style="display:none">

 

Hidden UpdatePanel, which is used to receive the new dock controls.

We will move them with script to the desired initial dock zone.

 

<asp:updatepanel runat="server" id="UpdatePanel1">

 

 

<triggers>

 

 

<asp:asyncpostbacktrigger controlid="ButtonAddDock" eventname="Click" />

 

 

</triggers>

 

 

</asp:updatepanel>

 

 

</div>

 

 

</telerik:raddocklayout>

 

 

 

</form>
In Page1.aspx.cs

 

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.DynamicDocks

 

{

 

public partial class DefaultCS : System.Web.UI.Page

 

 

 

{

 

private SqlConnection _conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);

 

 

// myConnection2 = New SqlConnection(ConfigurationManager.AppSettings("ConnectionString"))

 

 

 

 

private int _count = 0;

 

 

private RadDock CreateRadDockFromState(string stringState)

 

{

System.Web.Script.Serialization.

JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

 

 

DockState state = serializer.Deserialize<DockState>(stringState);

 

 

RadDock dock = new RadDock();

 

dock.ID =

string.Format("RadDock{0}", state.UniqueName);

 

dock.ApplyState(state);

dock.Command +=

new DockCommandEventHandler(dock_Command);

 

dock.Commands.Add(

new DockCloseCommand());

 

dock.Commands.Add(

new DockExpandCollapseCommand());

 

 

return dock;

 

}

 

private RadDock CreateRadDock()

 

{

 

RadDock dock = new RadDock();

 

dock.UniqueName =

Guid.NewGuid().ToString();

 

dock.ID =

string.Format("RadDock{0}", dock.UniqueName);

 

dock.Title =

"Dock";

 

dock.Text =

string.Format("Added at {0}", DateTime.Now);

 

dock.Width =

Unit.Pixel(300);

 

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;

 

 

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);

}

 

protected void Page_Init(object sender, EventArgs e)

 

{

 

string dockState = "";

 

 

try

 

 

 

{

_conn.Open();

 

SqlCommand command = new SqlCommand("select State from dockState where id=1", _conn);

 

dockState = command.ExecuteScalar().ToString();

_conn.Close();

}

 

catch

 

 

 

{

}

 

string[] currentDockStates = dockState.Split('|');

 

_count = currentDockStates.Length;

 

//Recreate the docks in order to ensure their proper operation

 

 

 

 

for (int i = 0; i < _count; i++)

 

{

 

if (currentDockStates[i].Trim() != string.Empty)

 

{

 

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);

 

//We want to save the dock state every time a dock is moved.

 

 

 

CreateSaveStateTrigger(dock);

}

}

}

 

 

protected void RadDockLayout1_SaveDockLayout(object sender, Telerik.Web.UI.DockLayoutEventArgs e)

 

{

 

string dockState;

 

System.Web.Script.Serialization.

JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

 

 

List<DockState> stateList = RadDockLayout1.GetRegisteredDocksState();

 

 

StringBuilder serializedList = new StringBuilder();

 

 

int i = 0;

 

 

while (i < stateList.Count)

 

{

serializedList.Append(serializer.Serialize(stateList[i]));

serializedList.Append(

"|");

 

i++;

}

dockState = serializedList.ToString();

 

if (dockState.Trim() != String.Empty)

 

{

 

try

 

 

 

{

_conn.Open();

 

SqlCommand command = new SqlCommand(String.Format("update DockState set State='{0}' where id=3", dockState), _conn);

 

command.ExecuteNonQuery();

_conn.Close();

}

 

catch

 

 

 

{

}

}

 

//Save the dockState string into DB

 

 

 

}

 

protected void RadDockLayout1_LoadDockLayout(object sender, Telerik.Web.UI.DockLayoutEventArgs e)

 

{

System.Web.Script.Serialization.

JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();

 

 

//Get saved state string from the database - set it to dockState variable for example

 

 

 

 

string dockState = "";

 

 

try

 

 

 

{

_conn.Open();

 

SqlCommand command = new SqlCommand("select State from DockState where id=3", _conn);

 

dockState = command.ExecuteScalar().ToString();

_conn.Close();

}

 

catch

 

 

 

{

}

 

string[] currentDockStates = dockState.Split('|');

 

 

foreach (string stringState in currentDockStates)

 

{

 

if (stringState.Trim() != string.Empty)

 

{

 

DockState state = serializer.Deserialize<DockState>(stringState);

 

e.Positions[state.UniqueName] = state.DockZoneID;

e.Indices[state.UniqueName] = state.Index;

}

}

}

 

 

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, RadDockZone1.ClientID),

 

 

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);

}

}

}

Thanks

0
Obi-Wan Kenobi
Top achievements
Rank 1
answered on 05 Nov 2008, 04:18 PM
I saw that in LoadDockLayout the command is with id=1:
SqlCommand command = new SqlCommand("select State from dockState where id=1", _conn);
But in SaveDockLayout , the command is with id=3:
SqlCommand command = new SqlCommand(String.Format("update DockState set State='{0}' where id=3", dockState), _conn); 

On my opinion the id should be equal in both cases.

P.S You can use the FormatCodeBlock tool when you paste a code from VS to RadEditor
Tags
Dock
Asked by
Priya
Top achievements
Rank 1
Answers by
Obi-Wan Kenobi
Top achievements
Rank 1
Ricky
Top achievements
Rank 2
Priya
Top achievements
Rank 1
Share this question
or