您的位置:首页 > Web前端 > JQuery

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

2011-05-05 21:20 941 查看
生成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+" onclick=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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐