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

How do you open a popup user control edit form programmatically after inserting a record in a RadGrid?

6 Answers 376 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Justin
Top achievements
Rank 1
Justin asked on 01 Oct 2012, 08:43 PM

i have a radgrid showing header data with a nested detail table.
the header data is inserted/edited via an editForm.
the detail data is inserted/edited via a user control contained in a popup (EditFormType = WebUserControl).

data is submitted from the editForm using an asp.net button whose CommandName property is either "PerformInsert" or "Update".

upon submission of the new header data, i would like for the popup edit control to automatically appear so that detail data can be entered right away instead of having the user expand the newly created header row and then clicking "Add New Detail Record".

how can this be done?

6 Answers, 1 is accepted

Sort by
0
Eyup
Telerik team
answered on 04 Oct 2012, 12:21 PM
Hello Justin,

Could you please try the following approach?
bool isInserted = false;
protected void RadGrid1_ItemInserted(object sender, GridInsertedEventArgs e)
{
    isInserted = true;
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (isInserted)
    {
        GridDataItemCollection items = RadGrid1.MasterTableView.Items as GridDataItemCollection;
        GridDataItem dataItem = items[items.Count - 1] as GridDataItem;
        dataItem.FireCommandEvent("ExpandCollapse", String.Empty);
        GridItem[] nestedViewItems = RadGrid1.MasterTableView.GetItems(GridItemType.NestedView);
        RadGrid detailGrid = nestedViewItems[nestedViewItems.Length - 1].FindControl("RadGrid2") as RadGrid;
        detailGrid.MasterTableView.IsItemInserted = true;
        detailGrid.MasterTableView.Rebind();
    }
}

I hope this will prove helpful. Please give it a try and let me know about the result.

Regards,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Justin
Top achievements
Rank 1
answered on 04 Oct 2012, 02:26 PM
thanks for the response.

i tried the code you provided, and the FindControl function is coming back null. i am not sure what name to substitute the "RadGrid2" that you are passing as the argument.
RadGrid detailGrid = nestedViewItems[nestedViewItems.Length - 1].FindControl("???") as RadGrid;

below is a snippet of the source of the page. perhaps it will help you understand what i need to pass to the FindControl function:
<telerik:RadGrid ID="RadGridProductionOrders" runat="server" AllowSorting="True"
                   Skin="WebBlue" CellSpacing="0" DataSourceID="LinqDataSourceProductionOrders"
                   GridLines="None" ShowFooter="True" ShowStatusBar="True" OnUpdateCommand="RadGridProductionOrders_UpdateCommand"
                   OnInsertCommand="RadGridProductionOrders_InsertCommand" OnItemDataBound="RadGridProductionOrders_ItemDataBound"
                   OnDetailTableDataBind="RadGridProductionOrders_DetailTableDataBind" OnItemCommand="RadGridProductionOrders_ItemCommand"
                   OnItemCreated="RadGridProductionOrders_ItemCreated"
                   oniteminserted="RadGridProductionOrders_ItemInserted"
                   onprerender="RadGridProductionOrders_PreRender">
                   <ClientSettings>
                       <Selecting CellSelectionMode="None"></Selecting>
                   </ClientSettings>
                   <MasterTableView AutoGenerateColumns="False" DataKeyNames="orderNo" DataSourceID="LinqDataSourceProductionOrders"
                       CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add new Production Order"
                       NoMasterRecordsText="No Production Orders to display." EditFormSettings-EditFormType="Template"
                       Caption="Production Orders" HorizontalAlign="Center" Name="MasterTableViewProductionOrderDetails">
                       <DetailTables>
                           <telerik:GridTableView runat="server" DataKeyNames="orderNo,detailKey,itemCode" DataSourceID="LinqDataSourceProductionOrderDetails"
                               EditMode="PopUp" AutoGenerateColumns="False" ShowFooter="True" NoDetailRecordsText="No items in this Production Order."
                               NoMasterRecordsText="No Production Orders to display." Name="ProductionOrderDetails"
                               EditFormSettings-EditFormType="WebUserControl" EditFormSettings-UserControlName="/controls/ProdDetailControl.ascx"
                               EditFormSettings-CaptionDataField="itemCode" EditFormSettings-CaptionFormatString="{0}"
                               BackColor="White" CommandItemDisplay="Top" CommandItemSettings-AddNewRecordText="Add item to Production Order"
                               ForeColor="Black">
                               <DetailTables>
0
Accepted
Eyup
Telerik team
answered on 08 Oct 2012, 02:39 PM
Hello Justin,

The suggested approach was for NestedViewTemplate scenario. In your case, please use the following:
...
 GridItem[] nestedViewItems = RadGrid1.MasterTableView.GetItems(GridItemType.NestedView);
            GridTableView newDetailTable = (nestedViewItems[nestedViewItems.Length - 1] as GridNestedViewItem).NestedTableViews[0] as GridTableView;
            newDetailTable.IsItemInserted = true;
            newDetailTable.Rebind();

That should do the trick.

All the best,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Justin
Top achievements
Rank 1
answered on 09 Oct 2012, 01:45 PM
yes, the detail user control now appears when data is inserted, thank you!

however, because of the sorting on the radgrid the dataItem is getting set to the last row of the grid, which is not necessarily the item that was just inserted, so when i save the data from the form it gets saved to the last order.

how do i retrieve the newly inserted item from the

GridDataItemCollection

collection and from the nested view items?

i found a workaround by creating another global variable to hold the order number of the item just inserted.
i get the dataItem with this code:
dataItem = RadGridProductionOrders.MasterTableView.FindItemByKeyValue("orderNo", insertedProductionNo);

and the newDetailTable with this code:
foreach (GridNestedViewItem gridNestedViewItem in nestedViewItems)               
                    if (gridNestedViewItem.NestedTableViews[0].ParentItem.GetDataKeyValue("orderNo").ToString() == insertedProductionNo)
                        newDetailTable = gridNestedViewItem.NestedTableViews[0] as GridTableView;

so this works, but i was wondering if there was a more elegant way of retrieving these without having to employ a global variable?
0
Eyup
Telerik team
answered on 11 Oct 2012, 04:35 PM
Hello Justin,

Thank you for contacting us.

I am afraid there is no way to get newly inserted item without using a global or a session variable. I am glad that you have found out a solution which works for your current case, however, I suppose this approach could fail when paging is enabled. Here is an alternative approach which works even when paging is enabled:
bool isInserted = false;
string insertedKey = String.Empty;
protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
{
    isInserted = true;
    insertedKey = e.Command.Parameters["@CustomerID"].Value.ToString();
    RadGrid1.MasterTableView.AllowPaging = false;
}
protected void RadGrid1_PreRender(object sender, EventArgs e)
{
    if (isInserted)
    {
        GridDataItem dataItem = RadGrid1.MasterTableView.FindItemByKeyValue("CustomerID", insertedKey) as GridDataItem;
        int pageIndex = dataItem.ItemIndex / RadGrid1.MasterTableView.PageSize + 1;
        int itemIndex = dataItem.ItemIndex % RadGrid1.MasterTableView.PageSize;
 
        RadGrid1.MasterTableView.AllowPaging = true;
        RadGrid1.MasterTableView.Rebind();
 
        GridPagerItem pageritem = RadGrid1.MasterTableView.GetItems(GridItemType.Pager)[0] as GridPagerItem;
        pageritem.FireCommandEvent("Page", pageIndex.ToString());
 
        dataItem = RadGrid1.MasterTableView.Items[itemIndex] as GridDataItem;
        dataItem.FireCommandEvent("ExpandCollapse", String.Empty);
 
        GridItem[] nestedViewItems = RadGrid1.MasterTableView.GetItems(GridItemType.NestedView);
        GridTableView newDetailTable = (nestedViewItems[itemIndex] as GridNestedViewItem).NestedTableViews[0] as GridTableView;
        newDetailTable.IsItemInserted = true;
        newDetailTable.Rebind();
    }
}

I hope this will prove helpful.

All the best,
Eyup
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Justin
Top achievements
Rank 1
answered on 19 Oct 2012, 05:46 PM
eyup,
thanks for your help.
Tags
Grid
Asked by
Justin
Top achievements
Rank 1
Answers by
Eyup
Telerik team
Justin
Top achievements
Rank 1
Share this question
or