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

Radgrid OnCommand client side makes e.Item.ItemIndex 0

1 Answer 203 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeroen
Top achievements
Rank 1
Jeroen asked on 15 Jan 2013, 05:59 PM
Hi,

I've pasted my code below. I need to know which row caused the itemcommand server side. This works fine in below code as long as the OnCommand client event is not used. When i start using it the ItemIndex is always 0. The same issue is described in http://www.telerik.com/community/forums/aspnet-ajax/grid/itemcommand-why-is-my-itemindex-always-zero.aspx, but the solution does not work for me as the selected row might be different. GetDataKeyValue also does not work it always returns the data key from first row.

Is there a way to find which row caused the itemcommand while have the client side OnCommand?

Thanks!

using System;
 
using Telerik.Web.UI;
 
public partial class Default : System.Web.UI.Page
{
    public class A
    {
        public int a { get; set; }
        public int b { get; set; }
    }
 
 
    protected void Page_Load(object sender, EventArgs e)
    {
        RadGrid1.DataSource = new A[] {new A() {a = 1, b = 2}, new A() {a = 3, b = 4}};
    }
 
    protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
    {
        int i = e.Item.ItemIndex;
 
        GridDataItem item = (GridDataItem)e.Item;
        int letterId = (int)item.GetDataKeyValue("a");
 
        //GridDataItem dataItem = RadGrid1.SelectedItems[0] as GridDataItem;
    }
}

<%@ 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, Version=2012.3.1205.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4"
                Name="Telerik.Web.UI.Common.Core.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI, Version=2012.3.1205.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4"
                Name="Telerik.Web.UI.Common.jQuery.js" />
            <asp:ScriptReference Assembly="Telerik.Web.UI, Version=2012.3.1205.35, Culture=neutral, PublicKeyToken=121fae78165ba3d4"
                Name="Telerik.Web.UI.Common.jQueryInclude.js" />
        </Scripts>
    </telerik:RadScriptManager>
    <script type="text/javascript">
    //Put your JavaScript code here.
    </script>
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
    </telerik:RadAjaxManager>
    <div>
        <telerik:RadGrid ID="RadGrid1" runat="server" OnItemCommand="rg_ItemCommand">
            <MasterTableView DataKeyNames="a" AutoGenerateColumns="False">
                <Columns>
                    <telerik:GridBoundColumn DataField="a" DataType="System.Int32">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="b" DataType="System.Int32">
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn ButtonType="LinkButton" CommandName="test" Text="test">
                    </telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings>
                <%--removing Oncommand will make itemindex work--%>
                <ClientEvents OnCommand="rgCommand" />
            </ClientSettings>
        </telerik:RadGrid>
    </div>
    <script type="text/javascript">
        function rgCommand(sender, args) {
            var commandName = args.get_commandName();
             
        }
    </script>
    </form>
</body>
</html>

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 17 Jan 2013, 11:00 AM
Hi,

Since you have client OnCommand handler attached, your server ItemCommand is always fired by first item in Items collection. This behavior appear only when you have custom CommandName/CommandArguments set. In order to get real Item on server ItemCommand event you can use the code below.

C#:
  protected void rg_ItemCommand(object sender, GridCommandEventArgs e)
    {
        if (e.CommandName == "test")
        {
            GridDataItem item = RadGrid1.Items[e.CommandArgument.ToString()];
            int Id = (int)item.GetDataKeyValue("a");
       }
}

Thanks,
Princy
Tags
Grid
Asked by
Jeroen
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or