DataTableConverter.cs
上传用户:szgaoree
上传日期:2009-01-05
资源大小:74k
文件大小:4k
- using System;
- using System.Text;
- using System.Data;
- namespace AjaxPro
- {
- /// <summary>
- /// Provides methods to serialize and deserialize a DataTable object.
- /// </summary>
- public class DataTableConverter : IJavaScriptConverter
- {
- private string clientType = "Ajax.Web.DataTable";
- public DataTableConverter() : base()
- {
- }
- public override string GetClientScript()
- {
- return @"addNamespace(""Ajax.Web"");
- Ajax.Web.DataTable = function(columns, rows) {
- this.__type = ""System.Data.DataTable, System.Data"";
- this.Columns = new Array();
- this.Rows = new Array();
- this.addColumn = function(name, type) {
- var c = new Object();
- c.Name = name;
- c.__type = type;
-
- this.Columns.push(c);
- }
- this.toJSON = function() {
- var dt = new Object();
- dt.Columns = [];
- for(var i=0; i<this.Columns.length; i++)
- dt.Columns.push([this.Columns[i].Name, this.Columns[i].__type]);
- dt.Rows = [];
- for(var i=0; i<this.Rows.length; i++) {
- var row = [];
- for(var j=0; j<this.Columns.length; j++)
- row.push(this.Rows[i][this.Columns[j].Name]);
- dt.Rows.push(row);
- }
- return AjaxPro.toJSON(dt);
- }
- this.addRow = function(row) {
- this.Rows.push(row);
- }
- if(columns != null) {
- for(var i=0; i<columns.length; i++) {
- this.addColumn(columns[i][0], columns[i][1]);
- }
- }
- if(rows != null) {
- for(var i=0; i<rows.length; i++) {
- var row = new Object();
- for(var c=0; c<this.Columns.length && c<rows[i].length; c++) {
- row[this.Columns[c].Name] = rows[i][c];
- }
- this.addRow(row);
- }
- }
- }
- ";
- }
- public override object Deserialize(IJavaScriptObject o, Type t)
- {
- if(!(o is JavaScriptObject))
- throw new NotSupportedException();
- JavaScriptObject ht = (JavaScriptObject)o;
- if(!ht.Contains("Columns") || !(ht["Columns"] is JavaScriptArray) ||
- !ht.Contains("Rows") || !(ht["Rows"] is JavaScriptArray))
- {
- throw new NotSupportedException();
- }
- JavaScriptArray columns = (JavaScriptArray)ht["Columns"];
- JavaScriptArray rows = (JavaScriptArray)ht["Rows"];
- DataTable dt = new DataTable();
- DataRow row = null;
- Type colType;
- if(ht.Contains("TableName") && ht["TableName"] is JavaScriptString)
- dt.TableName = ht["TableName"].ToString();
- foreach(JavaScriptArray column in columns)
- {
- colType = Type.GetType(column[1].ToString(), true);
- dt.Columns.Add(column[0].ToString(), colType);
- }
-
- JavaScriptArray cols = null;
- foreach(JavaScriptArray r in rows)
- {
- // if(!(r is JavaScriptArray))
- // continue;
-
- cols = (JavaScriptArray)r;
- row = dt.NewRow();
- for(int i=0; i<cols.Count; i++)
- row[i] = JavaScriptDeserializer.Deserialize((IJavaScriptObject)cols[i], dt.Columns[i].DataType);
- dt.Rows.Add(row);
- }
-
- return dt;
- }
- public override string Serialize(object o)
- {
- if(!(o is DataTable))
- throw new NotSupportedException();
-
- StringBuilder sb = new StringBuilder();
- DataTable dt = (DataTable)o;
-
- DataColumnCollection cols = dt.Columns;
- DataRowCollection rows = dt.Rows;
- bool b = true;
- sb.Append("new ");
- sb.Append(clientType);
- sb.Append("([");
-
- foreach(DataColumn col in cols)
- {
- if(b){ b = false; }
- else{ sb.Append(","); }
- sb.Append('[');
- sb.Append(JavaScriptSerializer.Serialize(col.ColumnName));
- sb.Append(',');
- sb.Append(JavaScriptSerializer.Serialize(col.DataType.FullName));
- sb.Append(']');
- }
- sb.Append("],[");
- b = true;
- foreach(DataRow row in rows)
- {
- if(b){ b = false; }
- else{ sb.Append(","); }
- sb.Append(JavaScriptSerializer.Serialize(row));
- }
- sb.Append("])");
- return sb.ToString();
- }
- public override Type[] SerializableTypes
- {
- get
- {
- return new Type[]{typeof(DataTable)};
- }
- }
- public override Type[] DeserializableTypes
- {
- get
- {
- return new Type[]{typeof(DataTable)};
- }
- }
- }
- }