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

Loading Selected dock layout from database

1 Answer 79 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 29 Jan 2010, 08:52 AM
I have successfully implemented from your sample a portal page using the dock that saves and reloads from the database dependant on user. However the user has different roles and therefore different portal pages. This is selected by a dropdown on the page.

The problem I am having is as the dynamically loaded dock state is actioned on the initialization of the page the new selected value of the dropdown is not available until the load stage.

Can you please advise how I can load the dock from the database dependant on the selected value of the dropdown.

Thanks

Mark

Private _conn As New SqlConnection(ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString)  
 
    Private ReadOnly Property CurrentDockStates() As List(Of DockState)  
        Get 
            'Get saved state string from the database - set it to dockState variable for example   
            Dim dockStatesFromDB As String = "" 
 
            _conn.Open()  
            Dim command As New SqlCommand("select State from States where id='" + Me.DropDownList1.SelectedValue.ToString + "'", _conn)  
            dockStatesFromDB = command.ExecuteScalar().ToString()  
            _conn.Close()  
 
            Dim _currentDockStates As New List(Of DockState)()  
            Dim stringStates As String() = dockStatesFromDB.Split("|"c)  
            For Each stringState As String In stringStates  
                If stringState.Trim() <> String.Empty Then 
                    _currentDockStates.Add(DockState.Deserialize(stringState))  
                End If 
            Next 
            Return _currentDockStates  
        End Get 
    End Property 
 
    Protected Sub Page_Init(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Init  
 
        'Recreate the docks in order to ensure their proper operation  
        Dim i As Integer = 0  
        While i < CurrentDockStates.Count  
            If CurrentDockStates(i).Closed = False Then 
                Dim dock As RadDock = 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).  
 
                'We want to save the dock state every time a dock is moved.  
                CreateSaveStateTrigger(dock)  
                'Load the selected widget  
                LoadWidget(dock)  
 
                RadDockLayout1.Controls.Add(dock)  
            End If 
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)  
        End While 
    End Sub 
 
    Protected Sub RadDockLayout1_LoadDockLayout(ByVal sender As ObjectByVal e As DockLayoutEventArgs)  
        'Populate the event args with the state information. The RadDockLayout control  
        ' will automatically move the docks according that information.  
        For Each state As DockState In CurrentDockStates  
            e.Positions(state.UniqueName) = state.DockZoneID  
            e.Indices(state.UniqueName) = state.Index  
        Next 
    End Sub 
 
    Protected Sub RadDockLayout1_SaveDockLayout(ByVal sender As ObjectByVal e As DockLayoutEventArgs)  
        Dim stateList As List(Of DockState) = Me.RadDockLayout1.GetRegisteredDocksState()  
        Dim serializedList As New StringBuilder()  
        Dim i As Integer = 0  
 
        While i < stateList.Count  
            serializedList.Append(stateList(i).ToString())  
            serializedList.Append("|")  
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)  
        End While 
 
        Dim dockState As String = serializedList.ToString()  
        If dockState.Trim() <> [String].Empty Then 
            _conn.Open()  
            Dim command As New SqlCommand([String].Format("update States set State='{0}' where id='" + Me.DropDownList1.SelectedValue.ToString + "'", dockState), _conn)  
            command.ExecuteNonQuery()  
            _conn.Close()  
        End If 
    End Sub 
 
    Private Function CreateRadDockFromState(ByVal state As DockState) As RadDock  
        Dim dock As New RadDock()  
        dock.DockMode = DockMode.Docked  
        dock.ID = String.Format("RadDock{0}", state.UniqueName)  
        dock.ApplyState(state)  
        dock.EnableAnimation = True 
 
        dock.Commands.Add(New DockCloseCommand)  
        dock.Commands.Add(New DockExpandCollapseCommand())  
 
        Return dock  
    End Function 
 
    Private Function CreateRadDock() As RadDock  
        Dim docksCount As Integer = CurrentDockStates.Count  
 
        Dim dock As New RadDock()  
        dock.DockMode = DockMode.Docked  
        dock.UniqueName = Guid.NewGuid().ToString()  
        dock.ID = String.Format("RadDock{0}", dock.UniqueName)  
        dock.Title = Replace(Replace(DroptDownWidget.SelectedItem.Text, ".ascx"""), "_"" ")  
        dock.EnableAnimation = True 
 
        dock.Text = String.Format("Added at {0}", DateTime.Now)  
        dock.Width = Unit.Pixel(386)  
 
        dock.Commands.Add(New DockCloseCommand())  
        dock.Commands.Add(New DockExpandCollapseCommand())  
        dock.CommandsAutoPostBack = True 
 
        Return dock  
    End Function 
 
    Private Sub CreateSaveStateTrigger(ByVal dock As RadDock)  
        '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 
 
        Dim updatedControl As New AjaxUpdatedControl()  
        updatedControl.ControlID = "Panel1" 
 
        Dim setting1 As New AjaxSetting(dock.ID)  
        setting1.EventName = "DockPositionChanged" 
        setting1.UpdatedControls.Add(updatedControl)  
 
        Dim setting2 As New AjaxSetting(dock.ID)  
        setting2.EventName = "Command" 
        setting2.UpdatedControls.Add(updatedControl)  
 
        RadAjaxManager1.AjaxSettings.Add(setting1)  
        RadAjaxManager1.AjaxSettings.Add(setting2)  
 
 
    End Sub 
 
    Private Sub LoadWidget(ByVal dock As RadDock)  
        If String.IsNullOrEmpty(dock.Tag) Then 
            Return 
        End If 
        Dim widget As Control = LoadControl(dock.Tag)  
        dock.ContentContainer.Controls.Add(widget)  
    End Sub 
 
    Protected Sub ButtonAddDock_Click(ByVal sender As ObjectByVal e As EventArgs)  
        Dim dock As RadDock = CreateRadDock()  
        'find the target zone and add the new dock there  
        Dim dz As RadDockZone = DirectCast(XMLUtilities.FindControlRecursive(Master, "RadDockZone1"), RadDockZone)  
 
        dz.Controls.Add(dock)  
 
        CreateSaveStateTrigger(dock)  
 
        'Load the selected widget in the RadDock control  
        dock.Tag = DroptDownWidget.SelectedValue  
 
        LoadWidget(dock)  
        ScriptManager.RegisterStartupScript(MeMe.[GetType](), "MoveDock"String.Format("function _moveDock() {{" & vbCr & vbLf & vbTab & vbTab & vbTab & "        " & vbTab & "Sys.Application.remove_load(_moveDock);" & vbCr & vbLf & vbTab & vbTab & vbTab & vbTab & "        $find('{1}').dock($find('{0}'),{2});" & vbCr & vbLf & vbTab & vbTab & vbTab & "            }};" & vbCr & vbLf & vbTab & vbTab & vbTab & "            Sys.Application.add_load(_moveDock);""$find('<%= RadDock1.ClientID %>');""RadDockZone1", dz.Docks.Count), True)  
 
    End Sub 

1 Answer, 1 is accepted

Sort by
0
Pero
Telerik team
answered on 03 Feb 2010, 01:49 PM
Hi Mark,

You can find the control that posted the page back to the server with the following method:

public Control GetPostBackControl(Page page)
{
    Control control = null;
 
    eventTarget = page.Request.Params.Get("__EVENTTARGET");
    eventArgument = page.Request.Params.Get("__EVENTARGUMENT");
    if (eventTarget != null && eventTarget != string.Empty)
    {
        control = page.FindControl(eventTarget);
    }
    else
    {
        foreach (string ctl in page.Request.Form)
        {
            Control c = page.FindControl(ctl);
            if (c is RadComboBox)
            {
                control = c;
                break;
            }
        }
    }
    return control;
}

The page.Request.Params.Get("__EVENTTARGET"); contains the UniqueID of the control that posted back the page and page.Request.Params.Get("__EVENTARGUMENT"); contains the event arguments send. You can use a RadComboBox control to choose the current role and when the page is posted back you can extract the selected index at any stage in the page life cycle, from the event arguments sent by the ComboBox. Here is the source code of a project demonstrating this approach:

.aspx
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <div>
        <telerik:RadComboBox ID="RadComboBox1" runat="server" AutoPostBack="true">
            <Items>
                <telerik:RadComboBoxItem Text="Item1" Value="Item1" />
                <telerik:RadComboBoxItem Text="Item2" Value="Item2" />
                <telerik:RadComboBoxItem Text="Item3" Value="Item3" />
                <telerik:RadComboBoxItem Text="Item4" Value="Item4" />
            </Items>
        </telerik:RadComboBox>
        <br />
        <br />
        Control that caused postback:
        <asp:Label ID="Label1" runat="server" Text="[None]"></asp:Label><br />
        Event Arguments send by the ComboBox:
        <asp:Label ID="Label3" runat="server" Text="[None]"></asp:Label><br />
        Item selected from the dropdown:
        <asp:Label ID="Label2" runat="server" Text="[None]"></asp:Label>
    </div>
    </form>
</body>
</html>

.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Web.Script.Serialization;
 
public partial class Default2 : System.Web.UI.Page
{
    string eventTarget = "Index";
    string eventArgument = "Index";
 
    protected void Page_Init(object sender, EventArgs e)
    {
        GetPostBackControl(Page);
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            Label1.Text = eventTarget;
            Label3.Text = eventArgument;
 
            //The following lines will find the currently selected index of the combobox
            int begin = eventArgument.IndexOf("Index") + 7;
            int indexClicked = Convert.ToInt32(eventArgument.Substring(begin, 1));
 
            Label2.Text = eventArgument.Substring(begin, 1);
 
        }
 
    }
 
    public Control GetPostBackControl(Page page)
    {
        Control control = null;
 
        eventTarget = page.Request.Params.Get("__EVENTTARGET");
        eventArgument = page.Request.Params.Get("__EVENTARGUMENT");
        if (eventTarget != null && eventTarget != string.Empty)
        {
            control = page.FindControl(eventTarget);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is RadComboBox)
                {
                    control = c;
                    break;
                }
            }
        }
        return control;
    }
}


 

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.
Tags
Dock
Asked by
Mark
Top achievements
Rank 1
Answers by
Pero
Telerik team
Share this question
or