首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Telerik RadGrid中提供RadGrid功能?

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

Stack Overflow用户
提问于 2016-03-29 05:51:32
回答 1查看 1.4K关注 0票数 0

我正在使用一个具有批处理编辑功能的Telerik RadGrid。我正在尝试实现自动保存功能。我在下面添加了我的代码

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

发布于 2016-03-29 13:23:46

创建一个定时器(使用本机浏览器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() ),其中网格是对RadGrid的引用。

下面是一个基本的示例,您应该能够立即运行

代码语言:javascript
复制
        <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>

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36276417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档