JQuery Ajax動態產生Table表格_AJAX相關

來源:互聯網
上載者:User

前言:

  本樣本大概功能是前台通過JQuery的Ajax調用一般處理常式(Handler),擷取表格需要顯示的資訊,然後轉換成json格式返回給前台,前台擷取到資料後迴圈構建表格的行,最好把行附加到表裡。

目標:

  a 熟悉簡單JQuery Ajax的使用

  b 瞭解如何構造基本的Json格式的資料(構建Json也可以通過第三方的dll)

  c 熟悉下handler的基本用法

1 簡單效果圖

2 前台代碼

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DialogAjax.aspx.cs" Inherits="JQueryTest_DialogAjax" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">  <title></title>  <link href="../JQueryUi/jquery-ui-1.8.5.custom.css" rel="stylesheet" type="text/css" />  <script src="../JQueryUi/jquery-1.4.2.min.js" type="text/javascript"></script>  <script src="../JQueryUi/jquery-ui-1.8.5.custom.min.js" type="text/javascript"></script>  <style type="text/css">    #divTb    {      width:800px;      border:1px solid #aaa;      margin:0 auto;    }    .even{background:#CCCCCC;}    .odd{background:#FFFFFF;}  </style>  <script type="text/javascript">    //擷取發布模組類型    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 += "<tr class='" + trColor + "'><td>" + n.ModuleNum + "</td>" + "<td>" + n.ModuleName + "</td>" + "<td>" + n.ModuleDes + "</td></tr>";            $("#myTb").append(tbBody);          });        },        error: function(json) {          alert("載入失敗");        }      });    }    $(function() {      getModuleInfo();    });  </script></head><body>  <form id="form1" runat="server">  <div id="divTb">    <table id="myTb" style=" width:100%">    </table>  </div>  </form></body></html>

3 Handler代碼

<%@ WebHandler Language="C#" Class="TestHandler" %>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<Module> lsModule = ModuleDAL.GetModuleList();    sb.Append("{\"Module\":[");    for (int i = 0; i < lsModule.Count; i++)    {      jsonData = "{\"ModuleNum\":" + "\"" + lsModule[i].ModuleNum + "\"" + ",\"ModuleName\":" + "\"" + lsModule[i].ModuleName + "\"" + ",\"ModuleDes\":" + "\"" + lsModule[i].ModuleDes + "\"" + "},";      sb.Append(jsonData);    }    if (lsModule.Count > 0)      sb = sb.Remove(sb.Length - 1, 1);    sb.Append("]}");    Response.Write(sb);  }  public bool IsReusable  {    get {      return false;    }  }}

以上代碼超簡單吧,JQuery Ajax動態產生Table表格的內容就全部完成了,希望對大家有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.