Создайте таймер (используйте собственный метод браузера setInterval()
) и клиентский API сетки (см. метод saveChanges(tableView)
или saveAllChanges()
): http://docs.telerik.com/devtools/aspnet-ajax/control/grid/data-editing/edit-mode/batch-editing/client-side-api. Вам нужна ссылка на менеджер пакетного редактирования, методы которого это, например, grid.get_batchEditingManager()
, где сетка является ссылкой на RadGrid.
Вот базовый пример, который вы сможете запустить немедленно.
<telerik:RadGrid ID="RadGrid1" runat="server" MasterTableView-EditMode="Batch" AllowAutomaticDeletes="True"
AllowAutomaticInserts="True" AllowAutomaticUpdates="True" AllowPaging="True" PageSize="10" DataSourceID="SqlDataSource1"></telerik:RadGrid>
<telerik:RadCodeBlock runat="server" ID="RadCodeBlock1">
<script>
var autoSaveInterval = null;
//store the interval so you can cancel it at any time you like
function saveGridChanges() {
var grid = $find("<%=RadGrid1.ClientID%>");//reference the grid
var batchEditManager = grid.get_batchEditingManager();//get the batch edit manager
batchEditManager.saveAllChanges();//use its API to save the changes
}
//use the Sys.Application.Load event as this is the earliest point at which IScriptControl client-side objects can be accessed
Sys.Application.add_load(function () {
autoSaveInterval = window.setInterval(saveGridChanges, 30000);
});
</script>
</telerik:RadCodeBlock>
<%--Optionally, add AJAX for a loading indicator--%>
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" Skin="Black"></telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" DefaultLoadingPanelID="RadAjaxLoadingPanel1">
<AjaxSettings>
<telerik:AjaxSetting AjaxControlID="RadGrid1">
<UpdatedControls>
<telerik:AjaxUpdatedControl ControlID="RadGrid1" />
</UpdatedControls>
</telerik:AjaxSetting>
</AjaxSettings>
</telerik:RadAjaxManager>
<%--Data source. Here is the one from the Telerik demo--%>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
DeleteCommand="DELETE FROM [Products] WHERE [ProductID] = @ProductID" InsertCommand="INSERT INTO [Products] ([ProductName], [CategoryID], [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock]) VALUES (@ProductName, @CategoryID, @UnitPrice, @Discontinued, @QuantityPerUnit, @UnitsInStock)"
SelectCommand="SELECT [ProductID], [ProductName], [Products].[CategoryID], [Categories].[CategoryName] as CategoryName, [UnitPrice], [Discontinued], [QuantityPerUnit], [UnitsInStock] FROM [Products] JOIN Categories ON Products.CategoryID=Categories.CategoryID"
UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName, [CategoryID] = @CategoryID, [UnitPrice] = @UnitPrice, [Discontinued] = @Discontinued, [QuantityPerUnit] = @QuantityPerUnit, [UnitsInStock] = @UnitsInStock WHERE [ProductID] = @ProductID">
<DeleteParameters>
<asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
<asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
<asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
<asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
<asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="ProductName" Type="String"></asp:Parameter>
<asp:Parameter Name="CategoryID" Type="Int32"></asp:Parameter>
<asp:Parameter Name="UnitPrice" Type="Decimal"></asp:Parameter>
<asp:Parameter Name="Discontinued" Type="Boolean"></asp:Parameter>
<asp:Parameter Name="QuantityPerUnit" Type="String"></asp:Parameter>
<asp:Parameter Name="UnitsInStock" Type="Int16"></asp:Parameter>
<asp:Parameter Name="ProductID" Type="Int32"></asp:Parameter>
</UpdateParameters>
</asp:SqlDataSource>
Я также советую добавить обработчик для других событий пакетного редактирования, чтобы поднимать флаги и/или отменять интервал, когда пользователь в данный момент редактирует ячейку/строку, чтобы предотвратить потерю данных и проблемы.
person
rdmptn
schedule
29.03.2016