使用JSOn和asp.net实现jqgrid

时间:2011-09-19 19:48:30

标签: asp.net jqgrid

上次,我问了类似的问题。现在我试着将jqgrid放在一起工作。但是,我试图带网格,但数据没有加载。

这是两个文件 GetData.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Script.Serialization"%>
<%@ Import Namespace="System.Collections.ObjectModel"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Write(GetData());
        Response.End();
        //Response.Write ("Data is being loaded");
    }

    protected string GetData()
    {
                 var list = new Collection<People>
                              {
                                  new People {Id = 1, Name = "John", Gender = 1, IsClosed = false},
                                  new People {Id = 2, Name = "Abel", Gender = 1, IsClosed = false},
                                  new People {Id = 3, Name = "Aaron", Gender = 1, IsClosed = true},
                                  new People {Id = 4, Name = "Tsion", Gender = 2, IsClosed = true},
                                  new People {Id = 5, Name = "Mussie", Gender = 2, IsClosed = true}
                              };

                   return GridData(1, 1, list.Count, list);
    }

    public string GridData(int noOfPages, int startPage, int noOfRecords, Collection<People> list)
    {
        var gridData = new
                           {
                               total = noOfPages,
                               page = startPage,
                               records = noOfRecords,
                               rows = list,
                           };

        var jsonSerializer = new JavaScriptSerializer();
        return jsonSerializer.Serialize(gridData);

    }



    public class People
    {
        public People()
        {
            Name = string.Empty;
            Id = 0;
            Gender = 0;
            IsClosed = false;
        }



        public string Name { get; set; }
        public int Id { get; set; }
        public int Gender { get; set; }
        public bool IsClosed { get; set; }
    }

    public enum Oper
    {
        edit=1,
        del=2,
        add=3
    }



</script>
</html>

JQExample.aspx

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head id="Head1" runat="server">
    <title>JqGrid</title>
    <link href="themes/redmond/jquery-ui-1.8.2.custom.css" rel="stylesheet" type="text/css" />
    <link href="themes/ui.jqgrid.css" rel="stylesheet" type="text/css" />    
    <script src="Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script src="Scripts/jquery.jqGrid.js" type="text/javascript"></script>

</head>
<body>
   <%-- <form id="form1">--%>
    <table id="jsonmap">
    </table>
    <div id="pjmap">
    </div>


    <script type="text/javascript">

        jQuery("#jsonmap").jqGrid({
            url: 'GetData.aspx',
            datatype: 'json',
            colNames: ['ID', 'Name','Active','Gender'],
            colModel: [{
                    name: 'id',
                    index: 'id',
                    width: 35,
                    editable: false,
                    editoptions: {
                    readonly: true,
                    size: 10
                }},                
                {
                    name: 'name',
                    index: 'name',
                    width: 150,
                    align: "left",
                    editable: true,
                    size: 100
                  },
                {
                    name: 'isClosed',
                    index: 'isClosed',
                    width: 100,
                    align: 'left',
                    editable: true,
                    edittype: "checkbox",
                    editoptions: {
                    value: "true:false",
                    formatter: "checkbox"
                }},
                {
                    name: 'gender',
                    index: 'gender',
                    width: 100,
                    formatter:'select',
                    editable: true,
                    edittype: "select", 
                    editoptions: {value: "0:select;1:male;2:female"}
                }],
                rowNum: 10,
                rowList: [2, 5, 10, 15],
                pager: '#pjmap',
                sortname: 'id',
                sortorder: "desc",
                viewrecords: true, 
                jsonReader: {
                    repeatitems: false
                },
                width: 600,
                caption: 'First jqGrid',
                height: '100%',
                editurl: 'GetData.aspx'
            });


            jQuery("#jsonmap").navGrid("#pjmap", {
                    edit: true,
                    add: true,
                    del: true
                },
                {
                    closeAfterEdit: true,
                    reloadAfterSubmit: false
                },
                {
                    closeAfterAdd: true,
                    reloadAfterSubmit: false
                },
                {
                    reloadAfterSubmit: false
                });



    </script>
      <table id="Jqgrid" width="100%">
    </table>
    <div id="pager"></div> 
    </form>
</body>
</html>

现在我想更改要使用ADO.net动态显示的数据。

先生,谢谢你。它现在有效。

我想更改为动态获取数据,这会更改GetData.aspx中的数据源并格式化JQExample.aspx文件中的colNames和ColModel:

 $(document).ready(function () {
           $.ajax({
                            type: 'POST',
                            contentType: "application/json; charset=utf-8",
                           //url: "GetData.aspx", 
                            datatype: 'json',
                            success: function (result) {

//                                colD = result.colData;
//                                colN = result.colNames;
                                var colM = result.colModel;
                                 //alert(result.colModel);
                                jQuery("#jsonmap").jqGrid
                                      (
                                        {
                                      jsonReader: { repeatitems: false, cell: "",id: "0" },
                                            //url: "GetData.aspx", 
                                            datatype: 'json',
                                            mtype: 'POST',
                                            colModel: colM,

                                            data: colD.rootVar,
                                            ColNames: colN,
                                            ColModel: ColM,
                                            height: "auto",
                                            gridview: true,
                                            pager: '#pager',
                                            rowNum: 5,
                                            rowList: [5, 10, 20, 50],
                                            viewrecords: true,
                                            loadComplete: function (data) {
                                                alert('loaded completely'); 
                                             },
                                            loadError: function () {
                                                alert('error'); 
                                             }
                                        });
                            },
                            error: function (x, e) {
                                alert(x.readyState + ' ' + x.status + e.msg);
                            }
                        });

以下代码应替换为GetData.aspx中的ADO.Net连接

var list = new Collection<People>
                              {
                     new People {Id = 1, Name = "John", Gender = 1, IsClosed = false},
                     new People {Id = 2, Name = "Abel", Gender = 1, IsClosed = false},
                     new People {Id = 3, Name = "Aaron", Gender = 1, IsClosed = true},
                     new People {Id = 4, Name = "Tsion", Gender = 2, IsClosed = true},
                     new People {Id = 5, Name = "Mussie", Gender = 2, IsClosed = true}
                              }; 

请您告诉我下一步该怎么做? 我有服务器端数据源(SQL服务器)。那么我应该编写一个select语句来获取记录,页面和排序的总数(排序)在网格中显示的恐惧吗?如果是这样,请给出具有顶部,限制和顺序的示例代码?

上述代码中是否缺少动态获取数据的内容?

1 个答案:

答案 0 :(得分:0)

您的实现有几个问题,特别是如果您希望它可以编辑,而只是加载修复列映射所需的数据。他们不匹配。此外,我认为它区分大小写,因此您需要将colNames更改为以下并更新colModel以匹配这些名称/大小写:

colNames: ['Id', 'Name','IsClosed','Gender']