实现 JSON + Jquery+.Net ajax功能

现在ajax技术应用越来越广泛 ,我也比较感兴趣今天正好也有个地方用到啦,现学现用,与朋友share一下
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>jquery 与asp.net加强应用</title>

    <script src="jquery-1.4.2.js" type="text/javascript"></script>
    <script type="text/javascript">
        //ajax加载客户基本信息
        function GetFriends() {
            $.getJSON("default.aspx", { name: " dfsf" }, function(data) {
                GetData(data);

            })
        }
        function GetData(data) {

            debugger
            alert(data.friend[0].name);
         }
     
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="button"  onclick ="GetFriends()"/>
    
    </div>
    </form>
</body>
</html>

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.Text;
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            GetJSON();
        }

        public DataTable GetDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("id");
            dt.Columns.Add("name");
            dt.Columns.Add("sex");
            dt.Columns.Add("age");
            dt.Columns.Add ("discription");
            DataRow dr = dt.NewRow();
            dr["id"] = 1;
            dr["name"] = "赵红叶";
            dr["sex"] = "女";
            dr["age"] = "20";
           
            dr["discription"] = "红福爱人";
            dt.Rows.Add(dr);
            dr = dt.NewRow();
            dr["id"] = 2;
            dr["name"] = "李明莉";
            dr["sex"] = "女";
            dr["age"] = "20";
            dr["discription"] = "红福的好朋友";
            dt.Rows.Add(dr);
            return dt;
        }
        public  string DataTableToJson(string jsonName, DataTable dt)
        {
            StringBuilder Json = new StringBuilder();
            Json.Append("{\"" + jsonName + "\":[");
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Json.Append("{");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        Json.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":\"" + dt.Rows[i][j].ToString() + "\"");
                        if (j < dt.Columns.Count - 1)
                        {
                            Json.Append(",");
                        }
                    }
                    Json.Append("}");
                    if (i < dt.Rows.Count - 1)
                    {
                        Json.Append(",");
                    }
                }
            }
            Json.Append("]}");
            return Json.ToString();
        }

        public void GetJSON()
        {
            if (Request.QueryString["name"] != null)
            {
                Response.Clear();
                Response.ContentType = "text/plain";
                string json = DataTableToJson("friend", GetDataTable());
                Response.Write(json);
                Response.End();
            }
        }


    }
}

 

posted on 2010-09-17 21:09  HelloHongfu  阅读(270)  评论(0编辑  收藏  举报

导航