前台代码:

 <%@ 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;
            }
        }
    }
}

Ajax实现页面动态加载,添加数据的更多相关文章

  1. layui中,同一个页面动态加载table数据表格

    效果图: 前端代码: <div class="layui-fluid" id="record-user" hidden="hidden" ...

  2. js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的

    js/jquery控制页面动态加载数据 滑动滚动条自动加载事件--转他人的 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样 ...

  3. geotrellis使用(二十三)动态加载时间序列数据

    目录 前言 实现方法 总结 一.前言        今天要介绍的绝对是华丽的干货.比如我们从互联网上下载到了一系列(每天或者月平均等)的MODIS数据,我们怎么能够对比同一区域不同时间的数据情况,采用 ...

  4. Echarts使用及动态加载图表数据

    Echarts使用及动态加载图表数据 官网:http://echarts.baidu.com/ 1.文档 2.实例 名词: 1.统计维度(说明数据) 维度就是统计致力于建立一个基于多方位统计(时间.地 ...

  5. AJAX 动态加载后台数据 绑定select

    <select id="select"> <!--下拉框数据动态加载--> </select> js:(使用jquery) $(document ...

  6. [JS前端开发] js/jquery控制页面动态加载数据 滑动滚动条自动加载事件

    页面滚动动态加载数据,页面下拉自动加载内容 相信很多人都见过瀑布流图片布局,那些图片是动态加载出来的,效果很好,对服务器的压力相对来说也小了很多 有手机的相信都见过这样的效果:进入qq空间,向下拉动空 ...

  7. Echarts动态加载后台数据

    注意:1.用Ajax请求获取后台数据 2.Echarts只能处理Json数据 后台Controller:根据业务需求不同而返回不同数据,我前台要循环遍历Echarts的series进行数据添加,所以后 ...

  8. Echarts使用及动态加载图表数据 折线图X轴数据动态加载

    Echarts简介 echarts,缩写来自Enterprise Charts,商业级数据图表,一个纯JavaScript的图表库,来自百度...我想应该够简洁了 使用Echarts 目前,就官网的文 ...

  9. 使用 Cesium 动态加载 GeoJSON 数据

    前言 需求是这样的,我需要在地图中显示 08 年到现在的地震情况,地震都是发生在具体的时间点的,那么问题就来了,如何实现地震情况按照时间动态渲染而不是一次全部加载出来. 一. 方案分析 这里面牵扯到两 ...

随机推荐

  1. CoreSeek Sphinx 安装

    1.centos Sphinx 先安装 yum install postgresql-libs yum install unixODBC CoreSeek 安装: http://www.coresee ...

  2. 解决Timer回调方法重复调用的问题

    如果Timer的回调方法的执行时间很长,计时器可能在上个回调方法还没有完成的时候再次触发,如果我们只是想让一个回调方法执行完后再执行下一个回调方法,可以这样: private static Timer ...

  3. [大整数乘法] java代码实现

    上一篇写的“[大整数乘法]分治算法的时间复杂度研究”,这一篇是基于上一篇思想的代码实现,以下是该文章的连接: http://www.cnblogs.com/McQueen1987/p/3348426. ...

  4. C#中鼠标划过按钮时候的提示信息

    ToolTip p = new ToolTip();            p.ShowAlways = true;            p.SetToolTip(this.Buton1, &quo ...

  5. LLVM小结

    随笔- 5  文章- 0  评论- 10  LLVM小结   如果说gcc是FSF的传奇,llvm就是Chris Lattner的小清新.当然啦,想具体看看这位四处游山玩水还GPA 4.0的大神和他的 ...

  6. JS放大镜特效(兼容版)

    原理 1.鼠标在小图片上移动时,通过捕获鼠标在小图片上的位置,定位大图片的相应位置 设计 1.页面元素:小图片.大图片.放大镜 2.技术点:事件捕获.定位 1)onmouseover:会在鼠标指针移动 ...

  7. set 类型

    set类型 map 容器是键-值对的集合,好比以人名为键的地址和电话号码. 相反地,set 容器只是单纯的键的集合.map 适用于字典.电话本.商品价目表等类似的模型.set 适用于黑名单.白名单等. ...

  8. UVa-Where's Waldorf?

    题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. C语言相关的经典书籍(附Ebook)

    个人稍微分了一下类,有的看过,有的听人“传说过”,欢迎大家补充和共同学习. 真心经典:每个行业或者领域都有一些东西是不会随时间而逐渐消逝,以下的就是. <C语言程序设计>(第2版.新版) ...

  10. 设置代码ios 6添加日历事件No calendar has been set.错误

    查了好多资料,发明还是不全,干脆自己整理吧,至少保证在我的做法正确的,以免误导读者,也是给自己做个记录吧! 在iOS 6做日历事件添加时,来来回回报错,No calendar has been set ...