New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Performing Batch Updates

A common scenario is to force several grid items in edit mode and then update them on single button click.

In the example below there is UpdateAll button in the grid command item template. The visitor can edit several grid items by pressing the edit button for each item(the Update button will be hidden and only the Cancel button will be present in edit mode). After editing the content of the grid items, a batch update is triggered when hitting the UpdateAll button in the command item template (the update is executed in the ItemCommand event handler when e.CommandName = UpdateAll). The idea is to iterate through all items in the EditItems collection of the grid and update their data with the new values in the grid data source.

ASP.NET
<telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" runat="server" AllowMultiRowEdit="True" DataSourceID="SqlDataSource1"
  OnItemCommand="RadGrid1_ItemCommand" OnItemDataBound="RadGrid1_ItemDataBound">
  <MasterTableView DataKeyNames="CustomerID" AutoGenerateColumns="false" EditMode="InPlace"
    CommandItemDisplay="TopAndBottom">
    <Columns>
      <telerik:GridBoundColumn ReadOnly="true" DataField="CustomerID" UniqueName="CustomerID"
        HeaderText="CustomerID">
      </telerik:GridBoundColumn>
      <telerik:GridBoundColumn DataField="ContactName" UniqueName="ContactName" HeaderText="ContactName">
      </telerik:GridBoundColumn>
      <telerik:GridEditCommandColumn UniqueName="EditCommandColumn" />
    </Columns>
    <CommandItemTemplate>
      <asp:Button runat="server" ID="UpdateAll" Text="Update All" CommandName="UpdateAll" /></CommandItemTemplate>
  </MasterTableView>
</telerik:RadGrid>
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
  ProviderName="System.Data.SqlClient" SelectCommand="SELECT TOP 10 CustomerID, ContactName FROM Customers"
  runat="server"></asp:SqlDataSource>

Client-side editing with batch updateOne possible implementation is demonstrated on this demo of RadGrid for ASP.NET AJAX (review the code implementation for details). The general idea is to:

  • Wire the ondblclick event of the grid cells

  • Togglethe visibility of the column editors and store the end user changes

  • Push the altered content on the server and update the data source

Furthermore, a user-friendly confirm dialog will be displayed to prompt whether the updates should be propagated or discarded when you perform sorting, paging, etc. operation. The example uses RadAjaxManager instance to ajaxify the grid and perform RadAjaxManagerClientObject.ajaxRequest(args) calls. These calls are invoked in the ProcessChanges and CancelChanges client methods and the data in the source is refreshed inside the OnAjaxRequest handler on the server. The sample also features RadInputManager and RadFormDecorator which are used to filter the numeric entries and style the textboxes, checkboxes and dropdowns.

Another approach is presented in the paragraph below.

The following sample takes advantage of the editSelected() client side method, to put the control in edit mode for all items that were previously selected. Once the control is in edit mode, the user can enter data in the edit fields, and then after clicking a button in the command template, all the data will be updated from the code-behind section.

ASP.NET
<script type="text/javascript">
    function ToggleEditSelected() {
        var grid = $find("RadGrid1");
        var masterTable = grid.get_masterTableView();
        masterTable.editSelectedItems();
    }
</script>
 
  <telerik:RadGrid RenderMode="Lightweight" AllowMultiRowEdit="true"
   AllowMultiRowSelection="true"
   ID="RadGrid1" OnNeedDataSource="RadGrid1_NeedDataSource" runat="server"
      OnItemCommand="RadGrid1_ItemCommand" >
      <MasterTableView DataKeyNames="CustomerID" CommandItemDisplay="Top">
          <CommandItemTemplate>
              <a href="javascript:void(0);" onclick="ToggleEditSelected();">
                  Edit selected</a>
              <asp:LinkButton ID="LinkButton1" CommandName="UpdateChanges" runat="server">Update all</asp:LinkButton>
          </CommandItemTemplate>
      </MasterTableView>
      <ClientSettings>
          <Selecting AllowRowSelect="True"></Selecting>
      </ClientSettings>
  </telerik:RadGrid>
Not finding the help you need?
Contact Support