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

RadGrid ItemCommand not firing with fireCommand

1 Answer 387 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Andrey
Top achievements
Rank 1
Andrey asked on 12 Mar 2012, 11:41 AM
Hello,

I use RadGrid and have javascript function that calls fireCommand for adding new rows. It works only when I the grid is not empty. When the gird havs 0 rows, it still fires Page_Load, but not the event handler for ItemCommand event. It used to work before the last 2 updates.

See the example below:

Main.aspx:
<%@ Page EnableViewState="true" Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs"
    Inherits="Pages.MainPage" MasterPageFile="~/Main.master" %>
 
<asp:Content ContentPlaceHolderID="Head" runat="server">
    <script type="text/javascript">
        function AddNewRow(idGrid) {
            var masterTable = $find(idGrid).get_masterTableView();
            masterTable.fireCommand("NewRow");
        }
    </script>
</asp:Content>
<asp:Content ContentPlaceHolderID="Content" runat="server">
    <telerik:RadGrid runat="server" ID="grid" OnNeedDataSource="grid_NeedDatasource"
        OnItemCommand="grid_ItemCommand" AutoGenerateColumns="false" Width="300">
        <MasterTableView ShowFooter="true" EnableViewState="true" DataKeyNames="Value">
            <Columns>
                <telerik:GridTemplateColumn>
                    <ItemTemplate>
                        <%# Eval( "Value" ) %>
                    </ItemTemplate>
                    <FooterTemplate>
                        <telerik:RadNumericTextBox runat="server" ID="tbValue" Value="0" />
                    </FooterTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn UniqueName="ColumnOperation">                      
                    <FooterTemplate>
                        <a href="javascript:void(0)">
                            <img src='<%# Telerik.Web.SkinRegistrar.GetWebResourceUrl(Page,typeof(RadGrid), "Telerik.Web.UI.Skins.WebBlue.Grid.AddRecord.gif") %>'
                                onclick="AddNewRow('<%# Container.OwnerTableView.Parent.ClientID %>');"
                                alt="Add" /></a>
                    </FooterTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridButtonColumn UniqueName="ColumnDelete" CommandName="Delete" ButtonType="ImageButton" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>
</asp:Content>

Main.aspx.cs:
using System;
using System.Web.UI;
using System.Collections.Generic;
using System.Linq;
using Telerik.Web.UI;
 
namespace Pages
{
    public partial class MainPage : Page
    {      
        public void Page_Load( object sender, EventArgs e )
        {
            if ( !IsPostBack )
            {
                m_values = new List<Double>() { 1, 2, 3 };
            }
        }
 
 
        protected void grid_NeedDatasource( Object sender, GridNeedDataSourceEventArgs e )
        {
            grid.DataSource = m_values.Select( x => new { Value = x } );
        }
 
 
        protected void grid_ItemCommand( object sender, GridCommandEventArgs e )
        {
            switch ( e.CommandName )
            {
                case "NewRow":
                    var item = grid.MasterTableView.GetItems( GridItemType.Footer )[ 0 ];
                    var tbValue = item.FindControl( "tbValue" ) as RadNumericTextBox;
 
                    m_values.Add( tbValue.Value.Value );
 
                    grid.Rebind();
                    break;
 
                case "Delete":
                    var value = (Double)( e.Item as GridDataItem ).GetDataKeyValue( "Value" );
                    m_values.Remove( value );
                     
                    grid.Rebind();
                    break;
            }
        }
 
 
        private List<Double> m_values
        {
            get
            {
                return ViewState[ "values" ] as List<Double>;
            }
            set
            {
                ViewState[ "values" ] = value;
            }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 14 Mar 2012, 03:33 PM
Andrey:

Take a look at this forum thread which should provide some insights to assist:

Item_command InitInsert

It appears that custom commands are fired through the first data item in the GridTableView. This means that no command will be fired if the grid is bound to an empty data source.

Hope this helps!
Tags
Grid
Asked by
Andrey
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Share this question
or