方法一:同时处理多个返回值
ListData.cs 类
using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
public class ListData
{
private int pageSize;
public int PageSize
{
get { return pageSize; }
set { pageSize = value; }
}
private int records;
public int Records
{
get { return records; }
set { records = value; }
}
public ListData()
{
}
public ListData(int pageSize, int records)
{
this.PageSize = pageSize;
this.Records = records;
}
public string ToJson()
{
System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
return serializer.Serialize(this);
}
}
一般处理程序ashx:
ListData ldata = new ListData();实例化类
ldata.PageSize = pageSize;//每页显示多少条记录
ldata.Records = recount;//总记录数
context.Response.Write(ldata.ToJson());//输出到页面
页面接收值:
$(function () {
var title = $("#Info_title").text(); //评论主题
var url = '<wtl:system type="Systempath"></wtl:system>sitecn/conmmentOn/ConmmentOnListPage.ashx';//链接地址:一般处理程序地址
var date = { ctent: "0", pageTopic: "page_indexs",title:title };
$.post(url, date, function (getdata) {
var tip = eval("(" + getdata + ")");
$("#page_count").html("" + tip.Records + "");//总条数
$("#page_Size").html("" + tip.PageSize + ""); //每页显示几条
});
});
方法二:处理单个值的处理
前端:
<script type="text/javascript">
function GetPrice(p) {
$(function () {
var url = '{$syspath}Ajax/handler.ashx';
var date = { price: p };
$.post(url, date, function (getdata) {
if (getdata > 0) {
$("#discount_onclick").hide();//隐藏
$("#discount_price").text(getdata);//得到值
}
});
});
}
$(function () {
var url = '{$syspath}Ajax/Discount.ashx';
var date = { state: "0" };
$.post(url, date, function (getdata) {
$("#id_discount").text(getdata);//得到值
});
});
</script>
一般处理程序:
<%@ WebHandler Language="C#" Class="Discount" %>
using System;
using System.Web;
using System.Data;
public class Discount : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
try
{
string result = string.Empty;
//string option1 = string.Empty;
DataTable dt = (Whir.Repository.DbHelper.CurrentDb.Query("select top 1 * from dbo.Whir_Mem_Member where loginName=@0 and isdel=0",new FrontBasePage().GetUserName())).Tables[0];
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
if (!string.IsNullOrEmpty(dr["discount"].ToString()))
{
result = dr["discount"].ToString();
}
}
}
context.Response.Write(result);
}
catch (Exception ex) { }
}
public bool IsReusable {
get {
return false;
}
}
}