Articles in this section
Category / Section

How to save edited records without showing confirmation message dialog in ASP.NET Web Forms Grid?

1 min read

To save the unsaved edited records on any Grid action, in batch edit mode.

Solution               

In Batch Edit Mode, with edited records not saved, if we perform any Grid actions such as paging, sorting, etc., an alert message would be shown to save the edited record or cancel the Grid action as in the below screenshot.

We can avoid or prevent this alert message by using “ShowConfirmDialog” Grid EditSettings API.

But preventing the alert message will result in losing the edited records on performing Grid action like paging.

To prevent the alert message and at the same time to save the edited records, we can make use “ActionBegin Grid event in which using “batchSave“ Grid method we can save the edited records.

 

JS

<script type="text/javascript">
        $(function () {
            $("#Grid").ejGrid({
 
                dataSource: window.gridData,
                
                allowPaging: true,
                
                editSettings: { allowEditing: true, allowAdding: true, allowDeleting: true, editMode: "batch" },
                
                toolbarSettings: { showToolbar: true, toolbarItems: [ej.Grid.ToolBarItems.Add, ej.Grid.ToolBarItems.Edit, ej.Grid.ToolBarItems.Delete, ej.Grid.ToolBarItems.Update, ej.Grid.ToolBarItems.Cancel] },
                
                actionBegin: “begin”,
                
                columns: [
                        { field: "OrderID", isPrimaryKey: true, headerText: "Order ID", textAlign: ej.TextAlign.Right, width: 90 },
                        { field: "CustomerID", headerText: 'Customer ID', validationRules: { required: true,}, width: 90 },
                        { field: "EmployeeID", headerText: 'Employee ID', editType: ej.Grid.EditingType.Dropdown, textAlign: ej.TextAlign.Right, width: 80 },
                        { field: "Freight", headerText: 'Freight', textAlign: ej.TextAlign.Right, editType: ej.Grid.EditingType.Numeric, width: 80, format: "{0:C}" },
                        { field: "ShipName", headerText: 'Ship Name', width: 150 },
                        { field: "ShipCountry", headerText: 'Ship Country', editType: ej.Grid.EditingType.Dropdown, width: 90 }
                ]
            });
        });
    </script>

 

MVC

@(Html.EJ().Grid<object>("Editing")
        .Datasource((IEnumerable<object>)ViewBag.datasource)
 
        .EditSettings(edit => { edit.AllowAdding().AllowDeleting().AllowEditing().EditMode(EditMode.Batch).ShowConfirmDialog(false); })
 
        .ToolbarSettings(toolbar =>
        {
            toolbar.ShowToolbar().ToolbarItems(items =>
            {
                items.AddTool(ToolBarItems.Add);
                items.AddTool(ToolBarItems.Edit);
                items.AddTool(ToolBarItems.Delete);
                items.AddTool(ToolBarItems.Update);
                items.AddTool(ToolBarItems.Cancel);
            });
        })
 
        .AllowPaging()
 
        .Columns(col =>
        {
            col.Field("OrderID").HeaderText("Order ID").IsPrimaryKey(true).TextAlign(TextAlign.Right).Width(90).Add();
            col.Field("CustomerID").HeaderText("Customer ID").ValidationRules(v => v.AddRule("required", true)).Width(90).Add();
            col.Field("EmployeeID").HeaderText("Employee ID").EditType(EditingType.Dropdown).TextAlign(TextAlign.Right).Width(80).Add();
            col.Field("Freight").HeaderText("Freight").TextAlign(TextAlign.Right).Width(80).EditType(EditingType.Numeric).Format("{0:C}").Add();
            col.Field("ShipName").HeaderText("ShipName").Width(150).Add();
            col.Field("ShipCountry").HeaderText("ShipCountry").EditType(EditingType.Dropdown).Width(90).Add();
        })
 
        .ClientSideEvents(eve => eve.ActionBegin("begin"))
        )

 

ASP.NET

<ej:Grid ID="OrdersGrid" runat="server" AllowPaging="True">
 
            <Columns>
                <ej:Column Field="OrderID" HeaderText="Order ID" IsPrimaryKey="true" TextAlign="Right" Width="75" />
                <ej:Column Field="CustomerID" HeaderText="Customer ID" Width="80" >
                    <ValidationRule>
                        <ej:KeyValue Key="required" Value="true" />
                    </ValidationRule>
                </ej:Column>
                <ej:Column Field="EmployeeID" HeaderText="Employee ID" TextAlign="Right" Width="80" EditType="Dropdown" />
                <ej:Column Field="Freight" HeaderText="Freight" TextAlign="Right" Width="80" Format="{0:C}" EditType="Numeric" />
                <ej:Column Field="ShipName" HeaderText="ShipName" Width="150" />
                <ej:Column Field="ShipCountry" HeaderText="ShipCountry" Width="90" EditType="Dropdown" />
            </Columns>
 
            <EditSettings AllowEditing="True" AllowAdding="True" AllowDeleting="True" EditMode="Batch"></EditSettings>
 
            <ToolbarSettings ShowToolbar="True" ToolbarItems="add,edit,delete,update,cancel"></ToolbarSettings>
 
            <ClientSideEvents ActionBegin="begin" />
 
        </ej:Grid>

 

<script>
    function begin(args) {
       
         if (args.requestType == "paging" | args.requestType == "sorting") {
        
              if (this.model.isEdit && !this.getContent().find("#EditingEditForm").validate().form()) {
          
                  args.cancel = true; // Preventing Grid action in case of any validation error
            }
            
         var changes = this.getBatchChanges();// getBatchChanges method returns unsaved edited, added and deleted records.
 
         
         if (!args.cancel && (this.model.isEdit || changes.added.length || changes.deleted.length || changes.changed.length)) {
      
             this.batchSave(); // Saving edited records
 
            }
        }
    }
    
</script>

 

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