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

I need to create dock dynamically on page_Init

1 Answer 131 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Dodi
Top achievements
Rank 1
Dodi asked on 08 Oct 2008, 12:17 PM

I need to create dock dynamically on page_Init, I have some error on aspx page which i mentioned with red color.Also do i need to change code in aspx.cs page



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="docking.aspx.cs" Inherits="docking" %>
<%@ register tagprefix="telerik" namespace="Telerik.Web.UI" assembly="Telerik.Web.UI" %>
<%--<%@ register tagprefix="qsf" tagname="Header" src="~/Common/Header.ascx" %>
<%@ register tagprefix="qsf" tagname="HeadTag" src="~/Common/HeadTag.ascx" %>
<%@ register tagprefix="qsf" tagname="Footer" src="~/Common/Footer.ascx" %>--%>

<!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">
   <%--  <qsf:headtag id="Headtag1" runat="server" />--%>
    <!--[if lte IE 6]>
    <style type="text/css">
    .raddockzone{height:200px}
    </style>
    <![endif]-->

</head>
<body>

  <form id="Form1" method="post" runat="server">
        <%--<qsf:header id="Header1" runat="server" navigationlanguage="c#" />--%>
        <asp:scriptmanager id="ScriptManager" runat="server" />
        <div class="module">
            <%--Select Module: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;--%>
            <%-- <br/>Select Docking Zone:
            <asp:dropdownlist id="DropDownZone" runat="server" datasource="<%#GetZones() %>"
                datatextfield="ID" datavaluefield="ClientID" width="150">
            </asp:dropdownlist>--%>

            <asp:button runat="server"  id="ButtonAddDock" text="Add Dock"
                onclick="ButtonAddDock_Click"  />
                <asp:button runat="server"  id="loadDefaultDock" text="Load Default Dock"
                 onclick="loadDefaultDock_Click"
                  />
        </div>
        <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:15px;background: #f5f4e8;">
            </telerik:raddockzone>
            <telerik:raddockzone runat="server" id="RadDockZone2" width="300" MinHeight="200" style="background: #d5f0fa;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>
                    <asp:updatepanel runat="server" id="UpdatePanel2">
                   <triggers>
                        <asp:asyncpostbacktrigger controlid="loadDefaultDock" eventname="Click" />
                   </triggers>
                   </asp:updatepanel>
                 <asp:updatepanel runat="server" id="UpdatePanel3">
                   <triggers>
                        <asp:asyncpostbacktrigger controlid="loadDefaultDock" eventname="Page_Init" />
                   </triggers>
                   </asp:updatepanel>

            </div>
        </telerik:raddocklayout>
 
      <%--  <qsf:footer runat="server" id="Footer1" showcodeviewer="true" />--%>

    </form>
</body>
</html>






This is my code



using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using System.Collections.Generic;
using SocialTerrain.Web.Lob;
using SocialTerrain.Web.Lob.Entity;
using System.Web.Script.Serialization;
using System.Data.SqlClient;
using SocialTerrain.Web.Dal;
using System.Xml.Serialization;
using System.IO;

 

public partial class docking : System.Web.UI.Page
{
    STDocksMasterCollection stDocksMasterCollectionObj = new STDocksMasterCollection();
    STDocksMaster stDocksMasterObj = new STDocksMaster();
    STDocksMasterRoot stDocksMasterRootObj = new STDocksMasterRoot();
   

    private List<DockState> CurrentDockStates
    {
        get
        {
            //Store the info about the added docks in the session. For real life
            // applications we recommend using database or other storage medium
            // for persisting this information.

            List<DockState> _currentDockStates = new List<DockState>();

           _currentDockStates = (List<DockState>)Session["DockStates"];
            if (Object.Equals(_currentDockStates, null))
            {
                _currentDockStates = new List<DockState>();
               Session["DockStates"] = _currentDockStates;
            }
         
            return _currentDockStates;
        
        }
        set
        {
            Session["DockStates"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //DroptDownWidget.Visible = false;

   
      
   }
    

        protected void Page_Init(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Session["UserId"] = 62;
                int userId = int.Parse(Session["UserId"].ToString());
                STDocksMaster sTDocksMasterobj = stDocksMasterRootObj.GetDockState(userId);
                string dbDockState = sTDocksMasterobj.DocksState;
                if (dbDockState != null)
                {
                    object obj = null;
                    XmlSerializer xs = new XmlSerializer(typeof(List<DockState>));
                    obj = xs.Deserialize(new StringReader(dbDockState));
                    Session["DockStates"] = (List<DockState>)obj;
                }
               int available= stDocksMasterRootObj.CheckDocksUserAvailable(userId);
              if (sTDocksMasterobj.Status == false && Session["Status"] == null && available==1)
                {
                    CreateDynamicDock();
                    Session["Status"] = 1;
                }
            }
            //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);
                    //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)
        {
            //Save the dock state in the session. This will enable us
            // to recreate the dock in the next Page_Init.
            Session["UserId"] = 62;
            int userId = int.Parse(Session["UserId"].ToString());
            //stDocksMasterRootObj.DeleteDocksUser(userId);
            XmlSerializer ser = new XmlSerializer(typeof(List<DockState>));
            System.IO.StringWriter writer = new System.IO.StringWriter();
            ser.Serialize(writer, this.RadDockLayout1.GetRegisteredDocksState());
            string vv = writer.ToString();
            stDocksMasterRootObj.InsertDocksUser(userId, vv);
            STDocksMaster sTDocksMasterobj = stDocksMasterRootObj.GetDockState(userId);
            string dbDockState = sTDocksMasterobj.DocksState;
            object obj = null;
            XmlSerializer xs = new XmlSerializer(typeof(List<DockState>));
            obj = xs.Deserialize(new StringReader(dbDockState));
            Session["DockStates"] = (List<DockState>)obj;
           //// CurrentDockStates = (List<DockState>)Session["DockStates"];

        }

     

        private RadDock CreateRadDockFromState(DockState state)
        {
            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()
        {
          
            int docksCount = CurrentDockStates.Count;
         
            RadDock dock = new RadDock();
            dock.UniqueName = Guid.NewGuid().ToString();
            dock.ID = string.Format("RadDock{0}", dock.UniqueName);

            dock.Title = "";
          
            dock.Text = string.Format("Added at {0}", DateTime.Now);
            dock.Width = Unit.Pixel(300);
            dock.CssClass="margin-bottom:3px !important";
           // stDocksMasterRootObj.InsertDocksMaster(dock.UniqueName, true);

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

        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)
        {
            string _zone;
            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);

                int countzone1 = RadDockZone1.Docks.Count;
                int countzone2 = RadDockZone2.Docks.Count;
                if (countzone1 <= countzone2)
                {
                    _zone = "RadDockZone1";
                }
                else
                    _zone = "RadDockZone2";

            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, _zone),
            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
            //DroptDownWidget.Visible = true;
            dock.Tag = "Members/UserControl/DockUserControl.ascx";
           LoadWidget(dock);

        }

        protected void CreateDynamicDock()
        {

            RadDockZone1.Docks.Clear();
            RadDockZone2.Docks.Clear();
            STDocksMasterRoot sTDocksMasterRootObj = new STDocksMasterRoot();
            STDocksMasterCollection sTDocksMasterCollectionObj = sTDocksMasterRootObj.GetAllDocksName();
            string _zone;

            foreach (STDocksMaster dockName in sTDocksMasterCollectionObj)
            {
                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.

                UpdatePanel3.ContentTemplateContainer.Controls.Add(dock);

                if (ViewState["i"] == null)
                {
                    ViewState["i"] = 1;
                    _zone = "RadDockZone1";
                }
                else
                {
                    ViewState.Remove("i");
                    _zone = "RadDockZone2";
                }
              

                ScriptManager.RegisterStartupScript(

              dock,
              this.GetType(),
              "AddDock" + dock.ClientID,
              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, _zone),
              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);
                if (dockName.DocksName == "Blogs")
                {
                    dock.Tag = "Members/UserControl/BlogUserControl.ascx";
                    dock.Title = "Blogs";
                }
                if (dockName.DocksName == "Videos")
                {
                    dock.Tag = "Members/UserControl/VideoUserControl.ascx";
                    dock.Title = "Videos";
                }
                //Load the selected widget in the RadDock control
                //DroptDownWidget.Visible = true;

                LoadWidget(dock);

            }
        }

        protected void loadDefaultDock_Click(object sender, EventArgs e)
        {
            Session["UserId"] = 62;
            int userId = int.Parse(Session["UserId"].ToString());
            stDocksMasterRootObj.DeleteDocksUser(userId);
            //int countzone1 = RadDockZone1.Docks.Count;
            //int countzone2 = RadDockZone2.Docks.Count;
            //for (int i = countzone1 - 1; i > -1; i--)
            //{
            //    RadDockZone1.Docks.Remove(RadDockZone1.Docks[i]);
            //}
            //for (int j = countzone2 - 1; j > -1; j--)
            //{
            //    RadDockZone2.Docks.Remove(RadDockZone2.Docks[j]);
            //}
            RadDockZone1.Docks.Clear();
            RadDockZone2.Docks.Clear();
            STDocksMasterRoot sTDocksMasterRootObj = new STDocksMasterRoot();
            STDocksMasterCollection sTDocksMasterCollectionObj = sTDocksMasterRootObj.GetAllDocksName();
            string _zone;

            foreach (STDocksMaster dockName in sTDocksMasterCollectionObj)
            {
                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.

                UpdatePanel2.ContentTemplateContainer.Controls.Add(dock);

                if (ViewState["i"] == null)
                {
                     ViewState["i"] = 1;
                    _zone = "RadDockZone1";
                }
                else
                {
                     ViewState.Remove("i");
                    _zone = "RadDockZone2";
                }
           
                ScriptManager.RegisterStartupScript(
                     //UpdatePanel1,
                     //typeof(RadDock),
                     //"DockIntoZone" + dock.ClientID,
              dock,
              this.GetType(),
              "AddDock" + dock.ClientID,
              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, _zone),
              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);
                if (dockName.DocksName == "Blogs")
                {
                    dock.Tag = "Members/UserControl/BlogUserControl.ascx";
                    dock.Title = "Blogs";
                }
                if (dockName.DocksName == "Videos")
                {
                    dock.Tag = "Members/UserControl/VideoUserControl.ascx";
                    dock.Title = "Videos";
                }
                //Load the selected widget in the RadDock control
                //DroptDownWidget.Visible = true;

                LoadWidget(dock);

            }
        }
       
}

 

1 Answer, 1 is accepted

Sort by
0
Obi-Wan Kenobi
Top achievements
Rank 1
answered on 10 Oct 2008, 02:44 PM
  <asp:asyncpostbacktrigger controlid="loadDefaultDock" eventname="Page_Init" />


loadDefaultDock is a Button....So eventname should be click
Tags
Dock
Asked by
Dodi
Top achievements
Rank 1
Answers by
Obi-Wan Kenobi
Top achievements
Rank 1
Share this question
or