实现过程是这样的:前台通过jquery的ajax调用一般处理程序(handler),获取表格需要显示的信息,然后转换成json格式返回给前台,前台获取到数据后循环构建表格的行,最好把行附加到表里。感兴趣的朋友一起看看吧
前言:
本示例大概功能是前台通过JQuery的Ajax调用一般处理程序(Handler),获取表格需要显示的信息,然后转换成json格式返回给前台,前台获取到数据后循环构建表格的行,最好把行附加到表里。
目标:
a 熟悉简单JQuery Ajax的使用
b 了解如何构造基本的Json格式的数据(构建Json也可以通过第三方的dll)
c 熟悉下handler的基本用法
1 简单效果图
2 前台代码
nbsp;html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">#pTb { width:800px; border:1px solid #aaa; margin:0 auto; } .even{background:#CCCCCC;} .odd{background:#FFFFFF;} //获取发布模块类型 function getModuleInfo() { $.ajax({ type: "GET", dataType: "json", url: "../Handler/TestHandler.ashx?Method=GetModuleInfo", //data: { id: id, name: name }, success: function(json) { var typeData = json.Module; $.each(typeData, function(i, n) { var tbBody = "" var trColor; if (i % 2 == 0) { trColor = "even"; } else { trColor = "odd"; } tbBody += "" + n.ModuleNum + "" + "" + n.ModuleName + "" + "" + n.ModuleDes + ""; $("#myTb").append(tbBody); }); }, error: function(json) { alert("加载失败"); } }); } $(function() { getModuleInfo(); });
登录后复制
3 Handler代码
using System;using System.Web;using System.Collections.Generic;using System.Text;using DataDAL;using DataEnity;public class TestHandler : IHttpHandler { HttpRequest Request; HttpResponse Response; public void ProcessRequest (HttpContext context) { //不让浏览器缓存 context.Response.Buffer = true; context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1); context.Response.AddHeader("pragma", "no-cache"); context.Response.AddHeader("cache-control", ""); context.Response.CacheControl = "no-cache"; context.Response.ContentType = "text/plain"; Request = context.Request; Response = context.Response; string method = Request["Method"].ToString(); System.Reflection.MethodInfo methodInfo = this.GetType().GetMethod(method); methodInfo.Invoke(this, null); } public void GetModuleInfo() { StringBuilder sb = new StringBuilder(); string jsonData = string.Empty; List lsModule = ModuleDAL.GetModuleList(); sb.Append("{"Module":["); for (int i = 0; i 0) sb = sb.Remove(sb.Length - 1, 1); sb.Append("]}"); Response.Write(sb); } public bool IsReusable { get { return false; } }}
登录后复制
上面是我整理给大家的,希望今后会对大家有帮助。
相关文章:
使用Yii整合的pjax(pushstate+ajax)实现无刷新加载页面
浅析Asp.net MVC 中Ajax的使用
基于h5 ajax实现手机定位
以上就是JQuery Ajax动态生成Table表格的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2757699.html