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

Bug in DetailTable ItemCommand?

2 Answers 113 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 31 Mar 2015, 08:54 PM
Hi Telerik,

If a grid has detail table, the client grid command is set, the ItemCommand of the detail table will get the incorrect GridDataItem, can you take a look?

<ClientSettings>
   <ClientEvents OnCommand="GridCommand" />
</ClientSettings>

protected void RadGrid1_ItemCommand (object sender, GridCommandEventArgs e)
{
   if (e.CommandName == "RemoveLogin")
   {
      GridDataItem item = (GridDataItem)e.Item;
 
      int deleteKey = Convert.ToInt32 (item["KeyID"].Text);
 
      Label1.Text = item["KeyID"].Text + "<br/>" + item["LoginID"].Text;
 
      //RadGrid1.Rebind ();
   }
}

In following example, click "Remove" button on the row other than the first row in the detail table will get the wrong GridDataItem, the item of first row is always returned.

If the client event is disabled, the ItemCommand will work correctly.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
   <title></title>
   <telerik:RadStyleSheetManager ID="RadStyleSheetManager1" runat="server" />
</head>
<body>
   <form id="form1" runat="server">
   <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
      <Scripts>
         <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.Core.js" />
         <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQuery.js" />
         <asp:ScriptReference Assembly="Telerik.Web.UI" Name="Telerik.Web.UI.Common.jQueryInclude.js" />
      </Scripts>
   </telerik:RadScriptManager>
   <script type="text/javascript">
      function GridCommand(sender, eventArgs)
      {
 
      }
   </script>
   <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
   </telerik:RadAjaxManager>
   <div>
      <telerik:RadGrid ID="RadGrid1" AutoGenerateColumns="False" runat="server" AllowSorting="True" CellSpacing="0" GridLines="None" AllowPaging="True" PageSize="20"
         OnDetailTableDataBind="RadGrid1_DetailTableDataBind" OnItemCommand="RadGrid1_ItemCommand" OnNeedDataSource="RadGrid1_NeedDataSource">
         <SortingSettings EnableSkinSortStyles="false" />
         <ClientSettings>
            <Selecting AllowRowSelect="True" />
            <ClientEvents OnCommand="GridCommand" />
         </ClientSettings>
         <GroupingSettings CaseSensitive="false" />
         <MasterTableView DataKeyNames="MemberID" ClientDataKeyNames="MemberID" AllowFilteringByColumn="True">
            <DetailTables>
               <telerik:GridTableView Name="KeyDetail" Width="100%">
                  <Columns>
                     <telerik:GridBoundColumn DataField="KeyID" ReadOnly="true" HeaderText="Login ID" UniqueName="KeyID" AllowFiltering="false" />
                     <telerik:GridBoundColumn DataField="LoginID" ReadOnly="true" HeaderText="Login" UniqueName="LoginID" AllowFiltering="false" />
                     <telerik:GridBoundColumn DataField="LoginSID" ReadOnly="true" HeaderText="Encrypted SID" UniqueName="LoginSID" AllowFiltering="false" />
                     <telerik:GridButtonColumn UniqueName="RemoveLogin" ButtonType="PushButton" HeaderText="" CommandName="RemoveLogin" Text="Remove" />
                  </Columns>
               </telerik:GridTableView>
            </DetailTables>
            <Columns>
               <telerik:GridBoundColumn DataField="FirstName" ReadOnly="true" HeaderText="First Name" UniqueName="FirstName" ShowFilterIcon="false" AutoPostBackOnFilter="true"
                  CurrentFilterFunction="Contains" />
               <telerik:GridBoundColumn DataField="LastName" ReadOnly="true" HeaderText="Last Name" UniqueName="LastName" ShowFilterIcon="false" AutoPostBackOnFilter="true"
                  CurrentFilterFunction="Contains" />
               <telerik:GridButtonColumn UniqueName="EditLink" ButtonType="PushButton" HeaderText="Edit User" CommandName="EditUser" Text="Edit" />
               <telerik:GridButtonColumn UniqueName="ViewLog" ButtonType="PushButton" HeaderText="View Log" CommandName="ViewLog" Text="Log" />
            </Columns>
            <PagerStyle Mode="NextPrevNumericAndAdvanced" AlwaysVisible="true" />
            <SortExpressions>
               <telerik:GridSortExpression FieldName="userActive" SortOrder="Descending" />
            </SortExpressions>
         </MasterTableView>
      </telerik:RadGrid>
      <p>
         <asp:Label ID="Label1" runat="server" Text="Message"></asp:Label>
      </p>
   </div>
   </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using Telerik.Web.UI;
 
public partial class Default : System.Web.UI.Page
{
 
   protected void Page_Load (object sender, EventArgs e)
   {
      if (!IsPostBack)
      {
         DataTable masterTable = new DataTable ();
         masterTable.Columns.Add (new DataColumn ("FirstName", Type.GetType ("System.String")));
         masterTable.Columns.Add (new DataColumn ("LastName", Type.GetType ("System.String")));
         masterTable.Columns.Add (new DataColumn ("MemberID", Type.GetType ("System.Int32")));
         masterTable.Columns.Add (new DataColumn ("userActive", Type.GetType ("System.Boolean")));
 
         masterTable.Rows.Add ("John", "Doe", 1, true);
         masterTable.Rows.Add ("Sam", "Smith", 2, false);
         masterTable.Rows.Add ("Mary", "Doe", 3, true);
 
         DataTable detailTable = new DataTable ();
         detailTable.Columns.Add (new DataColumn ("MemberID", Type.GetType ("System.Int32")));
         detailTable.Columns.Add (new DataColumn ("KeyID", Type.GetType ("System.String")));
         detailTable.Columns.Add (new DataColumn ("LoginID", Type.GetType ("System.String")));
         detailTable.Columns.Add (new DataColumn ("LoginSID", Type.GetType ("System.String")));
 
         detailTable.Rows.Add (1, "1001", "Office\\JD", "JohnDoeSID");
         detailTable.Rows.Add (1, "1002", "Home\\JD", "JohnDoeSID2");
         detailTable.Rows.Add (1, "1003", "Home\\JD2", "JohnDoeSID3");
 
         detailTable.Rows.Add (2, "2001", "Office\\SS", "SamSmithSID");
         detailTable.Rows.Add (2, "2002", "Home\\SS", "SamSmithSID2");
 
 
         ViewState["master"] = masterTable;
         ViewState["detail"] = detailTable;
      }
   }
 
   protected void RadGrid1_NeedDataSource (object sender, GridNeedDataSourceEventArgs e)
   {
      RadGrid1.DataSource = (DataTable)ViewState["master"];
   }
 
   protected void RadGrid1_DetailTableDataBind (object sender, GridDetailTableDataBindEventArgs e)
   {
      GridDataItem dataItem = (GridDataItem)e.DetailTableView.ParentItem;
 
      if (e.DetailTableView.Name == "KeyDetail")
      {
         string memberID = dataItem.GetDataKeyValue ("MemberID").ToString ();
 
         DataTable detailTable =(DataTable)ViewState["detail"];
 
         e.DetailTableView.DataSource = detailTable.Select ("MemberID =" + memberID);
      }
   }
 
   protected void RadGrid1_ItemCommand (object sender, GridCommandEventArgs e)
   {
      if (e.CommandName == "RemoveLogin")
      {
         GridDataItem item = (GridDataItem)e.Item;
 
         int deleteKey = Convert.ToInt32 (item["KeyID"].Text);
 
         Label1.Text = item["KeyID"].Text + "<br/>" + item["LoginID"].Text;
 
         //RadGrid1.Rebind ();
      }
   }
}


2 Answers, 1 is accepted

Sort by
0
Accepted
Kostadin
Telerik team
answered on 02 Apr 2015, 07:29 AM
Hi Charles,

I already answered your support ticket and I will post my answer here as well if other user have a similar issue.

"The reason for that behavior is that you are using a custom command. When such command is used and GridCommand client event is attached the button execute a fireCommand client method with the custom command and pass the hierarchy index as an argument. Keep in mind in such case the Item from the argument might not be event a GridDataItem and it might be a GridHeaderItem or GridCommandItem.
Otherwise the button simply postback and you are able to get the GridDataItem from the ItemCommand arguments.
In order to be able to access the correct item you can use the CommandArgument of the ItemCommand event handler and manually get the appropriate item. For your convenience I modified the provided sample and attached it to this thread."

Regards,
Kostadin
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
0
Charles
Top achievements
Rank 1
answered on 02 Apr 2015, 01:43 PM
Thank you very much.
Tags
Grid
Asked by
Charles
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Charles
Top achievements
Rank 1
Share this question
or