天天看點

ajxa異步請求資料

<b>方法一:同時處理多個傳回值</b>

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 = '&lt;wtl:system type="Systempath"&gt;&lt;/wtl:system&gt;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 + ""); //每頁顯示幾條

        });

    });

方法二:處理單個值的處理

前端:

&lt;script type="text/javascript"&gt;

    function GetPrice(p) {

        $(function () {

            var url = '{$syspath}Ajax/handler.ashx';

            var date = { price: p };

            $.post(url, date, function (getdata) {

                if (getdata &gt; 0) {

                    $("#discount_onclick").hide();//隐藏

                    $("#discount_price").text(getdata);//得到值

                }

            });

        var url = '{$syspath}Ajax/Discount.ashx';

        var date = { state: "0" };

            $("#id_discount").text(getdata);//得到值

&lt;/script&gt;

一般處理程式:

&lt;%@ WebHandler Language="C#" Class="Discount" %&gt;

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 &amp;&amp; dt.Rows.Count &gt; 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;