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

Dynamic RadGrid inside NestedViewTemplate Not Firing Events

1 Answer 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
JFoulk
Top achievements
Rank 1
JFoulk asked on 06 Dec 2013, 05:09 PM
The issue is that a RadGrid that is created dynamically in the ItemCommand event of the parent grid is not firing commands correctly.

A RadGrid that is explicitly defined within the NestedViewTemplate works perfectly.

To demonstrate, I've created the following...   RadGridChild is explicitly defined within the "ChildPane" panel in the NestedViewTemplate.  It's events fire just as expected.

<telerik:RadScriptManager ID="ScriptManager1" runat="server" />
<telerik:RadAjaxManager ID="AjaxManager1" runat="server">
    <AjaxSettings>
        <telerik:AjaxSetting AjaxControlID="RadGridMaster">
            <UpdatedControls>
                <telerik:AjaxUpdatedControl ControlID="RadGridMaster" />
            </UpdatedControls>
        </telerik:AjaxSetting>
    </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadGrid ID="RadGridMaster" runat="server" OnNeedDataSource="RadGridMaster_NeedDataSource"
    OnPreRender="RadGridMaster_PreRender" OnItemCommand="RadGridMaster_ItemCommand"
    OnItemCreated="RadGridMaster_ItemCreated">
    <MasterTableView DataKeyNames="ItemID">
        <Columns>
        </Columns>
        <NestedViewSettings DataSourceID="ItemID">
            <ParentTableRelation>
                <telerik:GridRelationFields DetailKeyField="ItemID" MasterKeyField="ItemID" />
            </ParentTableRelation>
        </NestedViewSettings>
        <NestedViewTemplate>
            <asp:Panel ID="ChildPanel" runat="server">
                <telerik:RadGrid ID="RadGridChild" runat="server" OnNeedDataSource="RadGridChild_NeedDataSource"
                    OnItemCommand="RadGridChild_ItemCommand">
                    <MasterTableView DataKeyNames="DetailItemID" CommandItemDisplay="Top">
                        <CommandItemSettings AddNewRecordText="Add Detail" />
                        <Columns></Columns>
                    </MasterTableView>
                </telerik:RadGrid>
            </asp:Panel>
        </NestedViewTemplate>
    </MasterTableView>
</telerik:RadGrid>

Here's the code:
public partial class TestGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void RadGridMaster_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        using (TestDataEntities de = new TestDataEntities())
        {
            (sender as RadGrid).DataSource = de.Table1.ToList();
        }
    }
 
    protected void RadGridMaster_PreRender(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RadGridMaster.MasterTableView.Items[0].Expanded = true;
            var pnl = RadGridMaster.MasterTableView.Items[0].ChildItem.FindControl("ChildPanel") as Panel;
            pnl.Visible = true;
            pnl.Controls.OfType<RadGrid>().FirstOrDefault().Rebind();
        }
    }
 
    protected void RadGridMaster_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == RadGrid.ExpandCollapseCommandName && e.Item is GridDataItem)
        {
            ((GridDataItem)e.Item).ChildItem.FindControl("ChildPanel").Visible =
                !e.Item.Expanded;
            if (!e.Item.Expanded)
            {
                (((GridDataItem)e.Item).ChildItem.FindControl("ChildPanel") as Panel).Controls.OfType<RadGrid>().FirstOrDefault().Rebind();
 
                RadGrid RadGridChild2 = new RadGrid();
                RadGridChild2.ID = "RadGridChild2";
                RadGridChild2.NeedDataSource += RadGridChild2_NeedDataSource;
                RadGridChild2.ItemCommand += RadGridChild2_ItemCommand;
                RadGridChild2.MasterTableView.DataKeyNames = new string[] { "ItemID" };
                RadGridChild2.MasterTableView.CommandItemDisplay = GridCommandItemDisplay.Top;
                RadGridChild2.MasterTableView.CommandItemSettings.AddNewRecordText = "Add for 2";
                RadGridChild2.AutoGenerateColumns = false;
                RadGridChild2.MasterTableView.Columns.Add(new GridBoundColumn() { DataField = "DetailItemID", HeaderText = "Detail ID" });
                RadGridChild2.MasterTableView.Columns.Add(new GridBoundColumn() { DataField = "ItemDetail", HeaderText = "Detail" });
                (((GridDataItem)e.Item).ChildItem.FindControl("ChildPanel") as Panel).Controls.Add(RadGridChild2);
                RadGridChild2.Rebind();
            }
        }
    }
 
    protected void RadGridMaster_ItemCreated(object sender, GridItemEventArgs e)
    {
        if (e.Item is GridNestedViewItem)
        {
            e.Item.FindControl("ChildPanel").Visible = ((GridNestedViewItem)e.Item).ParentItem.Expanded;
        }
    }
 
    protected void RadGridChild_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid rg = sender as RadGrid;
        var temp = Convert.ToInt32((rg.NamingContainer as GridNestedViewItem).ParentItem.GetDataKeyValue("ItemID"));
 
        using (TestDataEntities de = new TestDataEntities())
        {
            rg.DataSource = de.Table2.Where(x => x.ItemID == temp).ToList();
        }
    }
 
    protected void RadGridChild_ItemCommand(object sender, GridCommandEventArgs e)
    {
        RadAjaxManager.GetCurrent(this.Page).Alert("Adding!");
    }
 
    protected void RadGridChild2_ItemCommand(object sender, GridCommandEventArgs e)
    {
        RadAjaxManager.GetCurrent(this.Page).Alert("Adding for 2!");
    }
 
    protected void RadGridChild2_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        RadGrid rg = sender as RadGrid;
        rg.AutoGenerateColumns = false;
        rg.MasterTableView.Columns.Clear();
        rg.MasterTableView.Columns.Add(new GridBoundColumn() { DataField = "DetailItemID", HeaderText = "Detail ID" });
        rg.MasterTableView.Columns.Add(new GridBoundColumn() { DataField = "ItemDetail", HeaderText = "Detail" });
        var temp = Convert.ToInt32((rg.NamingContainer as GridNestedViewItem).ParentItem.GetDataKeyValue("ItemID"));
 
        using (TestDataEntities de = new TestDataEntities())
        {
            rg.DataSource = de.Table2.Where(x => x.ItemID == temp).ToList();
        }
    }
}

When I click on the "Add Record" button on the first child grid, I get the expected alert box.
When I click on the "Add Record" button on the second child grid (dynamic), It disappears and I get nothing.

Any help is appreciated.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 09 Dec 2013, 06:09 AM
Hi ,

Please try the following code snippet to bind the RadGrid inside NestedViewTemplate.

C#:
protected void RadGridMaster_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.ExpandCollapseCommandName && !e.Item.Expanded)
    {
        GridDataItem parentItem = e.Item as GridDataItem;
        RadGrid rg = parentItem.ChildItem.FindControl("RadGridChild") as RadGrid;
        rg.Rebind();
    }
}
protected void RadGridChild_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    GridDataItem parentItem = ((sender as RadGrid).NamingContainer as GridNestedViewItem).ParentItem as GridDataItem;
    string datakeyvalue = parentItem.GetDataKeyValue("ItemID").ToString();
    //You code to populate RadGrid
}

Thanks,
Princy
Tags
Grid
Asked by
JFoulk
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or