今天用asp.net配置jqgrid,一天都没有成功,相当的郁闷,请高手指点,我的分不多,希望见谅
用的是示例数据库Northwind
以下为前台代码
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jqgrid.aspx.cs" Inherits="jQueryPages.jqgrid" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>jqgrid </title>
<link rel="Stylesheet" type="text/css" href="jqGrid/themes/coffee/grid.css" />
<script type="text/javascript" src="jqGrid/jquery.js"> </script>
<script type="text/javascript" src="jqGrid/js/grid.base.js"> </script>
<script type="text/javascript" src="jqGrid/js/grid.locale-cn.js"> </script>
<script type="text/javascript">
$(document).ready(function(){
$("#list").jqGrid({
url:'jqgrid.ashx',
datatype: 'json',
mtype: 'GET',
colNames:['OrderId','CustomerId', 'ShipName'],
colModel :[
{name:'OrderId', index:'OrderId', width:100},
{name:'CustomerId', index:'CustomerId', width:100},
{name:'ShipName', index:'ShipName', width:100, align:'right'}],
pager: $('#pager'),
rowNum:10,
rowList:[10,20,30],
sortname: 'id',
sortorder: "desc",
viewrecords: true,
imgpath: 'jqgrid/themes/basic/images',
caption: 'My first grid'
});
});
</script>
</head>
<body>
<form id="form1" runat="server" enableviewstate="false">
<table id="list" class="scroll"> </table>
<div id="pager" class="scroll" style="text-align:center;"> </div>
</form>
</body>
</html>
以下为jqgrid.ashx代码
using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace jQueryPages
{
/// <summary>
/// $codebehindclassname$ 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class jqgrid1 : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
//不让浏览器缓存
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
context.Response.ContentType = "text/plain";
DataTable dt = SqlHelper.ExecuteDataSet("select top 20 OrderId,CustomerId,ShipName from Orders order by Orderid asc").Tables[0];
string jsonData = JsonHelper.JsonForJqgrid(dt);
context.Response.Write(jsonData);
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
以下为上面ashx中引用的JsonForJqrid类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Newtonsoft.Json;
using System.Text;
using System.IO;
namespace jQueryPages
{
public class JsonHelper
{
public JsonHelper()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
public static string DataTableToJSON(DataTable dt, string dtName)
{
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jw = new JsonTextWriter(sw))
{
JsonSerializer ser = new JsonSerializer();
jw.WriteStartObject();
jw.WritePropertyName(dtName);
jw.WriteStartArray();
foreach (DataRow dr in dt.Rows)
{
jw.WriteStartObject();
foreach (DataColumn dc in dt.Columns)
{
jw.WritePropertyName(dc.ColumnName);
ser.Serialize(jw, dr[dc].ToString());
}
jw.WriteEndObject();
}
jw.WriteEndArray();
jw.WriteEndObject();
sw.Close();
jw.Close();
}
return sb.ToString();
}
public static string JsonForJqgrid(DataTable dt)
{
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.Append("{\"");
jsonBuilder.Append("\"page\":1,\"total\":" + Northwind.GetPageCount("Orders") + ",\"records\":" + dt.Rows.Count + ",\"rows\"");
jsonBuilder.Append("\":[");
for (int i = 0; i < dt.Rows.Count; i++)
{
jsonBuilder.Append("{");
for (int j = 0; j < dt.Columns.Count; j++)
{
jsonBuilder.Append("\"");
jsonBuilder.Append(dt.Columns[j].ColumnName);
jsonBuilder.Append("\":\"");
jsonBuilder.Append(dt.Rows[i][j].ToString());
jsonBuilder.Append("\",");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
jsonBuilder.Append("}");
return jsonBuilder.ToString();
}
}
}
运行后不出现任何错误,断点Json数据时,和jqgrid的要求的Json数据格式是相符的,但是内容怎么着都不出来
不知道这里怎么上传附件,我可以将解决方案全部传上来就好了
恩,爱莫能助阿~
恩,但是,给你个建议把,有时候程序输出的字符串可能因为特别容易忽略的问题,使得js端不能运行,你现不要用从数据库中读出来的数据帮定
可以拿jqgird的提供的实例的字符串帮定,一点点的排除问题
@黑曜石: 你是怎么弄的,我也遇到同样的问题了,求救!
我还是不行,我现在吧var jsondata=json;把拿回来的数据这样保存一次,用local模式调用jsondata就可以了,..,,.迷茫
遇到过一样的问题
我现在遇到和你一样的问题,请问怎么解决呢?谢谢