<body> <div> <table id="myTable"></table> </div> </body>
JavaScript code?1234567891011121314151617181920212223242526272829 <script type="text/javascript"> $(function () { $('#myTable').datagrid({ title: 'datagrid', url: 'datagrid_data.ashx', idField: 'id', width: 400, columns: [[ { field: 'name', title: '名字', width: 100 }, { field: 'age', title: '年龄', width: 100 }, { field: 'sex', title: '性别', width: 100 } ]], pagination: true, pageSize: 10, pageList: [10, 20, 30, 40, 50] }); $.ajax({ url: 'datagrid_data.ashx', data: 'aa:1', type: 'post', dataType: 'json', success: function (data) { console.info(data); } }); }) </script>
datagrid_data.ashx:
C# code?public void ProcessRequest(HttpContext context) { GridView<userInfo> gv = new GridView<userInfo>(); gv.rows = getlist(); gv.total = getlist().Count; JavaScriptSerializer json = new JavaScriptSerializer(); string result = json.Serialize(gv); context.Response.Write(result); context.Response.End(); } private DataTable GetTable() { DataTable dt = new DataTable(); dt.Columns.Add("id", typeof(int)); dt.Columns.Add("name", typeof(string)); dt.Columns.Add("age", typeof(int)); dt.Columns.Add("sex", typeof(string)); DataRow dr = dt.NewRow(); string[] name = { "张三", "李四", "王五", "赵六" }; string[] sex = { "男", "女", "女", "男" }; for (int i = 0; i < 4; i++) { dr["id"] = 0 + i; dr["name"] = name[i]; dr["age"] = 14 + i; dr["sex"] = sex[i]; } dt.Rows.Add(dr); return dt; } private IList<userInfo> getlist() { IList<userInfo> list = new List<userInfo>(); DataTable dt = GetTable(); foreach (DataRow dr in dt.Rows) { userInfo info = new userInfo(); info.Id = Convert.ToInt32(dr["id"]); info.Name = Convert.ToString(dr["name"]); info.Age = Convert.ToInt32(dr["age"]); info.Sex = Convert.ToString(dr["sex"]); list.Add(info); } return list; }
GridView
C# code?123456 public class GridView<T> where T : class,new() { public IEnumerable<T> rows { get; set; } public int total; }
打开以后就是这个样子 请问哪里错了呀??
返回的数据是
{"total":1,"rows":[{"Id":3,"Name":"赵六","Age":17,"Sex":"男"}]}
{ field: 'name', title: '名字', width: 100 }
改成{ field: 'Name', title: '年龄', width: 100 }
所有名字要一样,大小写区分