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

RadGrid / GridTableView ItemCommand not firing

4 Answers 194 Views
Grid
This is a migrated thread and some comments may be shown as answers.
C
Top achievements
Rank 1
C asked on 02 Sep 2013, 02:23 PM
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?

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 03 Sep 2013, 03:38 AM
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
0
C
Top achievements
Rank 1
answered on 03 Sep 2013, 07:05 AM
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?
0
Princy
Top achievements
Rank 1
answered on 04 Sep 2013, 04:18 AM
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
0
Tonya
Top achievements
Rank 1
answered on 04 Mar 2019, 04:32 PM

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

Tags
Grid
Asked by
C
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
C
Top achievements
Rank 1
Tonya
Top achievements
Rank 1
Share this question
or