简体   繁体   English

将jqGrid与Asp.Net Web窗体一起使用

[英]Using jqGrid with Asp.Net Web Forms

I am using Asp.Net/C# in my application ,I have a requirement where I need to show data in a jqGrid .The data should come from a Users table.This is my first time with jqGrid , can anybody help me out to start with jqGrid with Web Forms .Any examples or links would be much appreciated . 我在我的应用程序中使用Asp.Net/C# ,我有一个要求,我需要在jqGrid显示数据。数据应该来自Users表。这是我第一次使用jqGrid ,任何人都可以帮我开始使用带有Web Forms jqGrid 。非常感谢任何示例或链接。 Thanks. 谢谢。

You can include in any application just an empty table element and empty div for the pager at the bottom of the grid 您可以在任何应用程序中包含一个空表元素和网格底部的寻呼机的空div

<table id="grid" ></table>
<div id="pager"></div>

Then you includes 然后你包括

<script type="text/javascript">
$(function () {
    $("#grid").jqGrid({
        url: 'someURLfromYourProject', // 'xxx.svc', 'xxx.asmx', 'xxx.ashx' ...
        datatype: 'json',
        pager: '#pager',
        gridview: true,
        height: 'auto',
        //... other parameters
    });
});
</script>

which will modify at the runtime the empty table and div in the grid. 这将在运行时修改网格中的空表和div。 The data fir the grid will be get from the URL specified by url option. 网格中的数据将从url选项指定的url获取。

It's important to understand that you can very easy integrate WFC, ASMX web service or ASHX handler in any your existing application. 了解您可以在任何现有应用程序中轻松集成WFC,ASMX Web服务或ASHX处理程序,这一点非常重要。 You need just add new page (choose "Add new Item" context menu in the Solution Explorer of your ASP.NET project) to your existing project. 您只需添加新页面(在ASP.NET项目的解决方案资源管理器中选择“添加新项”上下文菜单)到现有项目。 In the way you will create the code which are more independent from the technology used mainly in your ASP.NET application. 在这种方式中,您将创建与主要在ASP.NET应用程序中使用的技术更加独立的代码。 If you decide later to migrate the application to ASP.NET MVC you can even not change the WFC ASMX web service or ASHX handler part of the application. 如果您稍后决定将应用程序迁移到ASP.NET MVC,则甚至无法更改应用程序的WFC ASMX Web服务或ASHX处理程序部分。

In the answer you will find some URLs with demo projects which you can download and play a little. 答案中,您将找到一些带有演示项目的URL,您可以下载并播放一些。 You can move the most code from the ASP.NET MVC example (see here and here ) to your WCF/ASMX/ASHX code. 您可以将ASP.NET MVC示例中的大多数代码(请参见此处此处 )移动到WCF / ASMX / ASHX代码中。

JQGRID example:- JQGRID示例: -

Download all the script files needed from the link http://www.trirand.com/blog/?page_id=6 从链接http://www.trirand.com/blog/?page_id=6下载所需的所有脚本文件

Design.aspx i have defined the script in the body section of the aspx page. Design.aspx我已经在aspx页面的body部分定义了脚本。

Iam using this grid only for search functionality, So i have disabled the edit and delete functionality Iam仅将此网格用于搜索功能,因此我已禁用编辑和删除功能

  <script src="../JQGridReq/jquery-1.9.0.min.js"></script>
    <script src="../JQGridReq/grid.locale-en.js"></script>
    <link href="../JQGridReq/jquery-ui-1.9.2.custom.css" rel="stylesheet" />
    <script src="../JQGridReq/jquery.jqGrid.js"></script>
    <link href="../JQGridReq/ui.jqgrid.css" rel="stylesheet" />
         <link href="../JQGridReq/themes/start/theme.css" rel="stylesheet" />

          <script type="text/javascript">           
        jQuery("#jQGridDemo").jqGrid({
            url: '../Handlers/JQGridHandler.ashx',
            datatype: "json",
            colNames: ['TableID', 'Region_ID', 'Region_Name', 'ActionDate', 'Field', 'OldValue', 'NewValue', 'TableName', 'Who','Comment'],
            colModel: [
                        { name: 'TableID', index: 'TableID', width: 50, stype: 'text' },
                        { name: 'Region_ID', index: 'Region_ID', width: 50, stype: 'text', sortable: true, editable: false },
                        { name: 'Region_Name', index: 'Region_Name', width: 100, editable: false },
                        { name: 'ActionDate', index: 'ActionDate', width: 80, editable: false, formatter: "date" },
                        { name: 'Field', index: 'Field', width: 100, align: "right", editable: false },
                        { name: 'OldValue', index: 'OldValue', width: 100, align: "right", editable: false },
                        { name: 'NewValue', index: 'NewValue', width: 100, align: "right", editable: false },
                        { name: 'TableName', index: 'TableName', width: 100, sortable: true, editable: false },
                        { name: 'Who', index: 'Who', width: 110, sortable: true, editable: false },
                        { name: 'Comment', index: 'Comment', width: 110, sortable: true, editable: false }
                      ],
            rowNum: 20,
            mtype: 'GET',
            loadonce: true,
            rowList: [10, 20, 30,40,50],
            pager: '#jQGridDemoPager',
            sortname: 'TableID',
            viewrecords: true,
            sortorder: 'desc',
            caption: "",
            editurl: '../Handlers/JQGridHandler.ashx'
        });

        $('#jQGridDemo').jqGrid('navGrid', '#jQGridDemoPager',
                   {
                       //edit: true,
                       //add: true,
                       //del: true,
                       search: true,
                       searchtext: "Search"
                       //addtext: "Add",
                       //edittext: "Edit",
                       //deltext:"Delete"
                   },
                   {   //EDIT
                       //                       height: 300,
                       //                       width: 400,
                       //                       top: 50,
                       //                       left: 100,
                       //                       dataheight: 280,
                       closeOnEscape: true,//Closes the popup on pressing escape key
                       reloadAfterSubmit: true,
                       drag: true,
                       afterSubmit: function (response, postdata) {
                           if (response.responseText == "") {

                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid');//Reloads the grid after edit
                               return [true, '']
                           }
                           else {
                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid'); //Reloads the grid after edit
                               return [false, response.responseText]//Captures and displays the response text on th Edit window
                           }
                       },
                       //editData: {
                       //    EmpId: function () {
                       //        var sel_id = $('#jQGridDemo').jqGrid('getGridParam', 'selrow');
                       //        var value = $('#jQGridDemo').jqGrid('getCell', sel_id, '_id');
                       //        return value;
                       //    }
                       //}
                   },
                   {
                       closeAfterAdd: true,//Closes the add window after add
                       afterSubmit: function (response, postdata) {
                           if (response.responseText == "") {

                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid')//Reloads the grid after Add
                               return [true, '']
                           }
                           else {
                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid')//Reloads the grid after Add
                               return [false, response.responseText]
                           }
                       }
                   },
                   {   //DELETE
                       closeOnEscape: true,
                       closeAfterDelete: true,
                       reloadAfterSubmit: true,
                       closeOnEscape: true,
                       drag: true,
                       afterSubmit: function (response, postdata) {
                           if (response.responseText == "") {

                               $("#jQGridDemo").trigger("reloadGrid", [{ current: true}]);
                               return [false, response.responseText]
                           }
                           else {
                               $(this).jqGrid('setGridParam', { datatype: 'json' }).trigger('reloadGrid')
                               return [true, response.responseText]
                           }
                       },
                       //delData: {
                       //    EmpId: function () {
                       //        var sel_id = $('#jQGridDemo').jqGrid('getGridParam', 'selrow');
                       //        var value = $('#jQGridDemo').jqGrid('getCell', sel_id, '_id');
                       //        return value;
                       //    }
                       //}
                   },
                   {//SEARCH
                       closeOnEscape: true
                   }
            );


    </script>

I have created a generic handler by right clicking the project and add new item as Generic Handler and named it as JQGridHandler.ashx and then placed it in a folder named Handlers for making the ajax calls. 我通过右键单击项目创建了一个通用处理程序,并将新项目添加为Generic Handler并将其命名为JQGridHandler.ashx,然后将其放在名为Handlers的文件夹中以进行ajax调用。

JqGridHandler.ashx.cs:- JqG​​ridHandler.ashx.cs: -

public class JQGridHandler : IHttpHandler
    {
        History myHistory = new History();    

        public void ProcessRequest(HttpContext context)
        {
            try {                   
    List<HistoryDetails> myHistoryDetails = new List<HistoryDetails>();
            myHistoryDetails = myHistory.GetAllHistoryDetails();
            var jsonSerializer = new JavaScriptSerializer();   
         context.Response.Write(jsonSerializer.Serialize(myHistoryDetails));
            }
            catch(Exception ex)
            {

            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

History.aspx.cs:- Iam creating a generic class called HistoryDetails which has all the required properties . History.aspx.cs: -我创建了一个名为HistoryDe​​tails的泛型类,它具有所有必需的属性。 Iam then calling the business logic.GetallHistoryDetails() which gets the records from the database using stored procedure 然后,我调用业务逻辑.GetallHistoryDe​​tails(),它使用存储过程从数据库中获取记录

 public List<HistoryDetails> GetAllHistoryDetails()
        {
            List<HistoryDetails> myHistoryDetails = new List<HistoryDetails>();
            try
            {
                myHistoryDetails = BusinessLogic.GetAllHistoryDetails(IdVal);                
            }
            catch(Exception ex)
            {

            }
            return myHistoryDetails;
        } 

References:- https://www.codeproject.com/Articles/609442/Using-JqGrid-in-ASP-NET 参考文献: - https://www.codeproject.com/Articles/609442/Using-JqGrid-in-ASP-NET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM