因项目需要,在苦痛挣扎了一天后,做出了一个返回JSON数据。先放在网上,***大家看下有什么地方需要优化没有,第二是避免其他ASP.NET程序员为网上苦苦搜索而找不到相关代码而郁闷。
***步:为了使服务器端查询返回JSON数据,从网上找了个用C#写的JSONHelper类(在此感谢那位不知名的好人),以下是JsonHelper类全部代码。(JsonHelper.cs)
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using System.Web.Script.Serialization;
- /// <summary>
- /// JSONHelper 的摘要说明
- /// </summary>
- public class JSONHelper
- {
- //对应JSON的singleInfo成员
- public string singleInfo = string.Empty;
- protected string _error = string.Empty;
- protected bool _success = true;
- protected long _totalCount = 0;
- protected System.Collections.ArrayList arrData = new ArrayList();
- protected System.Collections.ArrayList arrDataItem = new ArrayList();
- public JSONHelper()
- {
- }
- public static string ToJSON(object obj)
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- return serializer.Serialize(obj);
- }
- public static string ToJSON(object obj, int recursionDepth)
- {
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- serializer.RecursionLimit = recursionDepth;
- return serializer.Serialize(obj);
- }
- //对应于JSON的success成员
- public bool success
- {
- get
- {
- return _success;
- }
- set
- {
- //如设置为true则清空error
- if (success) _error = string.Empty;
- _success = value;
- }
- }
- //对应于JSON的error成员
- public string error
- {
- get
- {
- return _error;
- }
- set
- {
- //如设置error,则自动设置success为false
- if (value != "") _success = false;
- _error = value;
- }
- }
- public long totlalCount
- {
- get { return _totalCount; }
- set { _totalCount = value; }
- }
- //重置,每次新生成一个json对象时必须执行该方法
- public void Reset()
- {
- _success = true;
- _error = string.Empty;
- singleInfo = string.Empty;
- arrData.Clear();
- arrDataItem.Clear();
- }
- public void AddItem(string name, string value)
- {
- arrData.Add("\"" + name + "\":" + "\"" + value + "\"");
- }
- public void ItemOk()
- {
- arrData.Add("<BR>");
- }
- //序列化JSON对象,得到返回的JSON代码
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- sb.Append("{");
- sb.Append("totalCount:" + totlalCount.ToString() + ",");
- sb.Append("success:" + _success.ToString().ToLower() + ",");
- sb.Append("error:\"" + _error.Replace("\"", "\\\"") + "\",");
- sb.Append("singleInfo:\"" + singleInfo.Replace("\"", "\\\"") + "\",");
- sb.Append("data:[");
- int index = 0;
- sb.Append("{");
- if (arrData.Count <= 0)
- {
- sb.Append("}]");
- }
- else
- {
- foreach (string val in arrData)
- {
- index++;
- if (val != "<BR>")
- {
- sb.Append(val + ",");
- }
- else
- {
- sbsb = sb.Replace(",", "", sb.Length - 1, 1);
- sb.Append("},");
- if (index < arrData.Count)
- {
- sb.Append("{");
- }
- }
- }
- sbsb = sb.Replace(",", "", sb.Length - 1, 1);
- sb.Append("]");
- }
- sb.Append("}");
- return sb.ToString();
- }
- }
第二步:新建ASPX文件,做为服务器端,用返回查询的JSON数据(PagingRequest.cs)
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Text;
- namespace ExtJSDemo.Data
- {
- public partial class PagingRequest : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- int start = Convert.ToInt32(Request["start"].ToString()); // ExtJS Paging 必须指定的参数(从第几行记录开始)/ ExtJS默认0是***行记录
- int limit = Convert.ToInt32(Request["limit"].ToString()); // ExtJS Paging 必须指定的参数(每页显示多少行记录)
- JSONHelper jsonHelp = new JSONHelper();
- int TotalRecords = 0;
- DataSet DSet = GET_Product_Data(start, limit, out TotalRecords); // 获取数据
- jsonHelp.success = true;
- jsonHelp.totlalCount = TotalRecords;// 记录总数
- if (DSet != null)
- {
- DataTable DTable = DSet.Tables[0];
- for (int i = 0; i < DTable.Rows.Count; i++)
- {
- // 循环生成JSON代码
- jsonHelp.AddItem("Id", DTable.Rows[i]["Id"].ToString());
- jsonHelp.AddItem("Name", DTable.Rows[i]["Name"].ToString());
- jsonHelp.AddItem("StreetPrice", DTable.Rows[i]["StreetPrice"].ToString());
- jsonHelp.AddItem("TypeName", DTable.Rows[i]["TypeName"].ToString());
- jsonHelp.ItemOk();
- }
- }
- Response.Write(jsonHelp.ToString()); // 输出JSON代码
- }
- /// <summary>
- /// 获取数据
- /// </summary>
- /// <param name="PageIndex">记录索引</param>
- /// <param name="PageSize">每页显示记录数</param>
- /// <param name="TotalRecords">总记录数</param>
- public DataSet GET_Product_Data(int start, int limit,out int TotalRecords)
- {
- SqlConnection _Connection = null;
- SqlDataAdapter _Adapter = null;
- DataSet DSet = null;
- try
- {
- _Connection = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["SQLContionString"].ToString());
- _Connection.Open();
- _Adapter = new SqlDataAdapter("SELECT [Id] FROM [Product] ",_Connection);
- DSet = new DataSet();
- _Adapter.Fill(DSet);
- TotalRecords = Convert.ToInt32(DSet.Tables[0].Rows.Count); // 记录总数
- if (TotalRecords < 1) return null; // 没有记录
- int pageLowerBound = start+1; // 从第几条数据开始
- int pageUpperBound = pageLowerBound + limit; // 每页多少条数据
- StringBuilder sb = new StringBuilder();
- if (TotalRecords >= pageLowerBound)
- {
- for (int i = pageLowerBound; i < TotalRecords && i < pageUpperBound; i++)
- {
- sb.AppendFormat("'{0}',", DSet.Tables[0].Rows[i-1][0].ToString());//构造ID in() 条件,取其中一页
- }
- }
- else
- return null; // 没有记录
- if (sb.Length > 1)
- sb.Remove(sb.Length - 1, 1);//删除***一个逗号
- StringBuilder strSql = new StringBuilder();
- strSql.Append("SELECT a.[Id],a.[Name],a.[StreetPrice],b.TypeName ");
- strSql.Append(" FROM [Product] as a left join ProductType as b on a.Typeid = b.Typeid ");
- strSql.AppendFormat(" where a.[Id] in({0})", sb.ToString());
- _Adapter = new SqlDataAdapter(strSql.ToString(), _Connection);
- DSet = new DataSet();
- _Adapter.Fill(DSet);
- }
- finally
- {
- if (_Connection != null && _Connection.State == ConnectionState.Open)
- {
- _Connection.Close();
- }
- }
- return DSet;
- }
- }
- }
第三步:返回JSON数据页面用作客户端呈现结果的载体(PagingControls.aspx)
- <head id="Head1" runat="server">
- <title></title>
- <link rel="stylesheet" type="text/css" href="../ext3/resources/css/ext-all.css" mce_href="ext3/resources/css/ext-all.css" />
- <mce:script type="text/javascript" src="../ext3/adapter/ext/ext-base.js" mce_src="ext3/adapter/ext/ext-base.js"></mce:script>
- <mce:script type="text/javascript" src="../ext3/ext-all.js" mce_src="ext3/ext-all.js"></mce:script>
- <mce:script type="text/javascript" src="JS/PagingGridPanel.js" mce_src="JS/PagingGridPanel.js"></mce:script>
- <mce:script type="text/javascript"><!--
- Ext.BLANK_IMAGE_URL = "../ext3/resources/images/default/s.gif";
- Ext.onReady(function() {
- Ext.QuickTips.init();
- Ext.form.Field.prototype.msgTarget = "side";
- new PagingGridPanel(); // JS文件
- });
- // --></mce:script>
第四部:当然是写JS脚本啦!!而且肯定使用返回JSON数据
好了,出结果了,大功告成~~~~~~~~~~~~~``