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

Invalid ItemIndex when handling OnCommand Event on both server and client sides

1 Answer 37 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Reticent
Top achievements
Rank 1
Reticent asked on 07 Nov 2008, 02:57 PM
I'm using Telerik RadControls for ASP.NET AJAX 2008 Q2 and ASP.net 3.5.

I use a GridButtonColumn to do something on call back to server and it works just fine. The problem arises when I try to do something on the client side when I click the ButtonColumn. After adding the the client side handler, e.Item.ItemIndex will always be set to zero on the server. It only occurs when the CommandName is "View", but "RowClick" works as expected. The problem does not depend on what the client side handler does and an empty client side handler also causes this effect.

Am I doing anything wrong or is there a workaround?

The grid:
                            <telerik:RadGrid ID="GridMessages" runat="server" OnItemCommand="GridMessages_ItemCommand"
                                <MasterTableView AutoGenerateColumns="False" DataKeyNames="ID"
                                    <Columns> ...
                                        <telerik:GridButtonColumn CommandName="View" ImageUrl="Images/view.gif" ButtonType="ImageButton" 
                                            DataTextField="ID" UniqueName="view"
                                            <HeaderStyle Width="16px" /> 
                                        </telerik:GridButtonColumn> 
                                    </Columns> 
                                </MasterTableView> 
                                <ClientSettings EnablePostBackOnRowClick="true"
                                    <ClientEvents OnCommand="GridMessages_Command" /> 
                                    <Selecting AllowRowSelect="True" /> 
                                </ClientSettings> 
                            </telerik:RadGrid> 

The server side code:
    protected void GridMessages_ItemCommand(object source, GridCommandEventArgs e) 
    { 
        if (!(e.Item is GridDataItem)) 
            return
 
        e.Item.Selected = true
        var id = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["ID"].ToString(); 
 
        // ... 
        // Load the selected message from database 
 
        switch (e.CommandName) 
        { 
            case "RowClick"
                // Preview the message
                break
            case "View"
                // Open message in a new tab
                break
        } 

The client side code:
    function GridMessages_Command(sender, args) 
    { 
        // Sets the selected page view of a multipage 
    } 
 

Thanks,
Ilia


1 Answer, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 11 Nov 2008, 07:16 AM
Hello Reticent,

Can you show us how you bind your RadGrid?
If you don't bind your control server-side its Items collection will be empty when ItemCommand occurs.

Regards,
Nikolay
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
Reticent
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Share this question
or