使用的库是:Newtonsoft.Json 为asp.net 3.5开发的 Beta4版本,获取数据库数据用的是
Microsoft EnterpriseLibrary 4.1
其中扩展了这个库的功能,使之最适合把DataTable,DataSet,DataRow转为JSON模式
另外使用了Jquery的$.getJSON来解析后台传过来的JSON格式
另参考了:http://blog.csdn.net/dujingjing1230/archive/2009/08/28/4495008.aspx 裴旭更网友的文章
http://www.west-wind.com/Weblog/default.aspx
一个老外MVP此人专门写了一个JSON的等一系列的库,叫什么WestWind,呵呵
下面是扩展的Newtonsoft.Json几个类
DataRowConverter.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using Newtonsoft.Json; namespace Utility { public class DataRowConverter : JsonConverter { ///
DataSetConverter .cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Reflection; using System.Text; using Newtonsoft.Json; namespace Utility { public class DataSetConverter : JsonConverter { ///
DataTableConverter.cs
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using Newtonsoft.Json; namespace Utility { public class DataTableConverter : JsonConverter { ///
Serialize.cs
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using Newtonsoft.Json; namespace Utility { public class Serialize { public string Serializer(object value) { Type type = value.GetType(); Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer(); json.NullValueHandling = NullValueHandling.Ignore; json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; if (type == typeof(DataRow)) json.Converters.Add(new DataRowConverter()); else if (type == typeof(DataTable)) json.Converters.Add(new DataTableConverter()); else if (type == typeof(DataSet)) json.Converters.Add(new DataSetConverter()); StringWriter sw = new StringWriter(); Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw); writer.Formatting = Formatting.Indented; writer.QuoteChar = '"'; json.Serialize(writer, value); string output = sw.ToString(); writer.Close(); sw.Close(); return output; } public object Deserialize(string jsonText, Type valueType) { Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer(); json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore; json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace; json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore; json.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; StringReader sr = new StringReader(jsonText); Newtonsoft.Json.JsonTextReader reader = new JsonTextReader(sr); object result = json.Deserialize(reader, valueType); reader.Close(); return result; } } }
调用页面前台
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UltraWebGridJSON.aspx.cs" Inherits="Web.UltraWebGridJSON" %> <%@ Register Assembly="Infragistics35.WebUI.UltraWebGrid.v8.3, Version=8.3.20083.1009, Culture=neutral, PublicKeyToken=7dd5c3163f2cd0cb" Namespace="Infragistics.WebUI.UltraWebGrid" TagPrefix="igtbl" %>
调用页面后台
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.Practices.EnterpriseLibrary.Data; using Utility; using Newtonsoft.Json; namespace Web { public partial class UltraWebGridJSON : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if(Request.QueryString["Json"]=="json") { var sql = @"select id, title,title as test, filename, sortorder, createtime, filesize, width, height, classid from t_dataview order by sortorder"; Database db = DatabaseFactory.CreateDatabase(); var dt = db.ExecuteDataSet(CommandType.Text, sql).Tables[0]; Serialize sel = new Serialize(); var selStr = sel.Serializer(dt); Response.Clear(); Response.Write(selStr); Response.Flush(); Response.End(); } if (!IsPostBack) { } } } }
igtbl_addNew("Grid", 0, false, true); 这个函数解释一下,根据API上面说的,第三个参数是加行的时候,滚动条是否跑到新加的到行
这里设成false了,所以加的时候不会跟着动,第四个参数就是是否把加的那行设为活动行(set the newly added row as the active row for the grid)