Articles in this section
Category / Section

How to update only current page selection to server-end using header checkbox in ASP.NET Web Forms Grid?

3 mins read

This Knowledge base explains how to update only current page selected records to server-end using header checkbox on batch Editing.

Solution:

By default, Grid will select all the page records on clicking the header checkbox. To select only the currentPage records, we have used headertemplate property of the Grid to render the external checkbox in the header which act as header checkbox and handled the checkbox selection only to current page using actionComplete event.

1. Render the Grid control with actionComplete and dataBound event. Initialize the checkbox on column Header using headerTemplateID for type checkbox column.

HTML

<div id="headertemp">
            <input type='checkbox' id='extcheckselectall' />
</div>

JavaScript

<div id=”Grid”></div>
<script type="text/javascript">
        $(function () {
            $("#Grid").ejGrid({
         dataSource: ej.DataManager(window.gridData),                    
                   allowPaging: true,
                   allowGrouping: true,
                   allowSorting: true, 
                   dataBound: "dataBound",
                   actionComplete: "complete",
                    columns: [
                      { field: "Verified", type: "checkbox", headerText: "", headerTemplateID: "#headertemp", width: 50 ,allowSorting: false},
                      { field: "OrderID", isPrimaryKey: true, headerText: "Order ID",  width: 90 },
                      { field: "CustomerID", headerText: "Customer ID",  width: 90 },
                      { field: "EmployeeID", headerText: "Employee ID",  editType: ej.Grid.EditingType.Dropdown, width: 80  },
                      { field: "Freight", headerText: "Freight", width: 80, format: "{0:C}" }
                    ]
                });
            });
   </script>    

 

MVC

RAZOR

 @(Html.EJ().Grid<Object>("Grid")
           .Datasource((IEnumerable<object>)ViewBag.datasource)           
             .AllowPaging()
             .AllowGrouping()
             .AllowSorting()
            .Columns(col =>
              {
                  col.Field("Verified").Type("checkbox").HeaderText("").HeaderTemplateID("#headertemp").Width(90).AllowSorting(false).Add();
                  col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).Width(90).Add();
                  col.Field("CustomerID").HeaderText("Customer ID").Width(90).Add();
                  col.Field("EmployeeID").HeaderText("Employee ID").Width(80).Add();
                  col.Field("Freight").HeaderText("Freight").Width(80).Format("{0:C}").Add();
 
          })
     .ClientSideEvents(eve => { 
            eve.ActionComplete("complete");
            eve.DataBound("dataBound"); 
        }))

 

C#

[Controller]
namespace EJGrid.Controllers  {
    public class HomeController : Controller{
        public ActionResult Index()
        {
            var DataSource = OrderRepository.GetAllRecords();
            ViewBag.datasource = DataSource;
            return View();
        }        
    }
}

 

ASP.NET

ASPX

<ej:Grid ID="Grid" runat="server" AllowPaging="true" AllowGrouping="true" AllowSorting="true">
            <Columns>
                <ej:Column Field="Verified" HeaderText=""  Type="checkbox" AllowSorting="false" HeaderTemplateID="#headertemp" Width="90" />                
               <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="True"  Width="90" />
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="90" />
                <ej:Column Field="EmployeeID" HeaderText="Employee ID"  EditType="Dropdown" Width="80" />
                <ej:Column Field="Freight" HeaderText="Freight"  Width="80" Format="{0:C}"  />                
           </Columns>
           <ClientSideEvents  ActionComplete="complete" DataBound="dataBound"/>
 
 </ej:Grid >

 

ASPX.CS

[CS]
public partial class _Default : Page
{    
    protected void Page_Load(object sender, EventArgs e)
    {
            this.Grid.DataSource = new NorthwindDataContext().Orders;
            this.Grid.DataBind();
    }
}     

 

ASP.NET CORE

RAZOR

<ej-grid id="Grid" allow-paging="true" data-bound="dataBound"  datasource="ViewBag.DataSource" action-complete="complete" allow-grouping="true" allow-sorting="true" > 
    <e-columns> 
        <e-column field="Verified" header-text="" header-template-id="#headertemp" type="checkbox" width="90" " allow-sorting="false"></e-column> 
        <e-column field="OrderID" is-primary-key="true" header-text="Order ID" width="90"></e-column>
        <e-column field="CustomerID" header-text="Customer ID" width="90"></e-column>
        <e-column field="EmployeeID" header-text="Employee ID" width="80" edit-type="DropdownEdit"></e-column>
        <e-column field="Freight" header-text="Freight" width="80" format="{0:C}"></e-column>        
    </e-columns>
</ej-grid>  
 

 

C#

 public class GridController : Controller
     {
        public ActionResult GridFeatures()
           {
              var DataSource = new NorthwindDataContext().OrdersViews.ToList();
              ViewBag.DataSource = DataSource;
              return View();
            }
     }

Angular

html
<ej-grid #grid id="Grid" [dataSource]="gridData" [allowPaging]="true" [allowSorting]="true" [allowGrouping]="true" 
         (actionComplete)="complete($event)" (dataBound)="dataBound($event)">
    <e-columns>
        <e-column field="Verified" type="checkbox" headerText="" width="90" headerTemplateID="#headertemp" [allowSorting]="false"></e-column>
        <e-column field="OrderID" [isPrimaryKey]="true" headerText="Order ID" width="90"></e-column>
        <e-column field="CustomerID" headerText="Customer ID" width="90"></e-column>
        <e-column field="EmployeeID" headerText="Employee ID"  width="80"></e-column>
        <e-column field="Freight" headerText="Freight " format="{0:C}" width="80"></e-column>
    </e-columns>
</ej-grid>

 

[ts file]
import { Component, OnInit, Input, ElementRef, ViewChild } from '@angular/core';
import { EJComponents } from "ej-angular2/src/ej/core";
import { data } from "./data";
@Component({
    selector: 'ej-app',
    templateUrl: 'src/grid/grid.component.html',
})
export class GridComponent {
    public gridData: any;
    constructor() {
        this.gridData = data;
    }
}
 
 

 

2. In the dataBound event of the Grid, render the ejCheckBox and on change event of the checkbox, selected records will be collected and pushed to the getBatchChanges. Later, batchSave method will be called to update the changes to the Grid DataSource. The checkbox state also maintained on Paging, Sorting, Grouping functionalities using the actionCompete event.

JS

 
<script>
    function dataBound(args){
        $("#extcheckselectall").ejCheckBox({
            change: "selectall",// Rendered the ejCheckbox and bind change event
            checked: this.getContent().find("input[checked='checked']").length == this.model.currentViewData.length
        })
    }
    function complete(args) {
        if (!this.initialRender) {     //changed the checkbox state according to paging, sorting, filtering, Grouping
            $("#extcheckselectall").ejCheckBox({
                checked: this.getContent().find("input[checked='checked']").length == this.model.currentViewData.length
            })
        }
    }
    function selectall(args) {
        if (args.isInteraction) {
            var indexes = [];
            var obj = $("#Grid").ejGrid("instance");
            var data = obj.getCurrentViewData();
            for (var i = 0; i < data.length; i++) {
                data[i].Verified = args.isChecked;
            }
            obj.getBatchChanges().changed = data;
            obj.batchSave();
        }
    }
 
</script>

 

 

[ts file]
import { Component, OnInit, Input, ElementRef, ViewChild } from '@angular/core';
import { EJComponents } from "ej-angular2/src/ej/core";
import { data } from "./data";
@Component({
    selector: 'ej-app',
    templateUrl: 'src/grid/grid.component.html',
})
export class GridComponent {
    @ViewChild('grid') Grid: EJComponents<any, any>;
    public gridData: any;
    dataBound(e: any) {
 
        setTimeout($.proxy(function (arg) {
            var flag = this.Grid.widget.getContent().find("input[checked='checked']").length == this.Grid.widget.model.currentViewData.length;
            $("#extcheckselectall").ejCheckBox({
                change: this.selectAll,
                checked: flag
            })
        },this), 0)
    }
 
    complete(e: any) {
        if (!ej.isNullOrUndefined(this.Grid.widget) && !this.Grid.widget.initialRender) {
            var flag = this.Grid.widget.getContent().find("input[checked='checked']").length == this.Grid.widget.model.currentViewData.length;
            $("#extcheckselectall").ejCheckBox({
                checked: flag
            })
        }
    }
    selectAll(args) {
        if (args["isInteraction"]) {
            var indexes = [];
            var gridObj = $("#Grid").ejGrid("instance");
            var data = gridObj.getCurrentViewData();
            for (var i = 0; i < data.length; i++) {
                data[i].Verified = args.isChecked;
            }
            gridObj.getBatchChanges().changed = data;
            gridObj.batchSave();
        }
    }
}
 
 

 

 

 

 

 

 

Output:

ID

Sample Link:- https://jsplayground.syncfusion.com/xjmsplx2

 

Note:

A new version of Essential Studio for ASP.NET is available. Versions prior to the release of Essential Studio 2014, Volume 2 will now be referred to as a classic versions.The new ASP.NET suite is powered by Essential Studio for JavaScript providing client-side rendering of HTML 5-JavaScript controls, offering better performance, and better support for touch interactivity. The new version includes all the features of the old version, so migration is easy.

The Classic controls can be used in existing projects; however, if you are starting a new project, we recommend using the latest version of Essential Studio for ASP.NET. Although Syncfusion will continue to support all Classic Versions, we are happy to assist you in migrating to the newest edition.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls. If you have any queries or require clarifications, please let us know in the comments section below.

You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied