Failure: Add RadDock Dynamically from RadGrid button

2 posts, 0 answers
  1. Mc
    Mc avatar
    5 posts
    Member since:
    Jan 2015

    Posted 26 Jan 2015 Link to this post

    I have a RadGrid and I want to use one of the column to dynamically add new RadDock but nothing happened. I created a single asp:Button to test my FederationDetailBtnClick method which add dock correctly but the button from grid failed. Help needed!!!

    aspx file:
    <telerik:RadGrid ID="FederationGrid" runat="server"
            OnNeedDataSource="FederationGrid_NeedDataSource"
            OnItemCreated="FederationGrid_ItemCreated"
            OnItemDataBound="FederationGrid_ItemDataBound">
            <MasterTableView EnableHierarchyExpandAll="true" AutoGenerateColumns="False" TableLayout="Fixed" DataKeyNames="Id">
                <Columns>
                    <telerik:GridBoundColumn DataField="Id" HeaderText="Federation Name" UniqueName="FederationId">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Status" HeaderText="Status" UniqueName="Status">
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn HeaderText="Federation Detail" Text="Detail" UniqueName="FederationDetail" ButtonType="PushButton">
                    </telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
     
        <telerik:RadAjaxPanel ID="RadAjaxPanel" runat="server" LoadingPanelID="RadAjaxLoadingPanel">
            <telerik:RadDockLayout runat="server" ID="RadDockLayout_Federation"
                OnSaveDockLayout="RadDockLayout_SaveDockLayout"
                OnLoadDockLayout="RadDockLayout_LoadDockLayout">
                <telerik:RadDockZone ID="RadDockZone_Federation" runat="server" Orientation="Horizontal">
                </telerik:RadDockZone>
            </telerik:RadDockLayout>
        </telerik:RadAjaxPanel>

    aspx.cs file:
    private List<DockState> CurrentDockStates
            {
                get
                {
                    // Store the info about the added docks in the session.
                    List<DockState> _currentDockStates = (List<DockState>)Session["CurrentDockStatesDeployment"];
                    if (Object.Equals(_currentDockStates, null))
                    {
                        _currentDockStates = new List<DockState>();
                        Session["CurrentDockStatesDeployment"] = _currentDockStates;
                    }
                    return _currentDockStates;
                }
                set
                {
                    Session["CurrentDockStatesDeployment"] = value;
                }
            }
     
            protected void Page_Load(object sender, EventArgs e)
            {
                 
            }
     
            protected void FederationGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
            {
                this.federationList = DataModelRequestHandler.GetFederationsAsync(this.environment);
                FederationGrid.DataSource = this.federationList;
            }
     
            protected void FederationGrid_ItemCreated(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem dataitem = (GridDataItem)e.Item;
                    Button btn = (Button)dataitem["FederationDetail"].Controls[0];
                    btn.Click += new EventHandler(FederationDetailBtnClick);
                }
            }
     
            protected void FederationGrid_ItemDataBound(object sender, GridItemEventArgs e)
            {
                if (e.Item is GridDataItem)
                {
                    GridDataItem dataitem = (GridDataItem)e.Item;
                    Button btn = (Button)dataitem["FederationDetail"].Controls[0];
                    btn.CommandArgument = (e.Item as GridDataItem)["FederationId"].Text;
                }
            }
     
            protected void RadDockLayout_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 RadDockLayout_SaveDockLayout(object sender, DockLayoutEventArgs e)
            {
                CurrentDockStates = RadDockLayout_Federation.GetRegisteredDocksState();
            }
     
            private void FederationDetailBtnClick(object sender, EventArgs e)
            {
                var btnView = (Button)sender;
                var FederationId = btnView.CommandArgument;
     
                // Create RadDock
                RadDock dock = this.CreateRadDock(FederationId);
                // Dock it on DockZone
                RadDockLayout_Federation.Controls.Add(dock);
                dock.Dock(RadDockZone_Federation);
            }
     
            private RadDock CreateRadDock(string dockTitle)
            {
                RadDock dock = new RadDock();
                dock.DockMode = DockMode.Docked;
                dock.UniqueName = Guid.NewGuid().ToString().Replace("-", "a");
                dock.ID = string.Format("RadDock{0}", dock.UniqueName);
                dock.Title = dockTitle;
     
                dock.Commands.Add(new DockCloseCommand());
                dock.Commands.Add(new DockExpandCollapseCommand());
     
                return dock;
            }

  2. Slav
    Admin
    Slav avatar
    1356 posts

    Posted 29 Jan 2015 Link to this post

    Hi,

    You need to ensure that the FederationDetailBtnClick method, which is an event handler of the click event of the buttons in RadGrid, is property attached. The AutoEventWireup attribute of the Page directive should be set to true for this purpose. You can also set a breakpoint in the FederationDetailBtnClick method and run the page in debug mode to check if the method is executed on each button click.

    Also, if you want to persist the generated docks when a postback occurs, you need to recreate them on Page_Init, before their state is applied. This approach is demonstrated in the following demo: http://demos.telerik.com/aspnet-ajax/dock/examples/dynamicdocks/defaultcs.aspx

    You can find attached a sample page that shows both adding a dock when the button in the grid is clicked and persisting all created docks through postback.

    Regards,
    Slav
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top