help! Save/Load state Sample![C# code]

5 posts, 0 answers
  1. ghou ghiau-jsui
    ghou ghiau-jsui avatar
    2 posts
    Member since:
    Oct 2009

    Posted 12 Oct 2009 Link to this post

    Hello,
    I may have some trouble about Dynamic Saving & Loading docks from MS SQL DATABASE.

    Please Help Me!!!

    This is my default.cs file!

    is there any sample for "dynamic add" + "SAVE&LOAD" for us?

    error message "serializer doesn't support 'Telerik.Web.UI.DockState'." 
     
    using System;  
    using System.Data;  
    using System.Data.SqlClient;  
    using System.Configuration;  
    using System.Collections;  
    using System.Web;  
    using System.Web.Security;  
    using System.Web.UI;  
    using System.Web.UI.WebControls;  
    using System.Web.UI.WebControls.WebParts;  
    using System.Web.UI.HtmlControls;  
    using System.Web.Script.Serialization;  
    using System.Collections.Generic;  
    using Telerik.Web.UI;  
    using System.Text;  
     
    public partial class index : System.Web.UI.Page  
    {  
        protected string LoadUserData(string UserAccount)  
        {  
            SqlConnection conn = new SqlConnection("Data Source=(LOCAL);;Initial Catalog=BPMDB;User ID=sa;Password=*******");  
            conn.Open();  
     
            string SQLStr = "SELECT  [string] FROM [index_position] WHERE (id_user = '" + UserAccount + "')";  
            SqlCommand cmd3 = new SqlCommand(SQLStr, conn);  
            SqlDataAdapter sda = new SqlDataAdapter(cmd3);  
            DataTable dt = new DataTable();  
            sda.Fill(dt);  
            string posiStr = dt.Rows[0].ItemArray[0].ToString();  
            return posiStr;  
        }  
        protected bool UserDataChecker(string UserAccount)  
        {  
            SqlConnection conn = new SqlConnection("Data Source=(LOCAL);;Initial Catalog=BPMDB;User ID=sa;Password=1*********");  
            conn.Open();  
     
     
            string SQLStr = "SELECT  [string] FROM [index_position] WHERE (id_user = '" + UserAccount + "')";  
            SqlCommand cmd3 = new SqlCommand(SQLStr, conn);  
              
            SqlDataReader dr = cmd3.ExecuteReader();  
            if (dr.Read())  
            {  
                //not insert  
     
                return true;  
            }  
            else 
            {  
                //insert   
     
                return false;  
            }  
              
     
        }  
     
     
     
        private bool _dockStateCleared = false;  
        private List<DockState> CurrentDockStates  
        {  
            get 
            {  
     
                  
                List<DockState> _currentDockStates = (List<DockState>)Session["CurrentDockStatesMyPortal"];//UserDataLoader(AspxHelper.LoginUserAccount.ToString());  
                  
     
                 
                if (Object.Equals(_currentDockStates, null))  
                {  
                    _currentDockStates = new List<DockState>();  
                    Session["CurrentDockStatesMyPortal"] = _currentDockStates;  
     
                }  
     
     
                return _currentDockStates;  
            }  
            set 
            {  
     
                Session["CurrentDockStatesMyPortal"] = value;  
            }  
        }  
        //private List<DockState> CurrentDockStates = new List<DockState>();  
        protected void Page_Init(object sender, EventArgs e)  
        {  
            //if(UserDataChecker(AspxHelper.LoginUserAccount.ToString()))  
            //         CurrentDockStates = UserDataLoader(AspxHelper.LoginUserAccount.ToString());  
            foreach (DockState state in CurrentDockStates)  
            {  
                if (state.Closed == false)  
                {  
                    RadDock dock = CreateRadDockFromState(state);  
                    RadDockLayout1.Controls.Add(dock);  
                    CreateSaveStateTrigger(dock);  
                    LoadWidget(dock);  
                }  
            }  
              
     
        }  
        protected void Page_Load(object sender, EventArgs e)  
        {  
     
            PageHeader1.SelectedPageIndex = 0;  
            this.Title = "SmartBPM.NET--" + PageHeader1.SelectedPageName;  
            if (!IsPostBack)  
            {  
                DropDownZone.DataBind();  
            }  
        }  
     
          
     
        private void LoadWidget(RadDock dock)  
        {  
            if (string.IsNullOrEmpty(dock.Tag))  
            {  
                return;  
            }  
            Control widget = LoadControl(dock.Tag);  
            dock.ContentContainer.Controls.Add(widget);  
        }  
        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 RadDock CreateRadDockFromState(DockState state)  
        {  
            //建立一個dock並設定可用之動作(關閉、收合)  
            RadDock dock = new RadDock();  
            dock.DockMode = DockMode.Docked;  
            dock.ID = string.Format("RadDock{0}", state.UniqueName);  
            dock.ApplyState(state);  
            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.DockMode = DockMode.Docked;  
            dock.UniqueName = Guid.NewGuid().ToString();  
            dock.ID = string.Format("RadDock{0}", dock.UniqueName);  
            dock.Title = title;  
            dock.Text = string.Format("Added at {0}", DateTime.Now);  
            dock.Width = Unit.Pixel(300);  
     
            dock.Commands.Add(new DockCloseCommand());  
            dock.Commands.Add(new DockExpandCollapseCommand());  
     
            return dock;  
        }  
     
       
        protected void RadDockLayout1_LoadDockLayout(object sender, DockLayoutEventArgs e)  
        {  
            //CurrentDockStates = UserDataLoader(AspxHelper.LoginUserAccount.ToString());  
            JavaScriptSerializer serializer = new JavaScriptSerializer();  
            string str = LoadUserData(AspxHelper.LoginUserAccount.ToString());  
            string[] currentDockStates2 = str.Split('|');   
            foreach (string stringState in currentDockStates2)   
            {  
                if (stringState != string.Empty)  
                {  
                    DockState state = serializer.Deserialize<DockState>(stringState);  
                    e.Positions[state.UniqueName] = state.DockZoneID;  
                    e.Indices[state.UniqueName] = state.Index;  
                }  
            }  
        }       
     
              
          
        protected void RadDockLayout1_SaveDockLayout(object sender, DockLayoutEventArgs e)  
        {  
            string dockState;  
            JavaScriptSerializer serializer = new 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();  
            bool ExistOrNot = UserDataChecker(AspxHelper.LoginUserAccount.ToString());  
            SqlConnection conn = new SqlConnection("Data Source=(LOCAL);;Initial Catalog=BPMDB;User ID=sa;Password=16*******");  
            conn.Open();  
            if (ExistOrNot)  
            {  
                //update  
                string SQLStr = "UPDATE index_position SET string='" + dockState + "' WHERE id_user = '" + AspxHelper.LoginUserAccount + "'";  
                SqlCommand cmd3 = new SqlCommand(SQLStr, conn);  
                cmd3.ExecuteNonQuery();  
            }  
            else 
            {  
                //insert  
                string SQLStr = "INSERT INTO index_position(id_user,string)VALUES('" + AspxHelper.LoginUserAccount + "','" + dockState + "')";  
                SqlCommand cmd3 = new SqlCommand(SQLStr, conn);  
                cmd3.ExecuteNonQuery();  
            }  
     
             
        }  
     
        public ArrayList GetZones()  
        {  
            ArrayList zones = new ArrayList();  
            zones.Add(RadDockZone1);  
            zones.Add(RadDockZone2);  
     
            return zones;  
        }  
     
        protected void ButtonAddDock_Click(object sender, EventArgs e)  
        {  
            RadDock dock = CreateRadDock(DroptDownWidget.SelectedItem.Text);  
     
            //find the target zone and add the new dock there  
            RadDockZone dz = (RadDockZone)FindControl(DropDownZone.SelectedItem.Text);  
            dz.Controls.Add(dock);  
     
            CreateSaveStateTrigger(dock);  
     
            //Load the selected widget in the RadDock control  
            dock.Tag = DroptDownWidget.SelectedValue;  
            LoadWidget(dock);  
        }  
        protected void ButtonPostBack_Click(object sender, EventArgs e)  
        {  
             
        }  
        protected void ButtonClear_Click(object sender, EventArgs e)  
        {  
             
        }  
         
    }  
     
  2. Pero
    Admin
    Pero avatar
    1156 posts

    Posted 12 Oct 2009 Link to this post

    Hello,

    We have a sample project that saves the state of dynamically created RadDocks in a DataBase. Please find it attached to this thread.


    All the best,
    Pero
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. ghou ghiau-jsui
    ghou ghiau-jsui avatar
    2 posts
    Member since:
    Oct 2009

    Posted 12 Oct 2009 Link to this post

    Thanks alot !

    I'll try it!
  5. vivek shukla
    vivek shukla avatar
    3 posts
    Member since:
    Dec 2009

    Posted 14 Dec 2009 Link to this post

    Plz help me

    I m using this code for save and load dock
    all the docks arranged by index but collapse and close not working.
    mean if i close the the dock or collapsed the dock and do post back all the docks are coming and  collapsed is false.

    i m also trying code from telerik CHM for load and save dock but result is same .

    protected void RadDockLayout1_SaveDockLayout(object sender, DockLayoutEventArgs e)
            {
                //if (IsPostBack)
                //{
                UserPreferences UserPre = DataRepository.UserPreferencesProvider.GetByUserId((long)Session["CurrentUserID"]);

                List<DockState> stateList = ((RadDockLayout)sender).GetRegisteredDocksState();
                StringBuilder serializedList = new StringBuilder();
                for (int i = 0; i < stateList.Count; i++)
                {
                    serializedList.Append(stateList[i].ToString());
                    serializedList.Append("|");
                }

                if (UserPre != null)
                {
                    UserPre.DashBoard = serializedList.ToString();
                    DataRepository.UserPreferencesProvider.Update(UserPre);
                }
                else
                {
                    UserPreferences InsertUser = UserPreferences.CreateUserPreferences((long)Session["CurrentUserID"], serializedList.ToString());
                    DataRepository.UserPreferencesProvider.Insert(InsertUser);
                }
                //}
            }

    protected void RadDockLayout1_LoadDockLayout(object sender, DockLayoutEventArgs e)
            {
                UserPreferences UserPre = DataRepository.UserPreferencesProvider.GetByUserId((long)Session["CurrentUserID"]);
                if (UserPre != null)
                {
                    string serializedList = UserPre.DashBoard;
                    if (serializedList != null)
                    {
                        string[] states = serializedList.Split('|');
                        for (int i = 0; i < states.Length; i++)
                        {
                            DockState state = DockState.Deserialize(states[i]);

                            e.Positions[state.UniqueName] = state.DockZoneID;
                            e.Indices[state.UniqueName] = state.Index;
                        }
                    }
                }
                //foreach (DockState state in CurrentDockStates)
                //{
                //    e.Positions[state.UniqueName] = state.DockZoneID;
                //    e.Indices[state.UniqueName] = state.Index;
                //}
            }  






    protected void RadDockLayout1_LoadDockLayout(object sender, DockLayoutEventArgs e)
            {
                UserPreferences UserPre = DataRepository.UserPreferencesProvider.GetByUserId((long)Session["CurrentUserID"]);
                if (UserPre != null)
                {
                    string serializedList = UserPre.DashBoard;
                    if (serializedList != null)
                    {
                        string[] states = serializedList.Split('|');
                        for (int i = 0; i < states.Length; i++)
                        {
                            DockState state = DockState.Deserialize(states[i]);

                            e.Positions[state.UniqueName] = state.DockZoneID;
                            e.Indices[state.UniqueName] = state.Index;
                        }
                    }
                }
                //foreach (DockState state in CurrentDockStates)
                //{
                //    e.Positions[state.UniqueName] = state.DockZoneID;
                //    e.Indices[state.UniqueName] = state.Index;
                //}
            }  


    aspx ============================================

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Testproject.WebForm2" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        
        <form id="form1" runat="server">
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server" EnablePageMethods="true">
        </telerik:RadScriptManager>
        <div>
            
            <asp:UpdatePanel runat="server" ID="UpdatePanel2" ChildrenAsTriggers="false" UpdateMode="Conditional">
                <ContentTemplate>
                    <telerik:RadDockLayout ID="RadDockLayout1" runat="server" OnLoadDockLayout="RadDockLayout1_LoadDockLayout"
                        OnSaveDockLayout="RadDockLayout1_SaveDockLayout">
                        <telerik:RadDockZone ID="RadDockZone1" runat="server" BorderStyle="none">
                            <telerik:RadDock ID="dockAccount" runat="server" DockMode="Docked" Title="My Account"
                                Style="margin: 10px 10px 10px 0px" AutoPostBack="True" CommandsAutoPostBack="True">
                                <ContentTemplate>
                                    <div style="padding: 10px">
                                        <telerik:RadAjaxPanel ID="RadPanelAccount" runat="server">
                                            <asp:Button Text="Update" runat="server" ID="btnUpdateAccount" CssClass="Invisible" />
                                        </telerik:RadAjaxPanel>
                                    </div>
                                </ContentTemplate>
                            </telerik:RadDock>
                            <telerik:RadDock ID="dockMyProjects" runat="server" DockMode="Docked" Title="My Projects"
                                Style="margin: 10px 10px 10px 0px" AutoPostBack="True" CommandsAutoPostBack="True">
                                <ContentTemplate>
                                    <div style="padding: 10px">
                                        <telerik:RadAjaxPanel ID="RadPanelProjectList" runat="server">
                                            <asp:Button Text="Update" runat="server" ID="btnUpdateProjects"
                                                CssClass="Invisible" />
                                        </telerik:RadAjaxPanel>
                                    </div>
                                </ContentTemplate>
                            </telerik:RadDock>
                            <telerik:RadDock ID="dockMyTasks" runat="server" DockMode="Docked" Title="My Tasks"
                                BackColor="white" Style="margin: 10px 10px 10px 0px" AutoPostBack="True" CommandsAutoPostBack="True">
                                <ContentTemplate>
                                    <div style="padding: 10px">
                                        <telerik:RadAjaxPanel ID="RadPanelTaskList" runat="server">
                                            <asp:Button Text="Update" runat="server" ID="btnMyTask" CssClass="Invisible"  />
                                        </telerik:RadAjaxPanel>
                                    </div>
                                </ContentTemplate>
                            </telerik:RadDock>
                            <telerik:RadDock ID="docCompletedTasks" runat="server" DockMode="Docked" Title="Recently Completed Tasks"
                                BackColor="white" Style="margin: 10px 10px 10px 0px" CommandsAutoPostBack="True"
                                AutoPostBack="True">
                                <ContentTemplate>
                                    <div style="padding: 10px">
                                        <telerik:RadAjaxPanel ID="RadPanelCompletedTask" runat="server">
                                            <asp:Button Text="update" runat="server" ID="btnUpdateRecentCompleted"
                                                CssClass="Invisible" />
                                        </telerik:RadAjaxPanel>
                                    </div>
                                </ContentTemplate>
                            </telerik:RadDock>
                            <telerik:RadDock ID="docRecentQueries" runat="server" DockMode="Docked" Title="Recent Queries"
                                BackColor="White" Style="margin: 10px 10px 10px 0px" CommandsAutoPostBack="True"
                                AutoPostBack="True">
                                <ContentTemplate>
                                    <div style="padding: 10px">
                                        <telerik:RadAjaxPanel ID="RadPanelRecentQurey" runat="server">
                                            <asp:Button Text="Update" runat="server" ID="btnUpdateRecentQueries" CssClass="Invisible"
                                                />
                                        </telerik:RadAjaxPanel>
                                    </div>
                                </ContentTemplate>
                            </telerik:RadDock>
                        </telerik:RadDockZone>
                    </telerik:RadDockLayout>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
        </form>
    </body>
    </html>






  6. Pero
    Admin
    Pero avatar
    1156 posts

    Posted 16 Dec 2009 Link to this post

    Hi Vivek,

    The problem is caused by the fact that you are never applying the DockState from the DataBase to the respective RadDock (i.e. you are never calling the dock.ApplyState(DockState)). You need to call this method in order for the state to be applied correctly. The easiest way to invoke this method is in the LoadDockLayout event handler.

    protected void RadDockLayout1_LoadDockLayout(object sender, DockLayoutEventArgs e)
    {
        UserPreferences UserPre = DataRepository.UserPreferencesProvider.GetByUserId((long)Session["CurrentUserID"]);
        if (UserPre != null)
        {
            string serializedList = UserPre.DashBoard;
            if (serializedList != null)
            {
                string[] states = serializedList.Split('|');
                for (int i = 0; i < states.Length; i++)
                {
                    DockState state = DockState.Deserialize(states[i]);
     
                    e.Positions[state.UniqueName] = state.DockZoneID;
                    e.Indices[state.UniqueName] = state.Index;
     
                    //Find the respective dock and apply its state
                    RadDock dock = (RadDock)RadDockLayout1.FindControl(state.UniqueName);
                    dock.ApplyState(state);
                }
            }
        }
        //foreach (DockState state in CurrentDockStates)
        //{
        //    e.Positions[state.UniqueName] = state.DockZoneID;
        //    e.Indices[state.UniqueName] = state.Index;
        //}
    }


    Kind regards,
    Pero
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017