JQuery AJAX 通过一般处理程序 取列表

由于上一篇的积累 这一个就简单了

也就是把反回了字符串 显示到table中

 $("#btnSearch").click(function () {
                $.post("CurrentStocklist.ashx", function (result) {
                    $(".table").append(result);
                })
            })

这样

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace UI
{
    /// <summary>
    /// CurrentStockList1 的摘要说明
    /// </summary>
    public class CurrentStockList1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            using (SqlConnection con = new SqlConnection("server =.;uid=sa;pwd=123;database=lt"))
            {
                string s = "select top 100 c.autoid,c.cWhCode,c.cinvCode,c.iQuantity,wh.cWhName,inv.cInvName"
                      + " from currentStock c left join wareHouse wh "
                      + " on c.cWhCode =wh.cWHCode left join inventory inv on c.cInvCode =inv.cInvCode ";

                using (SqlDataAdapter ada = new SqlDataAdapter(s, con))
                {
                    DataTable dt = new DataTable();
                    ada.Fill(dt);

                    foreach (DataRow dr in dt.Rows)
                    {
                        StringBuilder sb = new StringBuilder();

                        sb.Append("<tr>");
                        sb.Append("<td>" + dr["autoid"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cwhCode"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cWHName"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cInvCode"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cInvName"].ToString() + "</td>");
                        sb.Append("<td>" + dr["iquantity"].ToString() + "</td>");

                        sb.Append("</tr>");
                        context.Response.Write(sb.ToString());
                    }
                }

            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
View Code

 也可以通过传参数查询

 $("#btnSearch").click(function () {

                $.post("CurrentStocklist.ashx", { whNames: $("#txtWHNameS").val(), invNames: $("#txtInvNames").val() }, function (result) {
                    $(".table").append(result);
                })
            })

后台代码 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace UI
{
    /// <summary>
    /// CurrentStockList1 的摘要说明
    /// </summary>
    public class CurrentStockList1 : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";

            string whName = (context.Request["whNames"] ??"").ToString();
            string invName = (context.Request["invNames"] ?? "").ToString();

            using (SqlConnection con = new SqlConnection("server =.;uid=sa;pwd=123;database=lt"))
            {
                string s = "select  c.autoid,c.cWhCode,c.cinvCode,c.iQuantity,wh.cWhName,inv.cInvName"
                      + " from currentStock c left join wareHouse wh "
                      + " on c.cWhCode =wh.cWHCode left join inventory inv on c.cInvCode =inv.cInvCode "
                      +"where wh.cWHName like '%"+whName+"%' and inv.cInvName like "+ "'%"+invName+"%'";

                using (SqlDataAdapter ada = new SqlDataAdapter(s, con))
                {
                    DataTable dt = new DataTable();
                    ada.Fill(dt);

                    foreach (DataRow dr in dt.Rows)
                    {
                        StringBuilder sb = new StringBuilder();

                        sb.Append("<tr>");
                        sb.Append("<td>" + dr["autoid"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cwhCode"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cWHName"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cInvCode"].ToString() + "</td>");
                        sb.Append("<td>" + dr["cInvName"].ToString() + "</td>");
                        sb.Append("<td>" + dr["iquantity"].ToString() + "</td>");

                        sb.Append("</tr>");
                        context.Response.Write(sb.ToString());
                    }
                }

            }


        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
View Code

 

转载于:https://www.cnblogs.com/SoftWareIe/p/8657287.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值