DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX詳解 >> jquery $.getJSON與.NET結合用法推薦
jquery $.getJSON與.NET結合用法推薦
編輯:AJAX詳解     

廢話不多說,給出文件先:

Handler.ashx

代碼 <%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Data;
using System.Text;
public class Handler : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        /*context.Response.ContentType = "text/plain";
        string data = "[{name:\"fan\",age:26},{name:\"wang\",age:25}]";//構建的JSon數據
        context.Response.Write(data);*/

        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        dt.Columns.Add("name", typeof(string));
        dt.Columns.Add("year", typeof(string));
        dt.Columns.Add("avg", typeof(string));
        DataRow tr = dt.NewRow();
        tr["name"] = "shshshdy";
        tr["year"] = "2005-02-02";
        tr["avg"] = "男";
        dt.Rows.Add(tr);
        //ds.Tables.Add(dt);
        DataRow tr2 = dt.NewRow();
        tr2["name"] = "xiaoming";
        tr2["year"] = "2007-02-02";
        tr2["avg"] = "女";
        dt.Rows.Add(tr2);
        ds.Tables.Add(dt);



        context.Response.Clear();
        context.Response.ContentEncoding = Encoding.UTF8;
        context.Response.ContentType = "application/JSon";
        //Response.Write("{\"success\":true,\"name\":\"shshshdy\",\"year\":\"2009-12-12\"}");
        context.Response.Write(getJSON(ds));
        context.Response.Flush();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    private static string getJSON(DataSet ds)//這裡調用了JSON幫助文件,JSonHelp.cs 就不放出源碼了
    {
        Json.JSONHelper JSonHelp = new Json.JSONHelper();

        JSonHelp.success = true;
        //JSonHelp.totlalCount = getPrjListCount();
        JSonHelp.totlalCount = ds.Tables[0].Rows.Count;

        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            JSonHelp.AddItem("name", dr["name"].ToString());
            JSonHelp.AddItem("avg", dr["avg"].ToString());

            if (dr["year"] != DBNull.Value)
            {
                JSonHelp.AddItem("year", Convert.ToDateTime(dr["year"]).ToString("yyyy/MM/dd"));
            }
            else
            {
                JSonHelp.AddItem("year", string.Empty);
            }

            JSonHelp.ItemOk();
        }

        string strResult = JSonHelp.ToString();
        return strResult;
    }
}

 

test.ASPx

代碼     <script src="js/jquery.JS" type="text/Javascript" language="Javascript"></script>
    <script type="text/Javascript">
        $(function(){
            $.getJSON("Handler.ashx",function(JSon){
            $.each(JSon.data,function(i){$('#content').append("姓名:"+JSon.data[i].name+"----性別:"+JSon.data[i].avg+"----時間:"+JSon.data[i].year+"<br/>");});
            });
        });
    </script>     <body>
    <form id="form1" runat="server">
    <div id="content"></div>
    </form>
</body>

 

把Handler.ashx文件內容放入普通ASPx文件page_load方法中同樣適用

即$.getJSON("ssss.ASPx"function(e){});

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved