C# 把数据集生成为JSON格式 并在使用Jquery 获取JSON数据

生成JSON格式数据是后台文件 Handler.ashx

此处我用的是.ashx文件,这个文件类似于.aspx文件,可以通过它来调用HttpHandler类,从而免去了普通.aspx页面的控件解析以及页面处理的过程。这个文件特别适合于生成动态图片,生成动态文本等内容

Handler.ashx

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

using System;
using System.Web;
using System.Text;
using DataAccess.XX;
using BusinessLogic.XX;
public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        string action = context.Request["act"].ToString();

        if (action == "ajaxinitial")
        {
            int initial = int.Parse(context.Request["initial"].ToString());
            Ag.XXXDataTable funcInfo = new Agent().GetData();
            Ag.XXXRow funcInfoRow = null;
            StringBuilder sb = new StringBuilder();
            if (funcInfo.Rows.Count > 0)
            {
                if (initial * 18 >= funcInfo.Rows.Count)
                {
                    for (int i = (initial-1) * 18; i < funcInfo.Rows.Count; i++)
                    {
                        funcInfoRow = ((AgentInfo.Func_listRow)funcInfo.Rows[i]);
                        if (i == (initial - 1) * 18)
                            sb.Append("[{");
                        else
                            sb.Append("{");
                        for (int j = 0; j < funcInfo.Columns.Count; j++)
                        {

                            sb.Append(funcInfo.Columns[j].ColumnName + ":");
                            sb.Append("/"");
                            sb.Append(funcInfoRow[j].ToString());
                            if (j < funcInfo.Columns.Count - 1)
                                sb.Append("/",");
                            else
                                sb.Append("/"");


                        }
                        sb.Append("}");
                        if (i < funcInfo.Rows.Count-1)
                            sb.Append(",");
                        else
                            sb.Append("]");


                    }

                }
                else
                {
                    for (int i = 0; i < 18; i++)
                    {
                        funcInfoRow = ((AgentInfo.Func_listRow)funcInfo.Rows[(initial - 1) * 18 + i]);
                        if (i == 0)
                            sb.Append("[{");
                        else
                            sb.Append("{");
                        for (int j = 0; j < funcInfo.Columns.Count; j++)
                        {

                            sb.Append(funcInfo.Columns[j].ColumnName + ":");
                            sb.Append("/"");
                            sb.Append(funcInfoRow[j].ToString());
                            if (j < funcInfo.Columns.Count - 1)
                                sb.Append("/",");
                            else
                                sb.Append("/"");


                        }
                        sb.Append("}");
                        if (i < 17)
                            sb.Append(",");
                        else
                           sb.Append("]");


                    }

                }
                context.Response.ContentType = "text/plain";
                context.Response.Write(sb.ToString());

 

            }
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

前台调用:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SecondHomePage.aspx.cs" Inherits="SecondHomePage" %>

<!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 id="Head1" runat="server">
<title>首页</title>
<link href="resources/css/style.css" rel="stylesheet" type="text/css" />
<script src="resources/js/jquery/jquery-1.3.2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
            $(function() {
             $("#class_common").empty();
             $.getJSON("Handler.ashx",{act:"ajaxinitial",initial:"1"},function(json){
                     $.each(json,function(i,key){
                         if(json[i].status==1)
                            $("<li><a href="+json[i].href+" title="+json[i].title+" ><span><img src='"+json[i].img_use+"' title="+json[i].title+" οnclick=javascript:location.href='"+json[i].href+"' /></span>"+json[i].title+"</a></li>").appendTo("#class_common");
                         else if(json[i].status==2)
                            $("<li><a title="+json[i].title+" ><span><img src='"+json[i].img_nouse+"' title="+json[i].title+" /></span>"+json[i].title+"</a></li>").appendTo("#class_common");
                       });
                    });
            });
    </script>
</head>
<body >
    <form id="form1" runat="server">

        <div class="DataForm" id="DepositList" style="margin-left: 0px; height: 320px; border: 0px;">
            <ul id="class_common">

            </ul>
        </div>
    </body>
</html>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值