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

dynamic Dock states always empty

1 Answer 46 Views
Dock
This is a migrated thread and some comments may be shown as answers.
HL
Top achievements
Rank 1
HL asked on 22 Feb 2010, 05:11 AM
It has so many examples posted to save dynamic docks to DB and retrieve from DB. I couldn't figure out why the dock state are always empty when I added multi docks once. If I only add one dock per time, it works.

My situation is that all the user controls will be added to the docks together (once)  when user first goes to Dock widget page. Then user can drag, close, drop... etc  docks.User's dock state will save to DB after user click "Save" button. Before User click save button,  I saved the dockstate to the viewstate variable.The issue is that when I retrieve dockstate from the viewstate varible on ButtonSave_Click event, the viewstate variable is always nothing.


Can any one help me out?

Pls: it is the first time for me to post the question. not sure what is the best way to post codes since It took me 10 minutes to remove the extra empty space even I used the "Format Code Block". any suggestion?

My codes:

 

 

Private ReadOnly Property CurrentDockStates() As List(Of DockState)   
 Get   
 Dim dockStatesFromDB As String   
 if Not ViewState("UserDockStatesFromDB"Is Nothing AndAlso CType(ViewState("UserDockStatesFromDB"), String) <> String.Empty Then   
     dockStatesFromDB = CType(ViewState("UserDockStatesFromDB"), String)   
 End If   
 Dim _currentDockStates As New List(Of DockState)()  
 If dockStatesFromDB <> String.Empty Then   
        Dim stringStates As String() = dockStatesFromDB.Split("|")   
        For Each stringState As String In stringStates   
                   If stringState.Trim() <> String.Empty Then   
                      _currentDockStates.Add(DockState.Deserialize(stringState))   
                   End If 
       Next   
End If   
 
Return _currentDockStates   
End Get   
End Property 
 
 
Protected Sub Page_Init(ByVal sender As ObjectByVal e As EventArgs) Handles Me.Init   
If Not Page.IsPostBack Then   
  loadData()  
  ViewState("UserDockStatesFromDB") = GetUserDockState()   
 End If   
 '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))   
      RadDockLayout1.Controls.Add(dock)  
 
     'We want to save the dock state every time a dock is moved.    
      CreateSaveStateTrigger(dock)  
      'Load the selected widget   
      LoadWidget(dock)  
End If   
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)  
End While   
End Sub   
   
 
Private Function GetUserDockState() As String      
 Dim strUserDockStatesFromDB As String      
 Dim ds As DataSet      
 Dim intViewCategotyID As Integer = -1      
 Dim dr As DataRow      
 Dim dt As DataTable      
 Dim objBS As New  BusinessServices.DockView       
objBS.SystemUserID = SystemUserID       
If Not Me.drpDockViewCategoty.SelectedItem Is Nothing Then      
  intViewCategotyID = Me.drpDockViewCategoty.SelectedValue          
End If      
       
ds = objBS.GetSystemUserDockState(intViewCategotyID)       
CommonMethods.SetFormDataSet(GS.tblSystemUserDockState, FormData)       
If Not ds.Tables(GS.tblSystemUserDockState) Is Nothing Then        
dt = ds.Tables(GS.tblSystemUserDockState)      
If Not dt.Rows Is Nothing AndAlso dt.Rows.Count > 0 Then    
With ds.Tables(GS.tblSystemUserDockState).Rows(0)   
  strUserDockStatesFromDB = .Item("DockState")      
  CurUserDockStateID = GS.Null2Zero(.Item("SystemDockStateID"))      
  CurViewCategoryID = GS.Null2Zero(.Item("DockViewCategoryID"))     
End With      
End If       
End If       
    
If strUserDockStatesFromDB = String.Empty Then       
'reload all the default control        
CreateDefaultRadDock()       
End If      
    
Return strUserDockStatesFromDB        
End Function        
    
    
    
Private Sub loadData()     
'load View Category    
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(ByVal strTitle As StringAs 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 = strTitle     
   'dock.EnableAnimation = True      
   dock.Text =String.Format("Added at {0}", DateTime.Now)      
   dock.Width = Unit.Pixel(100)     
   dock.BorderWidth = Unit.Pixel(2)     
   dock.BorderColor = Color.LightGray     
   dock.Commands.Add(New DockCloseCommand())      
      dock.Commands.Add(New DockExpandCollapseCommand())      
  dock.CommandsAutoPostBack = True      
       
Return dock      
      
End Function      
 'when user first time load Dock layout, it will load all the control to three radzone      
 Private Sub CreateDefaultRadDock()     
 Dim index As Integer      
 Dim DockList As New ArrayList()      
 Dim intCount As Integer = 0      
  Dim intReminder As Integer      
  Dim dock As RadDock      
  If Not Me.chkDockList.Items Is Nothing Then      
  intCount = Me.chkDockList.Items.Count      
  End If      
      
      
For index = 0 To intCount - 1     
dock = CreateRadDock(chkDockList.Items(index).Text)    
intReminder = index Mod 3      
 Select Case intReminder      
   Case 0      
   RadDockZone1.Controls.Add(dock)     
  Case 1      
 RadDockZone2.Controls.Add(dock)     
  Case 2      
  RadDockZone3.Controls.Add(dock)     
  End Select      
   Me.chkDockList.Items(index).Selected = True      
  'We want to save the dock state every time a dock is moved.      
  CreateSaveStateTrigger(dock)     
  'Load the selected widget      
   dock.Tag = chkDockList.Items(index).Value     
    
LoadWidget(dock)     
Next index      
  End Sub    
 
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 saveStateTrigger As 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)     
    
 
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 RadDockLayout1_LoadDockLayout(ByVal sender As ObjectByVal e As DockLayoutEventArgs) Handles RadDockLayout1.LoadDockLayout      
 
Try      
 
    
Dim serializedList As String      
      
 If Not ViewState("UserDockStatesFromDB"Is Nothing Then      
  serializedList = ViewState(     
    
"UserDockStatesFromDB")      
      
    
      
    
    
    
End If      
      
    
    
    
      
    
If serializedList <> Nothing Then      
      
    
    
    
      
    
Dim states As String() = serializedList.Split("|"c)      
      
    
      
    
    
    
Dim i As Integer = 0      
      
    
      
    
    
    
While i < states.Length      
      
    
      
    
    
    
Dim state As DockState = DockState.Deserialize(states(i))      
      
    
e.Positions(state.UniqueName) = state.DockZoneID     
    
e.Indices(state.UniqueName) = state.Index     
    
      
    
    
    
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)      
 
End While      
 
    
End If      
   Catch ex As Exception      
 
End Try      
 End Sub    
 
Protected Sub RadDockLayout1_SaveDockLayout(ByVal sender As ObjectByVal e As DockLayoutEventArgs) Handles RadDockLayout1.SaveDockLayout      
      
 Dim stateList As List(Of DockState)=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      
     
ViewState("UserDockStatesFromDB") = dockState      
      
  End If      
End Sub      
      
      
'+-------------------------------------------------------------------------------'| Method : ButtonSave_Click      
'| Purpose : save all the state when user click Save button      
'| Returns : Boolean    
'+-------------------------------------------------------------------------------  
Protected Sub ButtonSave_Click(ByVal sender As ObjectByVal e As EventArgs) Handles btnSave.Click      
 
Dim dockState As String      
  If Not ViewState("UserDockStatesFromDB"Is Nothing Then      
    dockState = ViewState("UserDockStatesFromDB")      
End If    
    
If GetControlData(dockState) Then      
 
SaveUserDockState(FormData)     
 
End If      
 End Sub      
      
    
    
    
    
 
Private
 Function GetUserDockState() As String   
 Dim strUserDockStatesFromDB As String   
 Dim ds As DataSet   
 Dim intViewCategotyID As Integer = -1   
 Dim dr As DataRow   
 Dim dt As DataTable   
 Dim objBS As New  BusinessServices.DockView   
 
objBS.SystemUserID = SystemUserID  
 
   
 
 
 
If Not Me.drpDockViewCategoty.SelectedItem Is Nothing Then   
   
 
   
 
   
 
 
 
   
   
 
         intViewCategotyID =   
 
 
 
Me.drpDockViewCategoty.SelectedValue   
   
 
   
 
 
 
End If   
   
 
   
 
   
 
 
 
   
   
 
ds = objBS.GetSystemUserDockState(intViewCategotyID)  
 
CommonMethods.SetFormDataSet(GS.tblSystemUserDockState, FormData)  
 
   
 
 
 
If Not ds.Tables(GS.tblSystemUserDockState) Is Nothing Then   
   
 
   
 
   
 
 
 
   
   
 
dt = ds.Tables(GS.tblSystemUserDockState)  
 
   
 
 
 
If Not dt.Rows Is Nothing AndAlso dt.Rows.Count > 0 Then   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
With ds.Tables(GS.tblSystemUserDockState).Rows(0)   
   
 
      strUserDockStatesFromDB = .Item(  
 
 
 
"DockState")   
   
 
      CurUserDockStateID = GS.Null2Zero(.Item(  
 
 
 
"SystemDockStateID"))   
   
 
      CurViewCategoryID = GS.Null2Zero(.Item(  
 
 
 
"DockViewCategoryID"))   
   
 
   
 
 
 
End With   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
End If   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
End If   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
If strUserDockStatesFromDB = String.Empty Then   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
'reload all the default control   
   
 
   
 
   
 
 
 
   
   
 
CreateDefaultRadDock()  
 
   
 
 
 
End If   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
Return strUserDockStatesFromDB   
   
 
   
 
 
 
End Function   
   
 
   
 
   
 
 
 
 
   
   
 
   
 
 
 
Private Sub loadData()   
   
 
   
 
 
 
'load View Category   
   
 
   
 
   
 
 
 
   
   
 
   
 
   
 
 
 
   
   
 
 
 
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(ByVal strTitle As StringAs 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 = strTitle  
 
   
 
 
 
'dock.EnableAnimation = True   
   
 
   
 
   
 
 
 
   
   
 
dock.Text =   
 
 
 
String.Format("Added at {0}", DateTime.Now)   
   
 
dock.Width = Unit.Pixel(100)  
 
dock.BorderWidth = Unit.Pixel(2)  
 
dock.BorderColor = Color.LightGray  
 
dock.Commands.Add(  
 
 
 
New DockCloseCommand())   
   
 
dock.Commands.Add(  
 
 
 
New DockExpandCollapseCommand())   
   
 
   
 
 
 
dock.CommandsAutoPostBack = True   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
Return dock   
   
 
   
 
 
 
End Function   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
'when user first time load Dock layout, it will load all the control to three radzone   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
Private Sub CreateDefaultRadDock()  
 
   
 
   
 
 
 
 
Dim index As Integer   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
Dim DockList As New ArrayList()   
   
 
   
 
 
 
Dim intCount As Integer = 0   
   
 
   
 
 
 
Dim intReminder As Integer   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
Dim dock As RadDock   
   
 
   
 
 
 
If Not Me.chkDockList.Items Is Nothing Then   
   
 
   
 
   
 
 
 
   
   
 
intCount =   
 
 
 
Me.chkDockList.Items.Count   
   
 
   
 
 
 
End If   
   
 
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
For index = 0 To intCount - 1   
   
 
dock = CreateRadDock(chkDockList.Items(index).Text)  
 
intReminder = index   
 
 
 
Mod 3   
   
 
   
 
 
 
Select Case intReminder   
   
 
   
 
 
 
Case 0   
   
 
RadDockZone1.Controls.Add(dock)  
 
   
 
 
 
Case 1   
   
 
RadDockZone2.Controls.Add(dock)  
 
   
 
 
 
Case 2   
   
 
RadDockZone3.Controls.Add(dock)  
 
   
 
 
 
End Select   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
Me.chkDockList.Items(index).Selected = True   
   
 
   
 
   
 
 
 
   
   
 
   
 
 
 
'We want to save the dock state every time a dock is moved.   
   
 
   
 
   
 
 
 
   
   
 
CreateSaveStateTrigger(dock)  
 
   
 
 
 
'Load the selected widget   
   
 
   
 
   
 
 
 
   
   
 
dock.Tag = chkDockList.Items(index).Value  
 
LoadWidget(dock)  
 
   
 
 
 
Next index   
   
 
   
 
 
 
End Sub 
 
 
   
   
 
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 saveStateTrigger As 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)  
 
   
 
 
 
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 RadDockLayout1_LoadDockLayout(ByVal sender As ObjectByVal e As DockLayoutEventArgs) Handles RadDockLayout1.LoadDockLayout   
   
 
   
 
 
 
Try   
   
 
 
 
   
 
Dim serializedList As String   
   
 
 
 
   
 
If Not ViewState("UserDockStatesFromDB"Is Nothing Then   
   
 
 
 
serializedList = ViewState(  
 
"UserDockStatesFromDB")   
   
 
   
 
 
 
End If   
   
 
 
 
   
 
If serializedList <> Nothing Then   
   
 
 
 
   
 
Dim states As String() = serializedList.Split("|"c)   
   
 
   
 
 
 
Dim i As Integer = 0   
   
 
   
 
 
 
While i < states.Length   
   
 
   
 
 
 
Dim state As DockState = DockState.Deserialize(states(i))   
   
 
e.Positions(state.UniqueName) = state.DockZoneID  
 
e.Indices(state.UniqueName) = state.Index  
 
   
 
 
 
System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)   
   
 
   
 
 
 
End While   
   
 
 
 
   
 
End If   
   
 
 
 
   
 
Catch ex As Exception   
   
 
   
 
 
 
End Try   
   
 
 
 
   
 
End Sub 
 
 
 
   
 
 
 
   
 
Protected Sub RadDockLayout1_SaveDockLayout(ByVal sender As ObjectByVal e As DockLayoutEventArgs) Handles RadDockLayout1.SaveDockLayout   
   
 
   
 
 
 
Dim stateList As List(Of DockState) = 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   
   
 
 
 
ViewState(  
 
"UserDockStatesFromDB") = dockState   
   
 
   
 
 
 
End If   
   
 
 
 
   
 
End Sub   
   
 
   
 
 
 
   
 
'+---------------------------------------------------------------------------------------------   
   
 
 
 
   
 
'| Method : ButtonSave_Click   
   
 
 
 
   
 
'| Purpose : save all the state when user click Save button   
   
 
 
 
   
 
'| Returns : Boolean   
   
 
 
 
   
 
'+---------------------------------------------------------------------------------------------   
   
 
 
 
   
 
Protected Sub ButtonSave_Click(ByVal sender As ObjectByVal e As EventArgs) Handles btnSave.Click   
   
 
   
 
 
 
Dim dockState As String   
   
 
 
 
   
 
If Not ViewState("UserDockStatesFromDB"Is Nothing Then   
   
 
 
 
dockState = ViewState(  
 
"UserDockStatesFromDB")   
   
 
   
 
 
 
End If 
 
If GetControlData(dockState) Then   
   
 
 
 
SaveUserDockState(FormData)  
 
   
 
End If   
   
 
 
 
   
 
End Sub   
   
 
 
 
 
 
 

1 Answer, 1 is accepted

Sort by
0
Pero
Telerik team
answered on 24 Feb 2010, 04:22 PM
Hi HL,

The problem is caused by the fact that you are trying to use the ViewState object in the Page_Init event when the ViewState has not been loaded yet. The ViewState is loaded in the LoadViewState event which is fired on postback only after the Page_Init event: http://www.sql-server-performance.com/articles/asp_ado/page_life_cycle_p1.aspx. If you don't want to save the current DockStates on every postback in the DB, you should use the Session object which is available at any stage in the ASP.NET page lifecycle.


Greetings,
Pero
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Dock
Asked by
HL
Top achievements
Rank 1
Answers by
Pero
Telerik team
Share this question
or