如何在Telerik RadGrid中提供AutoSave功能?

时间:2016-03-29 05:51:33

标签: asp.net telerik telerik-grid radgrid autosave

我正在使用带有批量修改功能的Telerik RadGrid。我正在尝试实现自动保存功能。我在下面添加了我的代码

<form id="form1" runat="server">
         <div id="Demo">
        <telerik:RadListBox RenderMode="Lightweight" runat="server" ID="SavedChangesList" Width="600px" Height="200px" Visible="false"></telerik:RadListBox>
        <telerik:RadGrid RenderMode="Lightweight" ID="RadGrid1" GridLines="None" runat="server" AllowAutomaticDeletes="True"
            AllowAutomaticInserts="True" PageSize="10" OnItemDeleted="RadGrid1_ItemDeleted" OnItemInserted="RadGrid1_ItemInserted"
            OnItemUpdated="RadGrid1_ItemUpdated" OnPreRender="RadGrid1_PreRender" AllowAutomaticUpdates="True" AllowPaging="True"
            AutoGenerateColumns="False" OnBatchEditCommand="RadGrid1_BatchEditCommand" DataSourceID="SqlDataSource1">
            <MasterTableView CommandItemDisplay="TopAndBottom" DataKeyNames="ID"
                DataSourceID="SqlDataSource1" HorizontalAlign="NotSet" EditMode="Batch" AutoGenerateColumns="False">
                <BatchEditingSettings EditType="Cell" />
                <SortExpressions>
                    <telerik:GridSortExpression FieldName="ID" SortOrder="Descending" />
                </SortExpressions>
                <Columns>

                </Columns>
            </MasterTableView>
            <ClientSettings AllowKeyboardNavigation="true"></ClientSettings>
        </telerik:RadGrid>
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=STRMV3097\MSSQLSERVER2012;Initial Catalog=ToolsandTechResearchShowcase;User ID=pra_app_user;Password=pra"
        DeleteCommand="Delete Command" InsertCommand="Insert Command"
        SelectCommand="Select" UpdateCommand="Update">
    </asp:SqlDataSource>
</form>

1 个答案:

答案 0 :(得分:0)

创建计时器(使用本机浏览器setInterval()方法)和网格的客户端API(请参阅saveChanges(tableView)saveAllChanges()方法):http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/edit-mode/batch-editing/client-side-api。您需要对批处理编辑管理器的引用,例如grid.get_batchEditingManager(),其中grid是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>

我还建议为其他批处理编辑事件添加处理程序,以便在用户当前正在编辑单元格/行时提高标记和/或取消间隔,以防止数据丢失和问题。