# Show Confirmation Popup When User Leaves the Page with Unsaved Grid Changes

## Environment

<table>
 <tr>
  <td>Product</td>
  <td>Telerik UI for ASP.NET MVC Grid</td>
 </tr>
 <tr>
  <td>Progress Telerik UI for ASP.NET MVC version</td>
  <td>2024.4.1112</td>
 </tr>
</table>

## Description

How can I detect changes in an InLine editable Grid when the user tries to leave the page and prompt the user with a warning message about the unsaved changes?

## Solution

To detect the changes in the Grid when the user attempts to navigate away from the page with the Grid, you can check whether there has been a change in the Grid's DataSource. If at least one change occurs, show the prompt.

1. Handle the <a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event" target="_blank">`beforeunload` event</a> of the current window.
2. Get a reference to the Grid and access its DataSource.
3. Check the `dirty` flag of the data items, which indicates that the data item has been changed.
4. Show the popup if at least one change occurs.

```Razor HtmlHelper
    @(Html.Kendo().Grid<ProductViewModel>()
        .Name("grid")
        .Editable(editable => editable.Mode(GridEditMode.InLine))
       ...//  Additional configuration.
    )
```

```JS script
    $(document).ready(function () {
        $(window).bind("beforeunload", function (event) {
            var gridDS = $('#grid').getKendoGrid().dataSource; // Access the Grid's DataSource.
            if (dataSourceHasChanges(gridDS)) {
                return "You have unsaved changes. Are you sure you want to leave this page?"; // Show the popup.
            }
        });
    });

    function dataSourceHasChanges(ds) { // Check the Grid's DataSource for changes.
        var dirty = false;
        $.each(ds.data(), function () {
            if (this.dirty == true) {
                dirty = true;
            }
        });
        return dirty;
    }
```

## More ASP.NET MVC Grid Resources

* [ASP.NET MVC Grid Documentation](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/overview)

* [ASP.NET MVC Grid Demos](https://demos.telerik.com/aspnet-mvc/grid/index)

* [ASP.NET MVC Grid Product Page](https://www.telerik.com/aspnet-mvc/grid)

* [Telerik UI for ASP.NET MVC Video Onboarding Course (Free for trial users and license holders)](https://www.telerik.com/aspnet-mvc/documentation/getting-started/video-onboarding)

* [Telerik UI for ASP.NET MVC Forums](https://www.telerik.com/forums/aspnet-mvc)

## See Also

* [Client-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/kendo-ui/api/ui/grid)
* [Server-Side API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-mvc/api/grid)
* [Server-Side TagHelper API Reference of the Grid for ASP.NET MVC](https://docs.telerik.com/aspnet-core/api/taghelpers/grid)
* [Telerik UI for ASP.NET MVC Breaking Changes](https://www.telerik.com/aspnet-mvc/documentation/backwards-compatibility/2024-backwards-compatibility)
* [Telerik UI for ASP.NET MVC Knowledge Base](https://docs.telerik.com/aspnet-mvc/knowledge-base)
