New to Telerik UI for ASP.NET MVC? Download free 30-day trial

Persisting Function References of Custom Command Buttons in the Grid


Product Telerik UI for ASP.NET MVC Grid
Progress Telerik UI for ASP.NET MVC version Created with the 2022.3.1109 version


How can I persist the function references of the custom command buttons in the Telerik UI for ASP.NET MVC Grid after serialization?


To achieve the desired scenario:

  1. Create two separate buttons which will be responsible for the loading and persisting of the Grid's state.
  2. Handle the click event of both buttons.
  3. Store the options of the Grid by using the getOptions() method while serializing them within the LocalStorage of the application by using the kendo.stringify() method.
  4. Just before you pass the options to the setOptions method of the Grid upon the state's persistence, add the function reference to the parsed JSON file retrieved from the LocalStorage.
    <a href="#" class="k-button k-button-md k-rounded-md k-button-solid-base"  id="save">Save State</a>
    <a href="#" class="k-button k-button-md k-rounded-md k-button-solid-base" id="load">Load State</a>

        .Columns(columns =>
            columns.Bound(c => c.ContactName).Width(400);
            columns.Command(c => c.Custom("View Details").Click("onClick")).Width(200);
        .HtmlAttributes(new { style = "height: 550px;" })
        .Filterable(ftb => ftb.Mode(GridFilterMode.Row))
        .Reorderable(r => r.Columns(true))
        .Pageable(pageable => pageable
        .DataSource(dataSource => dataSource
            .Read(read => read.Action("Customers_Read", "Grid"))
        function onClick(){
            alert("Details command is clicked !");
        $(document).ready( function () {
            var grid = $("#grid").data("kendoGrid");
            $("#save").click(function (e) { // Handle the Save button's click event.
                localStorage["kendo-grid-options"] = kendo.stringify(grid.getOptions()); // Store the options within the LocalStorage.

            $("#load").click(function (e) { // Handle the Load button's click event.
                var parsedOptions = JSON.parse(localStorage["kendo-grid-options"]); 
                if (parsedOptions) {
                    parsedOptions.columns[1].command[0].click = onClick; // Add the function reference of the custom command.

For the complete implementation of the suggested approach, refer to the Telerik REPL example on persisting function references to a custom command in the Grid.

More ASP.NET MVC Grid Resources

See Also

In this article