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