Articles in this section
Category / Section

How to save data in server while batch editing is enabled in ASP.NET Web Forms Grid?

1 min read

Essential JavaScript Grid allows option to perform server side batch editing. This can be enabled by using the UrlAdaptor/RemoteSaveAdaptor in grid datasource and by specifying the URL to the batch operation. While performing batch editing, the needed information will be send to server through as AJAX post request.

In the following code snippet, we have demonstrated batch editing operation using UrlAdaptor.

Grid Initialization

 JS

 
<div id="Grid"></div>
 
<script type="text/javascript">
 
    $(function () {
 
        $("#Grid").ejGrid({
 
            dataSource: ej.DataManager({ url: "/Home/BatchData", batchUrl: "/Home/BatchUpdate" , adaptor: "UrlAdaptor" }),
            allowPaging: true,
            editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: "batch" },
            toolbarSettings: { showToolbar: true, toolbarItems: ["add", "edit", "delete", "update", "cancel"] },
            columns: [{ field: "Id", isPrimaryKey: true },
            { field: "CompanyName" },
            { field: "City" },
            { field: "PostalCode" },
            { field: "MobileNumber" }]
 
 
        });
 
    });
 
</script>
 

 

 MVC

 
@(Html.EJ().Grid<object>("ExportGrid")
    .Datasource(ds => { ds.URL("/Home/BatchData").BatchURL("/Home/BatchUpdate").Adaptor(AdaptorType.UrlAdaptor);})
    .AllowPaging()
    .EditSettings(e => e.AllowEditing().AllowDeleting().AllowAdding().EditMode(EditMode.Batch))
    .ToolbarSettings(tools => tools.ShowToolbar().ToolbarItems(item =>
    {
        item.AddTool(ToolBarItems.Add);
        item.AddTool(ToolBarItems.Edit);
        item.AddTool(ToolBarItems.Delete);
        item.AddTool(ToolBarItems.Update);
        item.AddTool(ToolBarItems.Cancel);
    }))
 
    .Columns(col =>
        {
 
            col.Field("Id").IsPrimaryKey(true).Add();
            col.Field("CompanyName").Add();
            col.Field("City").Add();
            col.Field("PostalCode").Add();
            col.Field("MobileNumber").Add();
        }))
 

 

The action in which, the batch save operation is performed is as follows.

 
public ActionResult BatchUpdate(string key, List<Supplier> changed, List<Supplier> added, List<Supplier> deleted)
        {
 
            NORTHWND1Entities db = new NORTHWND1Entities();
 
            //Performing insert operation
            if (added != null && added.Count() > 0)
            {
                foreach (var temp in added)
                {
                    db.Suppliers.Add(temp);
                }
            }
 
            ////Performing update operation
            if (changed != null && changed.Count() > 0)
            {
                foreach (var temp in changed)
                {
                    Supplier old = db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault();
                    if (old != null)
                    {
                        db.Entry(old).CurrentValues.SetValues(temp);
                    }
                }
            }
 
            //Performing delete operation
            if (deleted != null && deleted.Count() > 0)
            {
                foreach (var temp in deleted)
                {
                    db.Suppliers.Remove(db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault());
                }
            }
 
            db.SaveChanges();
 
 
            return RedirectToAction("BatchData");
        }
 

 

ASPX

 
<ej:Grid ID="Grid" runat="server" AllowPaging="true">
            <DataManager URL="Default.aspx/BatchData" BatchURL="Default.aspx/BatchUpdate" Adaptor="UrlAdaptor" />
 
            <EditSettings AllowEditing="true" AllowAdding="true" AllowDeleting="true" EditMode="Batch" />
            
            <ToolbarSettings ShowToolbar="true" ToolbarItems="add,edit,delete,update,cancel"/>
            
            <Columns>
                <ej:Column Field="Id" IsPrimaryKey="true" IsIdentity="true"/>
                <ej:Column Field="CompanyName"/>
                <ej:Column Field="City"/>
                <ej:Column Field="PostalCode"/>
                <ej:Column Field="MobileNumber"/>
                <ej:Column Field="EmailAddress"/>
                <ej:Column Field="WebAddress"/>
                <ej:Column Field="IsActive"/>
            </Columns>
 
        </ej:Grid>
 
 

 

In Webforms, the batch operation can performed in the Webmethod, please refer the below code snippet.

 
//Webmethod called when batch save operation performed
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void BatchUpdate(string key, List<Supplier> changed, List<Supplier> added, List<Supplier> deleted) {
 
            NORTHWND1Entities db = new NORTHWND1Entities();
 
            //Performing insert operation
            if (added != null && added.Count() > 0)
            {
                foreach (var temp in added)
                {
                    db.Suppliers.Add(temp);
                }
            }
 
            ////Performing update operation
            if (changed != null && changed.Count() > 0)
            {
                foreach (var temp in changed)
                {
                    Supplier old = db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault();
                    if (old != null)
                    {
                        db.Entry(old).CurrentValues.SetValues(temp);
                    }
                }
            }
 
            //Performing delete operation
            if (deleted != null && deleted.Count() > 0)
            {
                foreach (var temp in deleted)
                {
                    db.Suppliers.Remove(db.Suppliers.Where(o => o.Id == temp.Id).SingleOrDefault());
                }
            }
 
            db.SaveChanges();
 
        }
 

 

Result

Figure 1: Grid with batch editing

The POST request and response to the server during the batch processing will be as follows.

 

Figure 2: Batch request send to the server with edited data information on save operation.

 

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