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

ItemIndex always 0

4 Answers 111 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steele
Top achievements
Rank 1
Steele asked on 11 Jan 2011, 01:34 AM
Hi All,
I have seen some posts on this already, but don't think the work arounds apply to what I am experiencing.
Basically, if I setup the datagrid with a server-side ItemCommand event, the ItemIndex is correct.
Adding in a client-side OnCommand event, however, makes the ItemIndex server-side always 0 (zero).  Interestingly, the get_commandArgument returns the correct value within the OnGridCommand event (see example).
Why is this?  See below for an example.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridButtonIndex.aspx.cs"
    Inherits="TestTelerikWebApp.GridButtonIndex" %>
  
<!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>
</head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
    </telerik:RadScriptManager>
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function OnGridCommand(sender, args) {
                var grid = sender;
                debugger;
                if (args.get_commandName() == "InitInsert" && grid._editIndexes[0] >= 0) {
                    alert("Cannot add while editing a record.");
                    args.set_cancel(true);
                }
                if (args.get_commandName() == "Delete") {
                    if (grid._editIndexes[0] >= 0) {
                        alert("Cannot delete while editing a record.");
                        args.set_cancel(true);
                    }
                    else
                        var value = confirm("Are you sure you want to delete?");
                    if (!value)
                        args.set_cancel(true);
                }
            }
        </script>
    </telerik:RadCodeBlock>
    <div>
        <telerik:RadGrid ID="dgTest" runat="server" AutoGenerateColumns="False" GridLines="None"
            OnItemCommand="dgTest_ItemCommand" OnNeedDataSource="dgTest_NeedDataSource">
            <ClientSettings>
                <ClientEvents OnCommand="OnGridCommand" />
            </ClientSettings>
            <MasterTableView>
                <CommandItemSettings ExportToPdfText="Export to Pdf"></CommandItemSettings>
                <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridBoundColumn DataField="Description" FilterControlAltText="Filter colDesc column"
                        HeaderText="Description" UniqueName="colDesc">
                    </telerik:GridBoundColumn>
                    <telerik:GridButtonColumn ButtonType="PushButton" CommandName="TestClick" FilterControlAltText="Filter colButt column"
                        Text="Press Me" UniqueName="colButt">
                    </telerik:GridButtonColumn>
                </Columns>
                <EditFormSettings>
                    <EditColumn FilterControlAltText="Filter EditCommandColumn column">
                    </EditColumn>
                </EditFormSettings>
            </MasterTableView>
            <FilterMenu EnableImageSprites="False">
            </FilterMenu>
            <HeaderContextMenu CssClass="GridContextMenu GridContextMenu_Default">
            </HeaderContextMenu>
        </telerik:RadGrid>
    </div>
    <asp:Label ID="lblFeedback" runat="server"></asp:Label>
    <telerik:RadAjaxManager runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="dgTest">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="dgTest" />
                    <telerik:AjaxUpdatedControl ControlID="lblFeedback" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    </form>
</body>
</html>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
  
using System.Data;
using Telerik.Web.UI;
  
namespace TestTelerikWebApp
{
    public partial class GridButtonIndex : System.Web.UI.Page
    {
        protected DataTable GetData()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Description", typeof(string));
            DataRow dr = dt.NewRow();
            dr["Id"] = 1;
            dr["Description"] = "Test 1";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Id"] = 2;
            dr["Description"] = "Test 2";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["Id"] = 3;
            dr["Description"] = "Test 3";
            dt.Rows.Add(dr);
  
            return dt;
        }
  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                  
            }
        }
  
        protected void dgTest_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid rg = (RadGrid)sender;
            rg.DataSource = GetData();
        }
  
        protected void dgTest_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "TestClick")
            {
                GridEditableItem gei = (GridEditableItem)(e.Item);
                lblFeedback.Text = "Item Index is : " + gei.ItemIndex.ToString();
            }
        }
    }
}


I am using the latest Telerik release with ASP.Net 40 (2010.3.1215.40).

Thanks for any insight.
Steele.

4 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 11 Jan 2011, 08:58 AM
Hello Steele,

This is true only for custom commands, i.e commands for which RadGrid does not  have the knowledge how to handle them. The command will be triggered via first item in RadGrid, but if you do not explicitly set CommandArguments it will contain the index of the item from which the command was triggered.

In the meaning of facts the OnCommand client event is intended for client-side binding, thus using it with server-side binding might lead to such behavior.

Regards,
Nikolay
the Telerik team
Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
0
Steele
Top achievements
Rank 1
answered on 14 Jan 2011, 11:11 AM
Thanks Nikolay for the info. So if that is the limitation then what is the work around? Could you please identify the changes I would need to make to the example to make it work as expected? Thanks, Steele.
0
Steele
Top achievements
Rank 1
answered on 17 Jan 2011, 01:47 AM
Hi,
Any word on this?
You stated in your response :
but if you do not explicitly set CommandArguments it will contain the index of the item from which the command was triggered
Where do I explicitly set CommandArguments in the case?
Thanks for any help,
Steele.
0
Steele
Top achievements
Rank 1
answered on 17 Jan 2011, 02:33 AM
Ok - I see what is happening now.
In the grid's server side ItemCommand event, the ItemIndex property is wrong.
Using the GridCommandEventArgs's CommandArgument property instead fixes the issue.

int idx = -1;
if (!int.TryParse(e.CommandArgument.ToString(), out idx))
{
    idx = -1;
}

Thanks,
Steele.
Tags
Grid
Asked by
Steele
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Steele
Top achievements
Rank 1
Share this question
or