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

InsertCommand and ItemCommand not firing for grid inside FormTemplate

2 Answers 56 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Stephen
Top achievements
Rank 1
Stephen asked on 19 Jul 2013, 12:39 PM
Hi all,

I have a RadGrid inside the FormTemplate of another RadGrid.  My markup looks like this at the moment:

<telerik:RadGrid ID="CollectionsRadGrid" runat="server"
    OnNeedDataSource="CollectionsRadGrid_NeedDataSource"
    OnItemCommand="CollectionsRadGrid_ItemCommand"
    OnItemCreated="CollectionsRadGrid_ItemCreated">
    <MasterTableView DataKeyNames="CollectionID" AutoGenerateColumns="false" CommandItemDisplay="Top" EditMode="EditForms">
        <CommandItemTemplate>
            <telerik:RadToolBar ID="CollectionsRadToolBar" runat="server" AutoPostBack="true">
                <Items>
                    <telerik:RadToolBarButton Text="Add a new collection" ImageUrl="/Public/Images/icon-add.png" CommandName="InitInsert" />
                </Items>
            </telerik:RadToolBar>
        </CommandItemTemplate>
        <Columns>
            <telerik:GridBoundColumn HeaderText="Reference" DataField="Reference" ReadOnly="true" />
            <telerik:GridTemplateColumn HeaderText="Site">
                <ItemTemplate>
 
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn HeaderText="Collection Date" />
            <telerik:GridTemplateColumn HeaderText="Items">
                <ItemTemplate>
 
                </ItemTemplate>
            </telerik:GridTemplateColumn>
            <telerik:GridTemplateColumn HeaderText="Options">
                <ItemTemplate>
 
                </ItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
        <EditFormSettings EditFormType="Template">
            <FormTemplate>
                <asp:Panel ID="InnerContainerPanel" runat="server" CssClass="innerContainer">
                    <div class="nonTabPage">
                        <telerik:RadToolBar ID="RadToolBar1" runat="server" AutoPostBack="true" Width="100%">
                            <Items>
                                <telerik:RadToolBarButton
                                    Text='<%# (this.GetGridEditFormItem(Container) is GridEditFormInsertItem) ? "Insert" : "Update" %>'
                                    CommandName='<%# (this.GetGridEditFormItem(Container) is GridEditFormInsertItem) ? "PerformInsert" : "Update" %>' />
                                <telerik:RadToolBarButton Text="Cancel" CommandName="Cancel" />
                            </Items>
                        </telerik:RadToolBar>
                        <p>
                            <table cellspacing="0" cellpadding="0" width="100%">
                                <tr>
                                    <td valign="bottom">
                                        <asp:Label runat="server" Text="Site: " />
                                        <telerik:RadComboBox ID="SiteListBox" runat="server" EmptyMessage="Please Select" />
                                    </td>
                                    <td align="right">
                                        <asp:Label runat="server" Text="Collection Date: " />
                                        <asp:Label ID="CollectionDateLabel" runat="server" Text='<%#(string.IsNullOrEmpty(Eval("CollectedDate").ToString()) ? "TBC" : Eval("CollectedDate", "dd/MM/yyyy")) %>' />
                                    </td>
                                </tr>                  
                            </table>
                        </p>
                        <p>
                            <telerik:RadGrid ID="CollectionItemsRadGrid" runat="server"
                                OnItemCommand="CollectionItemsRadGrid_ItemCommand"
                                OnInsertCommand="CollectionItemsRadGrid_InsertCommand"
                                OnNeedDataSource="CollectionItemsRadGrid_NeedDataSource">
                                <MasterTableView DataKeyNames="InItemID" AutoGenerateColumns="false" CommandItemDisplay="Top" EditMode="InPlace">
                                    <CommandItemTemplate>
                                        <telerik:RadToolBar ID="RadToolBar1" runat="server" AutoPostBack="true">
                                            <Items>
                                                <telerik:RadToolBarButton Text="Add a new item"  ImageUrl="/Public/Images/icon-add.png" CommandName="InitInsert" />
                                            </Items>
                                        </telerik:RadToolBar>
                                    </CommandItemTemplate>
                                    <Columns>
                                        <telerik:GridTemplateColumn HeaderText="Item Type">
                                            <ItemTemplate>
                                                <%# Eval("tblItemType.Description") %>
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <telerik:RadComboBox ID="ItemTypeCombo" runat="server"
                                                    EmptyMessage="Please Select" />
                                            </EditItemTemplate>
                                        </telerik:GridTemplateColumn>
                                        <telerik:GridNumericColumn DataField="PlannedQty" HeaderText="Quantity" NumericType="Number" />
                                        <telerik:GridEditCommandColumn ButtonType="ImageButton" UniqueName="EditCommandColumn">
                                            <%--<ItemStyle CssClass="MyImageButton"></ItemStyle>--%>
                                        </telerik:GridEditCommandColumn>
                                        <telerik:GridButtonColumn ConfirmText="Delete this item?" ConfirmDialogType="RadWindow"
                                            ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" Text="Delete"
                                            UniqueName="DeleteColumn">
                                            <%--<ItemStyle HorizontalAlign="Center" CssClass="MyImageButton"></ItemStyle>--%>
                                        </telerik:GridButtonColumn>
                                    </Columns>
                                </MasterTableView>
                            </telerik:RadGrid>
                        </p>
                    </div>
                </asp:Panel>
 
            </FormTemplate>
        </EditFormSettings>
    </MasterTableView>
</telerik:RadGrid>

and my code behind is as follows:
The function at the very bottom (in bold) is where I am having the problem.  It is not being executed.

/// <summary>
/// Gets or sets the quote object from session
/// </summary>
protected tblQuote CurrentQuote
{
    get { return this.Session["CurrentQuote"] as tblQuote; }
    set { this.Session["CurrentQuote"] = value; }
}
 
/// <summary>
/// Gets or sets the current collection object from session
/// </summary>
protected tblCollection CurrentCollection
{
    get { return this.Session["CurrentCollection"] as tblCollection; }
    set { this.Session["CurrentCollection"] = value; }
}
 
#region Collections
 
/// <summary>
/// The collections radgrid need data source event
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event args</param>
protected void CollectionsRadGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    this.CollectionsRadGrid.DataSource = CurrentQuote.tblJob.tblCollection.ToList();
}
 
/// <summary>
/// Collections radgrid item command event
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event args</param>
protected void CollectionsRadGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
    if (e.CommandName == RadGrid.InitInsertCommandName)
    {
        this.CurrentCollection = new tblCollection()
        {
            JobID = this.CurrentQuote.JobID,
            CreateDate = DateTime.Now,
            tblItemsIn = new List<tblItemsIn>()
        };
    }
 
    if (e.CommandName == RadGrid.EditCommandName)
    {
        int collectionId = (int)this.CollectionsRadGrid.MasterTableView.Items[e.Item.ItemIndex].GetDataKeyValue("CollectionID");
        this.CurrentCollection = this.CurrentQuote.tblJob.tblCollection.Single(o => o.CollectionID == collectionId);
    }
}
 
/// <summary>
/// Collections radgrid item created event
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event args</param>
protected void CollectionsRadGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
    {
        RadGrid collectionItemsRadGrid = (RadGrid)e.Item.FindControl("CollectionItemsRadGrid");
        collectionItemsRadGrid.Rebind();
    }
}
 
#endregion
 
#region Collection Items
 
/// <summary>
/// The collection items radgrid need datasource event
/// </summary>
/// <param name="sender">The sender</param>
/// <param name="e">The event args</param>
protected void CollectionItemsRadGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    ((RadGrid)sender).DataSource = this.CurrentCollection.tblItemsIn.ToList();
}
 
protected void CollectionItemsRadGrid_ItemCommand(object sender, GridCommandEventArgs e)
{
}
 
#endregion
 
/// <summary>
/// Gets the grid data item by traversing up the control hierarchy
/// </summary>
/// <param name="sender">The object contained in the FormTemplate</param>
/// <returns>The GridEditFormItem</returns>
protected GridEditFormItem GetGridEditFormItem(Control sender)
{
    Control currentItem = sender;
    while (!(currentItem is GridEditFormItem))
    {
        if (currentItem.Parent == null)
        {
            return null;
        }
 
        currentItem = currentItem.Parent;
    }
 
    return (GridEditFormItem)currentItem;
}
 
protected void CollectionItemsRadGrid_InsertCommand(object sender, GridCommandEventArgs e)
{
    GridEditableItem item = (GridEditableItem)e.Item;
    tblItemsIn newItem = new tblItemsIn();
    item.UpdateValues(newItem);
    this.CurrentCollection.tblItemsIn.Add(newItem);
}

Many thanks

2 Answers, 1 is accepted

Sort by
0
Stephen
Top achievements
Rank 1
answered on 19 Jul 2013, 01:36 PM
I've created a support ticket
0
Stephen
Top achievements
Rank 1
answered on 19 Jul 2013, 01:59 PM
Found the problem myself:

Commented this function out - now works as expected

///// <summary>
///// Collections radgrid item created event
///// </summary>
///// <param name="sender">The sender</param>
///// <param name="e">The event args</param>
//protected void CollectionsRadGrid_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
//{
//    if (e.Item is GridEditableItem && e.Item.IsInEditMode)
//    {
//        //RadGrid collectionItemsRadGrid = (RadGrid)e.Item.FindControl("CollectionItemsRadGrid");
//        //collectionItemsRadGrid.Rebind();
//    }
//}

Tags
Grid
Asked by
Stephen
Top achievements
Rank 1
Answers by
Stephen
Top achievements
Rank 1
Share this question
or