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

Failure: Add RadDock Dynamically from RadGrid button

1 Answer 58 Views
Dock
This is a migrated thread and some comments may be shown as answers.
Mc
Top achievements
Rank 1
Mc asked on 27 Jan 2015, 03:16 AM
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;
        }

1 Answer, 1 is accepted

Sort by
0
Slav
Telerik team
answered on 29 Jan 2015, 03:18 PM
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.

 
Tags
Dock
Asked by
Mc
Top achievements
Rank 1
Answers by
Slav
Telerik team
Share this question
or