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

Batch Editing doesnt work with ajax

1 Answer 104 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rene
Top achievements
Rank 1
Rene asked on 08 Jul 2015, 03:09 PM

Hey guys,

im trying to follow the example from http://demos.telerik.com/aspnet-ajax/grid/examples/data-editing/batch-editing/defaultcs.aspx but i am unable to follow. I removed the datasource part becuase im not having the demo database installed and tried to use some static data instead but it seems like somehow neither my events nor the ajax stuff is fired. Anyone 

 

Codebehind: 

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 WebApplication2
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
            DataTable dt = new DataTable();
            DataColumn dc1 = new DataColumn();
            dc1.ColumnName = "ProductID";
            dc1.Caption = "ProductID";
            dc1.DataType = typeof(int);
            dt.Columns.Add(dc1);
 
            DataColumn dc2 = new DataColumn();
            dc2.ColumnName = "ProductName";
            dc2.Caption = "ProductName";
            dc2.DataType = typeof(string);
            dt.Columns.Add(dc2);
 
            DataColumn dc3 = new DataColumn();
            dc3.ColumnName = "CategoryID";
            dc3.Caption = "CategoryID";
            dc3.DataType = typeof(int);
            dt.Columns.Add(dc3);
 
            DataColumn dc4 = new DataColumn();
            dc4.ColumnName = "CategoryName";
            dc4.Caption = "CategoryName";
            dc4.DataType = typeof(string);
            dt.Columns.Add(dc4);
 
            DataColumn dc5 = new DataColumn();
            dc5.ColumnName = "UnitPrice";
            dc5.Caption = "UnitPrice";
            dc5.DataType = typeof(decimal);
            dt.Columns.Add(dc5);
 
            DataColumn dc6 = new DataColumn();
            dc6.ColumnName = "Discontinued";
            dc6.Caption = "Discontinued";
            dc6.DataType = typeof(bool);
            dt.Columns.Add(dc6);
 
            DataColumn dc7 = new DataColumn();
            dc7.ColumnName = "UnitsInStock";
            dc7.Caption = "UnitsInStock";
            dc7.DataType = typeof(int);
            dt.Columns.Add(dc7);
 
            dt.Rows.Add(1, "name", 1, "cname", 20, false, 20);
            RadGrid1.DataSource = dt;
        }
 
        protected void RadGrid1_BatchEditCommand(object sender, Telerik.Web.UI.GridBatchEditingEventArgs e)
        {
            SavedChangesList.Visible = true;
        }
 
        protected void RadGrid1_ItemUpdated(object source, Telerik.Web.UI.GridUpdatedEventArgs e)
        {
            GridEditableItem item = (GridEditableItem)e.Item;
            String id = item.GetDataKeyValue("ProductID").ToString();
            if (e.Exception != null)
            {
                e.KeepInEditMode = true;
                e.ExceptionHandled = true;
                NotifyUser("Product with ID " + id + " cannot be updated. Reason: " + e.Exception.Message);
            }
            else
            {
                NotifyUser("Product with ID " + id + " is updated!");
            }
        }
 
        protected void RadGrid1_ItemInserted(object source, GridInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                NotifyUser("Product cannot be inserted. Reason: " + e.Exception.Message);
            }
            else
            {
                NotifyUser("New product is inserted!");
            }
        }
 
        protected void RadGrid1_ItemDeleted(object source, GridDeletedEventArgs e)
        {
            GridDataItem dataItem = (GridDataItem)e.Item;
            String id = dataItem.GetDataKeyValue("ProductID").ToString();
            if (e.Exception != null)
            {
                e.ExceptionHandled = true;
                NotifyUser("Product with ID " + id + " cannot be deleted. Reason: " + e.Exception.Message);
            }
            else
            {
                NotifyUser("Product with ID " + id + " is deleted!");
            }
 
        }
 
        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            //RadNumericTextBox unitsNumericTextBox = (RadGrid1.MasterTableView.GetBatchColumnEditor("UnitsInStock") as GridNumericColumnEditor).NumericTextBox;
            //unitsNumericTextBox.Width = Unit.Pixel(60);
        }
 
        private void NotifyUser(string message)
        {
            RadListBoxItem commandListItem = new RadListBoxItem();
            commandListItem.Text = message;
            SavedChangesList.Items.Add(commandListItem);
        }
    }
}
 

 Markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
<%@ Register Assembly="Telerik.Web.UI, Culture=neutral, PublicKeyToken=121fae78165ba3d4"
    Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<head id="Head1" runat="server">
    <title>Telerik ASP.NET Example</title>
    </head>
<body>
    <form id="form1" runat="server">
    <telerik:RadScriptManager runat="server" ID="RadScriptManager1" />
    <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="RadGrid1">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="RadGrid1" LoadingPanelID="RadAjaxLoadingPanel1" />
                    <telerik:AjaxUpdatedControl ControlID="SavedChangesList" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManager>
    <telerik:RadAjaxLoadingPanel runat="server" ID="RadAjaxLoadingPanel1">
    </telerik:RadAjaxLoadingPanel>   
    <div id="demo" class="demo-container no-bg">
        <telerik:RadListBox runat="server" ID="SavedChangesList" Width="600px" Height="200px"
            Visible="false">
        </telerik:RadListBox>
        <telerik:RadGrid ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"
            AllowAutomaticInserts="True" PageSize="10" OnItemDeleted="RadGrid1_ItemDeleted"
            OnItemInserted="RadGrid1_ItemInserted" OnItemUpdated="RadGrid1_ItemUpdated" OnPreRender="RadGrid1_PreRender"
            AllowAutomaticUpdates="True" AllowPaging="True" AutoGenerateColumns="False" OnBatchEditCommand="RadGrid1_BatchEditCommand">
            <MasterTableView CommandItemDisplay="TopAndBottom" DataKeyNames="ProductID" HorizontalAlign="NotSet"
                EditMode="Batch" AutoGenerateColumns="False">
                <BatchEditingSettings EditType="Cell" />
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="ProductID" SortOrder="Descending" />
                </SortExpressions>
                <Columns>
                    <telerik:GridBoundColumn DataField="ProductName" HeaderStyle-Width="210px" HeaderText="ProductName"
                        SortExpression="ProductName" UniqueName="ProductName">
                        <ColumnValidationSettings EnableRequiredFieldValidation="true">
                            <RequiredFieldValidator ForeColor="Red" Text="*This field is required" Display="Dynamic">
                            </RequiredFieldValidator>
                        </ColumnValidationSettings>
                    </telerik:GridBoundColumn>
                    <telerik:GridNumericColumn DataField="UnitsInStock" HeaderStyle-Width="80px" HeaderText="Units In Stock"
                        SortExpression="UnitsInStock" UniqueName="UnitsInStock">
                    </telerik:GridNumericColumn>
                    <telerik:GridCheckBoxColumn DataField="Discontinued" HeaderStyle-Width="80px" HeaderText="Discontinued"
                        SortExpression="Discontinued" UniqueName="Discontinued">
                    </telerik:GridCheckBoxColumn>
                    <telerik:GridTemplateColumn HeaderText="UnitPrice" HeaderStyle-Width="80px" SortExpression="UnitPrice"
                        UniqueName="TemplateColumn" DataField="UnitPrice">
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblUnitPrice" Text='<%# Eval("UnitPrice", "{0:C}") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                            <span>
                                <telerik:RadNumericTextBox Width="55px" runat="server" ID="tbUnitPrice">
                                </telerik:RadNumericTextBox>
                                <span style="color: Red">
                                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="tbUnitPrice"
                                        ErrorMessage="*Required" runat="server" Display="Dynamic">
                                    </asp:RequiredFieldValidator>
                                </span></span>
                        </EditItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridButtonColumn ConfirmText="Delete this product?" ConfirmDialogType="RadWindow"
                        ConfirmTitle="Delete" HeaderText="Delete" HeaderStyle-Width="50px" ButtonType="ImageButton"
                        CommandName="Delete" Text="Delete" UniqueName="DeleteColumn">
                    </telerik:GridButtonColumn>
                </Columns>
            </MasterTableView>
            <ClientSettings AllowKeyboardNavigation="true">
            </ClientSettings>
        </telerik:RadGrid>
    </div>
    </form>
</body>
</html>

1 Answer, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 13 Jul 2015, 08:41 AM
Hi Rene,

In the provided code the RadGrid is not data bound properly. You should bind the grid to a declarative DataSource control or via the NeedDataSource event.

Also, in the sample code the automatic operations are enabled. Have in mind that in order to use automatic updates, inserts, deletes you should bind the grid to a declarative DataSource that supports automatic operations.

For your convenience I have prepared a sample project that illustrates the approach. In the sample there is a RadGrid that is data-bound via the NeedDataSource event. It also uses manual CRUD operations.


Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Rene
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Share this question
or