Prevent duplicate dock

2 posts, 0 answers
  1. Golu
    Golu avatar
    41 posts
    Member since:
    Dec 2008

    Posted 14 May 2009 Link to this post

    Dear sir,

    I am dock dynamically and it contain user control.

    I am creating a Dashboard and  dont wants to create duplicate user control on that.
    once we load a user control and if we again load that perticular control then it shows a msg "Already loaded" . how is it possible.

    when I closed a dock it also contain the value of that user control..
    I am sending u a code and plz tell me where is the mistake..

    aspx. page........

    <

     

    table width="100%" border="0" cellpadding="1" cellspacing="0" class="aspxMainTable">  

     

     

     

    <tr>

     

     

     

    <th class="tdMainHeader1">

     

     

     

    <img alt="Customer" src="Images/dashboard.gif" />

     

     

    Dashboard

    &nbsp;&nbsp;&nbsp;Objects &nbsp;

     

     

     

    <telerik:RadComboBox ID="cmbSelectDock" runat="server" Skin="Office2007" >

     

     

     

    <Items>

     

     

     

    <telerik:RadComboBoxItem Text="Case Summary" Value="CaseSummary" />

     

     

     

    <telerik:RadComboBoxItem Text="Task Summary" Value="TaskSummary" />

     

     

     

    <telerik:RadComboBoxItem Text="Pending Tasks" Value="MyTask" />

     

     

     

    <telerik:RadComboBoxItem Text="Pending Cases" Value="MyCase" />

     

     

     

    <telerik:RadComboBoxItem Text="Overdue Cases" Value="OverdueCases" />

     

     

     

    </Items>

     

     

     

    </telerik:RadComboBox>

     

     

     

    &nbsp;

     

     

     

    <telerik:RadComboBox ID="cmbZone" runat="server" Skin="Office2007" DataSource="<%#GetZones() %>"

     

     

     

    DataTextField="ID" DataValueField="ClientID">

     

     

     

    </telerik:RadComboBox>

     

     

     

    &nbsp;

     

     

     

    <asp:Button ID="btnAddDock" runat="server" Text="Add" Width="100px" OnClientClick="return ValidateSave();"

     

     

     

    OnClick="btnAddDock_Click" />

     

     

     

    </th>

     

     

     

    </tr>

     

     

     

    <tr>

     

     

     

    <td colspan="5">

     

     

     

    <asp:UpdatePanel runat="server" ID="UpdatePanel1">

     

     

     

    <ContentTemplate>

     

     

     

    <telerik:RadDockLayout runat="server" ID="rdDockLayout" OnSaveDockLayout="rdDockLayout_SaveDockLayout"

     

     

     

    Skin="Office2007" OnLoadDockLayout="rdDockLayout_LoadDockLayout">

     

     

     

    <table id="TableLayout" border="0" width="100%">

     

     

     

    <tr>

     

     

     

    <td style="width: 50%;" align="left" valign="top">

     

     

     

    <table width="100%" border="0">

     

     

     

    <tr>

     

     

     

    <td>

     

     

     

    <telerik:RadDockZone runat="server" ID="RadDockZone1" Width="360" MinHeight="250"

     

     

     

    Orientation="Vertical" Style="background: #f5f4e8; float: right;" Skin="Office2007"

     

     

     

    FitDocks="true">

     

     

     

    </telerik:RadDockZone>

     

     

     

    </td>

     

     

    <%

    --Style="float: left; margin-right: 5px; background: #f5f4e8;"--%>

     

     

     

    </tr>

     

     

     

    </table>

     

     

     

    </td>

     

     

     

    <td style="width: 50%;" align="left" valign="top">

     

     

     

    <table width="100%" border="0">

     

     

     

    <tr>

     

     

     

    <td>

     

     

     

    <telerik:RadDockZone runat="server" ID="RadDockZone2" Width="360" MinHeight="250"

     

     

     

    Orientation="Horizontal" Style="background: #d5f0fa; float: left;" FitDocks="true"

     

     

     

    Skin="Office2007">

     

     

     

    </telerik:RadDockZone>

     

     

     

    </td>

     

     

    <%

    --Style="background: #d5f0fa; float: left;" --%>

     

     

     

    </tr>

     

     

     

    </table>

     

     

     

    </td>

     

     

     

    </tr>

     

     

     

    </table>

     

     

     

    </telerik:RadDockLayout>

     

     

     

    </ContentTemplate>

     

     

     

    <Triggers>

     

     

     

    <asp:AsyncPostBackTrigger ControlID="btnAddDock" EventName="Click" />

     

     

     

    </Triggers>

     

     

     

    </asp:UpdatePanel>

     

     

     

    </td>

     

     

     

    </tr>

     

     

     

    <tr>

     

     

     

    <td>

     

     

     

    <asp:Label ID="lblMessage" runat="server"></asp:Label>

     

     

     

    </td>

     

     

     

    </tr>

     

     

     

    </table>

     

     


    aspx.cs page.........

    DockStateWS

     

    objDockStateWS;

     

     

     

    DataSet dsDataSet;

     

     

     

    List<DockState> stateList;

     

     

     

    StringBuilder serializedList = new StringBuilder();

     

     

     

    private bool _dockStateCleared = false;

     

     

     

    protected void Page_InIt(object sender, EventArgs e)

     

    {

    System.Web.Script.Serialization.

     

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

     

    dsDataSet =

     

    new DataSet();

     

     

     

    DataTable dt1 = new DataTable();

     

     

     

    try

     

     

    {

    objDockStateWS =

    new DockStateWS();

     

    dsDataSet = objDockStateWS.Fetch_Dock_State(0, 2);

     

     

    if (dsDataSet.Tables["DockState"].Rows.Count > 0)

     

    {

    dockState =

     

    Convert.ToString(dsDataSet.Tables["DockState"].Rows[0]["dock_state"]);

     

     

     

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

     

    rdDockLayout.Controls.Add(dock);

     

     

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

     

     

    CreateSaveStateTrigger(dock);

    LoadWidget(dock);

    }

    }

    }

    }

     

    catch (Exception ex)

     

    {

     

     

    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "errMsg", "javascript:alert(\"Error:-Method -> 'Page_Init' \\n Description:- \\n" + ex.Message + "\");", true);

     

     

     

    return;

     

    }

    }

     

     

    protected void Page_Load(object sender, EventArgs e)

     

    {

    Session[

     

    "LoggedInContactID"] = TB_nEmployeeID;

     

    Session[

     

    "LoggedInContactDeptID"] = TB_nDepartmentID;

     

    Session[

     

    "LoggedInContactDesigLevel"] = TB_nDesignationLevel;

     

    Session[

     

    "LoggedInContactAccountID"] = TB_nAccountID;

     

     

     

     

     

    if (!Page.IsPostBack)

     

    {

    cmbZone.DataBind();

    }

    }

     

     

    private List<DockState> CurrentDockStates

     

    {

     

     

    get

     

     

    {

     

    JavaScriptSerializer serializer = new JavaScriptSerializer();

     

     

     

    List<DockState> _currentDockStates = (List<DockState>)Session["CurrentDockStatesMyPortal"];

     

     

     

    if (Object.Equals(_currentDockStates, null))

     

    {

    _currentDockStates =

     

    new List<DockState>();

     

    Session[

     

    "CurrentDockStatesMyPortal"] = _currentDockStates;

     

    }

     

     

    return _currentDockStates;

     

    }

     

     

    set

     

     

    {

    Session[

    "CurrentDockStatesMyPortal"] = value;

     

    }

    }

     

     

    public ArrayList GetZones()

     

    {

     

     

    ArrayList zones = new ArrayList();

     

    zones.Add(RadDockZone1);

    zones.Add(RadDockZone2);

     

     

    return zones;

     

    }

     

     

    protected void rdDockLayout_LoadDockLayout(object sender, DockLayoutEventArgs e)

     

    {

     

     

    DataTable dt = new DataTable();

     

    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

     

     

     

    try

     

     

    {

    objDockStateWS =

    new DockStateWS();

     

    dsDataSet =

     

    new DataSet();

     

    dsDataSet = objDockStateWS.Fetch_Dock_State(0, 2);

     

     

    if (dsDataSet.Tables["DockState"].Rows.Count > 0)

     

    {

    dockState =

     

    Convert.ToString(dsDataSet.Tables["DockState"].Rows[0]["dock_state"]);

     

     

     

    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;

    }

    }

    }

    }

     

     

    catch (Exception ex)

     

    {

    LogMessage(ex, 1);

    }

    }

     

     

     

    protected void rdDockLayout_SaveDockLayout(object sender, DockLayoutEventArgs e)

     

    {

     

     

    try

     

     

    {

     

    //--Save Dock states in Database

     

     

    System.Web.Script.Serialization.

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

     

    stateList = rdDockLayout.GetRegisteredDocksState();

     

     

    while (i < stateList.Count)

     

    {

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

    serializedList.Append(

     

    "|");

     

    i++;

    }

    dockState = serializedList.ToString();

     

     

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

     

    {

    objDockStateWS =

     

    new DockStateWS();

     

    dsDataSet =

     

    new DataSet();

     

    dsDataSet = objDockStateWS.Save_Dock_State(0, 2, dockState);

    }

    }

     

     

    catch (Exception ex)

     

    {

    LogMessage(ex, 1);

    }

    }

     

     

    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;

     

    }

     

     

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

     

    CurrentDockStates.Clear();

    _dockStateCleared =

     

    true;

     

    }

    }

     

     

    private RadDock CreateRadDock()

     

    {

     

     

    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 = cmbSelectDock.SelectedItem.Text;

    dock.Width =

     

    Unit.Pixel(360);

     

    dock.Height =

     

    Unit.Pixel(250);

     

    dock.Resizable =

     

    true;

     

    dock.Commands.Add(

     

    new DockCloseCommand());

     

    dock.Commands.Add(

     

    new DockExpandCollapseCommand());

     

     

     

    return dock;

     

    }

     

     

    private void CreateSaveStateTrigger(RadDock dock)

     

    {

    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 btnAddDock_Click(object sender, EventArgs e)

     

    {

    System.Web.Script.Serialization.

     

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

     

    objDockStateWS =

     

    new DockStateWS();

     

    dsDataSet =

     

    new DataSet();

     

     

     

    string strControlID = string.Empty;

     

     

     

    string strDockTagName = string.Empty;

     

     

     

    try

     

     

    {

    dsDataSet = objDockStateWS.Fetch_Dock_State(0, 2);

     

    if (dsDataSet.Tables["DockState"].Rows.Count > 0)

     

    {

    dockState =

     

    Convert.ToString(dsDataSet.Tables["DockState"].Rows[0]["dock_state"]);

     

     

     

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

     

     

     

    foreach (string stringState in currentDockStates)

     

    {

     

     

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

     

    {

     

     

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

     

     

     

    if (cmbSelectDock.SelectedItem.Text == state.Title)

     

    {

     

     

    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "AlreadyExist", "javascript:alert('Object already exist');", true);

     

     

     

    return;

     

    }

    }

    }

    }

     

     

    if (cmbSelectDock.SelectedValue == "CaseSummary")

     

    {

     

     

    UserControls_Case_Summary_CustCateg_Chart uclCaseSummary_Chart = (UserControls_Case_Summary_CustCateg_Chart)Page.LoadControl("~/UserControls/Case_Summary_CustCateg_Chart.ascx");

     

    strControlID =

     

    "uclCaseSummary_Chart" + TB_nUserID;

     

    uclCaseSummary_Chart.GetCaseSummary();

    strDockTag =

     

    "UserControls/Case_Summary_CustCateg_Chart.ascx";

     

    }

     

     

    else if (cmbSelectDock.SelectedValue == "TaskSummary")

     

    {

     

     

    UserControls_Task_Summary_Chart uclTaskSummary_Chart = (UserControls_Task_Summary_Chart)Page.LoadControl("~/UserControls/Task_Summary_Chart.ascx");

     

    strControlID =

     

    "uclTaskSummary_Chart" + TB_nUserID;

     

    uclTaskSummary_Chart.GetTaskSummary();

    strDockTag =

     

    "~/UserControls/Task_Summary_Chart.ascx";

     

    }

     

     

    else if (cmbSelectDock.SelectedValue == "MyTask")

     

    {

     

     

    UserControls_MyTask uclMyTask = (UserControls_MyTask)Page.LoadControl("~/UserControls/MyTask.ascx");

     

    strControlID =

     

    "uclTask" + TB_nUserID;

     

    uclMyTask.GetTasks();

    strDockTag =

     

    "~/UserControls/MyTask.ascx";

     

    }

     

     

    else if (cmbSelectDock.SelectedValue == "MyCase")

     

    {

     

     

    UserControls_MyCase uclMyCase = (UserControls_MyCase)Page.LoadControl("~/UserControls/MyCase.ascx");

     

    strControlID =

     

    "uclCase" + TB_nUserID;

     

    uclMyCase.GetCases();

    strDockTag =

     

    "~/UserControls/MyCase.ascx";

     

    }

     

     

    else if (cmbSelectDock.SelectedValue == "OverdueCases")

     

    {

     

     

    UserControls_OverdueCases uclOverdueCases = (UserControls_OverdueCases)Page.LoadControl("~/UserControls/OverdueCases.ascx");

     

    strControlID =

     

    "uclOverdueCase" + TB_nUserID;

     

    uclOverdueCases.GetOverDueCases();

    strDockTag =

     

    "~/UserControls/OverdueCases.ascx";

     

    }

    }

     

     

    catch (Exception ex)

     

    {

    LogMessage(ex, 1);

    }

     

     

     

    try

     

     

    {

     

    RadDock dock = CreateRadDock();

     

     

     

    //find the target zone and add the new dock there

     

     

     

    RadDockZone dz = (RadDockZone)FindControl(cmbZone.SelectedItem.Text);

     

     

     

    //dz.Width = Unit.Percentage(50);

     

     

    dz.Controls.Add(dock);

    CreateSaveStateTrigger(dock);

     

    //Load the selected widget in the RadDock control

     

     

     

    //dock.Tag = cmbSelectDock.SelectedValue;

     

     

    dock.Tag = strDockTag;

    LoadWidget(dock);

    }

     

    catch (Exception ex)

     

    {

    LogMessage(ex, 1);

    }

    }

     

     

    void LogMessage(Exception e, Int32 param)

     

    {

     

     

    if (param == 1)

     

    {

    lblMessage.Text = e.Message;

    lblMessage.CssClass =

     

    "error";

     

    }

     

     

    else

     

     

    {

    lblMessage.Text = e.Message;

    lblMessage.CssClass =

    "success";

     

    }

    }

    and also set the height & width in percentange

    thanks and regards

  2. Obi-Wan Kenobi
    Obi-Wan Kenobi  avatar
    460 posts
    Member since:
    Aug 2007

    Posted 15 May 2009 Link to this post

    My suggestion is to use a different approach.
    Add all UserControls into array. If an user control is already added to a RadDock -> remove it from the Array. Bind RadComboBox to this array. If you use this approch user will not see already added usercontrols and it will not be able to add them twice.
Back to Top