Recently a colleague asked with ASP.NET AJAX Call Web Service can return DataTable do? Now backstage company projects many of which are straight with AJAX make out, to achieve asynchronous results, the company's practice is to use Web Service returns a List to the front to JavaScript for Parse, Parse process took adjust many programs and performance, so the question is If direct returns a DataTable how wonderful! ?
Recently a colleague asked with ASP.NET AJAX Call Web Service can return DataTable do?
The company now projects background with AJAX as many of them straight away, to achieve the effect of asynchronous,
The company's practice is to use a List Web Service to return to the front-end JavaScript for Parse,
Parse process took many programs and tune performance, so the question is, if they can be directly returned DataTable the more wonderful! ?
Yes, to return DataTable is actually feasible to use ASP.NET 2.0 AJAX Futures indeed there are ways to be done,
But, the Solution is still CTP, and over a long time, I do not know when it would release the final version, or go down the drain up?
Later, I came up with a solution that can achieve the "feels like" to return a DataTable, that is to use JSON,
As used herein, a C # Class to be resolved can be passed directly into this class of DataTable Method as parameters,
The Method will teach this DataTable refined into a JSON Format String, so the Web Service return type is a String Type,
C # Class (DT2JSON) as follows:
001 using ...System.Data;002 using System.Text;
003
004 public class DT2JSON
005 ...{
006 /////
007 /// converting DataTable to JSON format string (not specified query field)
008 /// use the JavaScript: OBJ.Table [0] .Row [0] .Cell [0] .Data
009 ///
010 /// DataTable
011 ///
012 public static string JSON_DataTable(DataTable dt)
013 ...{
014 StringBuilder JsonString = new StringBuilder();
015 JsonString.Append("...{ "");
016 JsonString.Append(""Table":[...{ "
043 JsonString.Append("]}]});
044 return JsonString.ToString();
045 });
017 JsonString.Append(""Row":[ ");
018 for (int i = 0; i < dt.Rows.Count; i++)
019 ...{
020 JsonString.Append("...{ "
038 else
039 ...{
040 JsonString.Append("]});
041 }, ");
021 JsonString.Append(""Cell":[ ");
022 for (int j = 0; j < dt.Columns.Count; j++)
023 ...{
024 if (j < dt.Columns.Count - 1)
025 ...{
026 JsonString.Append("...{"," + ""Value":"" + dt.Rows[i][j].ToString() + ""});
027 }
028 else if (j == dt.Columns.Count - 1)
029 ...{
030 JsonString.Append("...{"" + ""Value":"" + dt.Rows[i][j].ToString() + ""});
031 }
032 }
033
034 if (i == dt.Rows.Count - 1)
035 ...{
036 JsonString.Append("]});
037 } "
042 }
046
047 /////
048 /// converting DataTable to JSON format string (query fields can be specified)
049 /// use the JavaScript: OBJ.Data [0] .Name
050 ///
051 /// DataTable
052 ///
053 public static string CreateJsonParameters(DataTable dt)
054 ...{
055 StringBuilder JsonString = new StringBuilder();
056 if (dt != null && dt.Rows.Count > 0)
057 ...{
058 JsonString.Append("...{ "
084 JsonString.Append("]});
085 return JsonString.ToString();
086 });
059 JsonString.Append(""Data":[ ");
060 for (int i = 0; i < dt.Rows.Count; i++)
061 ...{
062 JsonString.Append("...{ "
079 else
080 ...{
081 JsonString.Append("});
082 }, ");
063 for (int j = 0; j < dt.Columns.Count; j++)
064 ...{
065 if (j < dt.Columns.Count - 1)
066 ...{
067 JsonString.Append(""" + dt.Columns[j].ColumnName.ToString() + "":" + """ + dt.Rows[i][j].ToString() + "",");
068 }
069 else if (j == dt.Columns.Count - 1)
070 ...{
071 JsonString.Append(""" + dt.Columns[j].ColumnName.ToString() + "":" + """ + dt.Rows[i][j].ToString() + """);
072 }
073 }
074
075 if (i == dt.Rows.Count - 1)
076 ...{
077 JsonString.Append("});
078 } "
083 }"
087 else
088 ...{
089 return null;
090 }
091 }
092
093 /////
094 /// converting DataTable to JSON format string (query fields can be specified)
095 /// use the JavaScript:. OBJ [DataTable Name] [0] .Name
096 ///
097 /// DataTable名称
098 /// DataTable
099 ///
100 public static string CreateJsonParameters(string dtName, DataTable dt)
101 ...{
102 StringBuilder JsonString = new StringBuilder();
103 if (dt != null && dt.Rows.Count > 0)
104 ...{
105 JsonString.Append("...{ "
131 JsonString.Append("]});
132 return JsonString.ToString();
133 });
106 JsonString.AppendFormat(""...{0}":[ ", dtName.Trim());
107 for (int i = 0; i < dt.Rows.Count; i++)
108 ...{
109 JsonString.Append("...{ "
126 else
127 ...{
128 JsonString.Append("});
129 }, ");
110 for (int j = 0; j < dt.Columns.Count; j++)
111 ...{
112 if (j < dt.Columns.Count - 1)
113 ...{
114 JsonString.Append(""" + dt.Columns[j].ColumnName.ToString() + "":" + """ + dt.Rows[i][j].ToString() + "",");
115 }
116 else if (j == dt.Columns.Count - 1)
117 ...{
118 JsonString.Append(""" + dt.Columns[j].ColumnName.ToString() + "":" + """ + dt.Rows[i][j].ToString() + """);
119 }
120 }
121
122 if (i == dt.Rows.Count - 1)
123 ...{
124 JsonString.Append("});
125 } "
130 }"
134 else
135 ...{
136 return null;
137 }
138 }
139 }
Web Service Method used:
01 [WebMethod]02 public string GetDataTable()
03 ...{
04 DataTable dt = new DataTable();
05
06 dt.Columns.Add(new DataColumn("Name", typeof(string)));
07 dt.Columns.Add(new DataColumn("LastName", typeof(string)));
08 dt.Columns.Add(new DataColumn("Email", typeof(string)));
09
10 dt.Rows.Add("何", "若瑄", "[email protected]");
11 dt.Rows.Add("毛", "俊杰", "[email protected]");
12
13 return DT2JSON.JSON_DataTable(dt);
14 }
ASPX JavaScript use, here is the approach taken jQuery, JavaScript approach is similar,
call usage can be parameterized C # Class, I have to write notes in.
Here the introduction of the 3 JS File
first one is the main jQuery
second is to let the VS 2008 SP1 support jQuery intellisense
third is the jQuery Plugin, use it to Parse JSON (download)
02
03
04
In this way, you can simply call the JSON data, transfer data between pages using JSON is really very convenient,
it is worth mentioning that JSON is faster, it feels like Array, and XML it? Although JSON faster than slower a bit,
but can show data formats would be very powerful willow!
Original: Big Box ASP.NET AJAX Call Web Service, Return JSON Format String