前台代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Products.aspx.cs" Inherits="_0726Test.Product.Products" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="../js/jquery.js"></script> <script type="text/javascript"> $(function () { //1.加载表格中的信息 loadComments(); //2.给按钮添加单击事件 $('#btnSend').click(function () { var post_data = $('#form1').serializeArray();//获取序列化表单元素 //将请求提交到一般处理程序 $.post("InsertProduct.ashx", post_data, function (_datetext) { if (_datetext == 1) { alert("添加成功"); loadComments(); } }) }) }) //页面加载事件 function loadComments() { $.getJSON('GetAllProduct.ashx?id=' + Math.random(), null, function (_dataJSON) { //获取tbody标签 var tbodyDate = $('#tbodyDate'); tbodyDate.empty(); //遍历JSON元素,添加到到Tbody for (var i = 0; i < _dataJSON.length; i++) { tbodyDate.append ($('<tr><td>' + _dataJSON[i].Id + '</td>' + '<td>' + _dataJSON[i].Name + '</td>'+ '<td>'+_dataJSON[i].Price+'</td>'+ '<td>'+_dataJSON[i].Sales+'</td></tr>')); } }) } </script> </head> <body> <form id="form1" runat="server"> <table border="1" cellspcing="2" cellpadding="1"> <tr> <td> 名称: </td> <td> <input type="text" name="txtPname" /> </td> </tr> <tr> <td> 价格: </td> <td> <input type="text" name="txtPrice" /> </td> </tr> <tr> <td> 数量: </td> <td> <input type="text" name="txtSales" /> </td> </tr> <tr> <td> </td> <td> <input type="button" id="btnSend" value="提交" /> </td> </tr> </table> </form> <p>--------------------商品列表----------------------</p> <table border="1" cellspacing="2" cellpadding="1" > <thead> <tr> <th> ID </th> <th> 名称 </th> <th> 价格 </th> <th> 数量 </th> </tr> </thead> <tbody id ="tbodyDate"> </tbody> </table> </body> </html>
一般处理程序代码:
GetAllProduct.ashx(加载数据到页面)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; using System.Web.Script.Serialization; namespace _0726Test.Product { /// <summary> /// GetAllProduct 的摘要说明 /// </summary> public class GetAllProduct : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string str = @"Data Source=QH-20160731XBEK\SQLSERVER2008;Initial Catalog=MvcTest;User ID=sa;Password=1234"; SqlConnection conn = new SqlConnection(str); conn.Open(); string selname = "SELECT * FROM MyProduct"; List<MyProduct> products = new List<MyProduct>(); SqlCommand sc = new SqlCommand(selname, conn); SqlDataReader dr = sc.ExecuteReader(); while (dr.Read()) { MyProduct product = new MyProduct(); product.Id = Convert.ToInt32(dr["Id"]); product.Name = dr["ProductName"].ToString(); product.Price = Convert.ToDouble(dr["Price"]); product.Sales = Convert.ToInt32(dr["Sales"]); products.Add(product); } dr.Close(); //把list集合转为JSON类型 JavaScriptSerializer jss = new JavaScriptSerializer(); string jsondata = jss.Serialize(products); context.Response.Clear(); context.Response.Write(jsondata); context.Response.End(); } public bool IsReusable { get { return false; } } } }
InsertProduct.ashx(添加数据)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.SqlClient; namespace _0726Test.Product { /// <summary> /// InsertProduct 的摘要说明 /// </summary> public class InsertProduct : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; MyProduct product = new MyProduct(); product.Name = context.Request.Form["txtPname"]; product.Price = Convert.ToDouble(context.Request.Form["txtPrice"]); product.Sales = Convert.ToInt32(context.Request.Form["txtSales"]); string str = @"Data Source=QH-20160731XBEK\SQLSERVER2008;Initial Catalog=MvcTest;User ID=sa;Password=1234"; SqlConnection conn = new SqlConnection(str); conn.Open(); string insertSQL = "INSERT INTO MyProduct VALUES(@name,@price,@sales)"; SqlCommand sc = new SqlCommand(insertSQL, conn); sc.CommandType = CommandType.Text; SqlParameter p1 = new SqlParameter("@name", product.Name); SqlParameter p2 = new SqlParameter("@price", product.Price); SqlParameter p3 = new SqlParameter("@sales", product.Sales); sc.Parameters.Add(p1); sc.Parameters.Add(p2); sc.Parameters.Add(p3); context.Response.Clear(); int result = sc.ExecuteNonQuery(); ) { context.Response.Write("); } else { context.Response.Write("); } context.Response.End(); conn.Close(); } public bool IsReusable { get { return false; } } } }