RadGrid / GridTableView ItemCommand not firing

5 posts, 0 answers
  1. C
    C avatar
    43 posts
    Member since:
    Jul 2013

    Posted 02 Sep 2013 Link to this post

    I have a RadGrid that contains a GridTableView in the DetailTables, as such:

    <telerik:RadGrid ID="RelatedProductsGrid" runat="server" Width="95%" ShowStatusBar="true"
                AutoGenerateColumns="False" PageSize="20" AllowSorting="False" AllowMultiRowSelection="False"
                AllowPaging="True">
                <pagerstyle mode="NextPrevAndNumeric"></pagerstyle>
                <mastertableview width="100%" datakeynames="PartId" allowmulticolumnsorting="False">
            <DetailTables>
                <telerik:GridTableView DataKeyNames="PartId" Name="RelatedProducts" Width="100%">
                    <Columns>

    ...

    <telerik:GridTemplateColumn HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right">
                            <ItemTemplate>
                                <telerik:RadButton ID="RemoveRelatedProductButton" runat="server" CommandName="Delete">
                                </telerik:RadButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>

    So, the above button, "RemoveRelatedProductButton" shows up allright, but whenever I press it - nothing happens.

    I have this in the code behind:

    _relatedProductsGrid = FindControl("RelatedProductsGrid") as RadGrid;
    _relatedProductsGrid.ItemCommand += RelatedProductsGrid_ItemCommand;

    And finally:

    protected void RelatedProductsGrid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
            {
                if (e.CommandName == "Delete")

    etc.

    However, this method never fires. What do I need to do?
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 02 Sep 2013 Link to this post

    Hi,

    I guess you want to access the delete command for the child grid.Please try the following code snippet.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="RadGrid1_ItemCommand" >
        <MasterTableView DataKeyNames="OrderID" Name="Parent">
            <Columns>       
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <telerik:RadButton ID="Remove" runat="server" CommandName="Delete" Text="Delete">
                        </telerik:RadButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
            <DetailTables>
                <telerik:GridTableView DataKeyNames="UnitPrice" Name="Child">
                    <ParentTableRelation>
                        <telerik:GridRelationFields DetailKeyField="OrderID" MasterKeyField="OrderID"></telerik:GridRelationFields>
                    </ParentTableRelation>
                    <Columns>                
                        <telerik:GridTemplateColumn>
                            <ItemTemplate>
                                <telerik:RadButton ID="RemoveRelatedProductButton" runat="server" CommandName="Delete"
                                    Text="Delete">
                                </telerik:RadButton>
                            </ItemTemplate>
                        </telerik:GridTemplateColumn>
                    </Columns>
                </telerik:GridTableView>
            </DetailTables>
        </MasterTableView>
    </telerik:RadGrid>

    C#:
    protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
      {
          if (e.CommandName == "Delete")
          {
               GridDataItem item =(GridDataItem)e.Item;
               if (item.OwnerTableView.Name == "Child")
               {
                   Response.Write("Child Delete");
                   string childval = item.GetDataKeyValue("UnitPrice").ToString();//Access the Child datakey value
                  //Code to delete
               }
               else if (item.OwnerTableView.Name == "Parent")
               {
                   Response.Write("Parent Delete");
                   string parentval = item.GetDataKeyValue("OrderID").ToString();//Access the parent datakey value
                   //Code to delete
               }          
          }
      }

    Thanks,
    Princy
  3. C
    C avatar
    43 posts
    Member since:
    Jul 2013

    Posted 03 Sep 2013 Link to this post

    Yes, I want to run the command "Delete", but the grid does not realize that any command gets run. At all.

    When I try your code, nothing shows up on the screen at all.

    Is it not possible to see from my code what is wrong?
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 03 Sep 2013 Link to this post

    HI,

    Please try the below code snippet and check if it fires the ItemCommand event.If this doesn't help,please provide your full code snippet.

    C#:
    protected void Page_Init(object source, System.EventArgs e)
       {
          RadGrid radgrid = FindControl("RadGrid1") as RadGrid;
          radgrid.ItemCommand += new GridCommandEventHandler(radgrid_ItemCommand);
       }
     
    protected void radgrid_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
       {
           if (e.CommandName == "Delete")
           {        
               //Code to delete                 
           }
       }

    Thanks,
    Princy
  5. Tonya
    Tonya avatar
    20 posts
    Member since:
    Jul 2014

    Posted 04 Mar 2019 in reply to Princy Link to this post

    Princy-

    Thank you so much for your suggestion to add those 2 lines to the Page_Init!!!

    I'd been struggling with this same issue for a few days. I meant to open a forum thread & instead ended up opening a Telerik support ticket. Unfortunately, the rep assigned wasn't able to help (though my code is now cleaner based on his suggestions). I was able close the ticket after finding this post! I'm just sad it took me so long to find it.

    Over the years, many of my grid issues have been solved by reviewing posts you've provided solutions for. Thank you! :)

    Tonya

Back to Top