在后台把DataTable组织成JSON,送到前台动态填充UltraWebGrid

使用的库是: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 { ///

/// Writes the JSON representation of the object. /// /// The to write to. /// The value. public override void WriteJson(JsonWriter writer, object dataRow, JsonSerializer serializer) { DataRow row = dataRow as DataRow; // *** HACK: need to use root serializer to write the column value // should be fixed in next ver of JSON.NET with writer.Serialize(object) JsonSerializer ser = new JsonSerializer(); writer.WriteStartObject(); foreach (DataColumn column in row.Table.Columns) { writer.WritePropertyName(column.ColumnName); ser.Serialize(writer, row[column]); } writer.WriteEndObject(); } /// /// Determines whether this instance can convert the specified value type. /// /// Type of the value. /// /// true if this instance can convert the specified value type; otherwise, false. /// public override bool CanConvert(Type valueType) { return typeof(DataRow).IsAssignableFrom(valueType); } /// /// Reads the JSON representation of the object. /// /// The to read from. /// Type of the object. /// The object value. public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer) { throw new NotImplementedException(); } } }

 

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 { ///

/// Writes the JSON representation of the object. /// /// The to write to. /// The value. public override void WriteJson(JsonWriter writer, object dataset, JsonSerializer serializer) { DataSet dataSet = dataset as DataSet; DataTableConverter converter = new DataTableConverter(); writer.WriteStartObject(); writer.WritePropertyName("Tables"); writer.WriteStartArray(); BindingFlags bf = BindingFlags.Public | BindingFlags.Static; foreach (DataTable table in dataSet.Tables) { converter.WriteJson(writer, table, serializer); } writer.WriteEndArray(); writer.WriteEndObject(); } /// /// Determines whether this instance can convert the specified value type. /// /// Type of the value. /// /// true if this instance can convert the specified value type; otherwise, false. /// public override bool CanConvert(Type valueType) { return typeof(DataSet).IsAssignableFrom(valueType); } /// /// Reads the JSON representation of the object. /// /// The to read from. /// Type of the object. /// The object value. public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer) { throw new NotImplementedException(); } } }

 

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 { ///

/// Writes the JSON representation of the object. /// /// The to write to. /// The value. public override void WriteJson(JsonWriter writer, object dataTable, JsonSerializer serializer) { DataTable table = dataTable as DataTable; DataRowConverter converter = new DataRowConverter(); writer.WriteStartObject(); writer.WritePropertyName("Rows"); writer.WriteStartArray(); foreach (DataRow row in table.Rows) { converter.WriteJson(writer, row,serializer); } writer.WriteEndArray(); writer.WriteEndObject(); } /// /// Determines whether this instance can convert the specified value type. /// /// Type of the value. /// /// true if this instance can convert the specified value type; otherwise, false. /// public override bool CanConvert(Type valueType) { return typeof(DataTable).IsAssignableFrom(valueType); } /// /// Reads the JSON representation of the object. /// /// The to read from. /// Type of the object. /// The object value. public override object ReadJson(JsonReader reader, Type objectType, JsonSerializer serializer) { throw new NotImplementedException(); } } }

 

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)

你可能感兴趣的:(NetAdvantage,For,Asp.net)