zoukankan      html  css  js  c++  java
  • JQuery's Ajax request a datatable

    How to make a JQuery's Ajax request for a datatable, and display it on the page. Here is my solution, Firstly,  using JQuery to make a async request to ASP.NET common http handler, Secondly retrieve datatable from database in httphanlder class, and formate the datatable to a html table, and respond the plain/text to JQuery's async request.

    The code blow is the front page:

     

    代码
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AjaxAndJQuery._Default" %>
    <!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>JQuery async request a table</title>
    <script language=javascript src=jquery-1.4.2.min.js>
    </script>
    <script language="javascript">
    function DoAsyncCall() {
    $.get(
    "GetDataTable.ashx", { Action: "get", Name: "name" }, function(data, textStatus) {
    //返回的 data 可以是 xmlDoc, jsonObj, html, text, 等等.
    // this;
    divContent.innerHTML = data;
    });
    }
    </script>
    <link rel="Stylesheet" href=Stylesheet1.css />
    </head>
    <body>
    <form id="form1" runat="server">
    <input id="btnAjax" type="button" onclick="DoAsyncCall()" value="Do Async Call"/>
    <hr />
    <div id="divContent"></div>
    </form>
    </body>
    </html>

    The blow is the common http handler:

    代码
    using System.Data;
    using System.Web;
    using System.Web.Services;
    using System.Text;

    namespace AjaxAndJQuery
    {
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo
    = WsiProfiles.BasicProfile1_1)]
    public class GetDataTable : IHttpHandler
    {

    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType
    = "text/plain";
    DataSet dataset
    = DBHelper.Query("select id,name,age from employee");
    DataTable table
    = dataset.Tables[0];
    StringBuilder sb
    = new StringBuilder("<table><tr>");
    for (int i = 0; i < table.Columns.Count; i++)
    {
    sb.Append(
    "<th>" + table.Columns[i].ColumnName + "</th>");
    }
    sb.Append(
    "</tr>");
    for (int i = 0; i < table.Rows.Count; i++)
    {
    sb.Append(
    string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td></tr>",table.Rows[i][0].ToString(),table.Rows[i][1].ToString(),table.Rows[i][2].ToString()));
    }
    sb.Append(
    "</table>");

    context.Response.Write(sb.ToString());
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }
    }
    }

    The blow is sql to create data table:

    代码
    CREATE TABLE [dbo].[Employee](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](50) NULL,
    [Age] [int] NULL,
    CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
    (
    [ID] ASC
    )
    WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    )
    ON [PRIMARY]
  • 相关阅读:
    iReaper
    展望未来,总结过去10年的程序员生涯,给程序员小弟弟小妹妹们的一些总结性忠告(turn)
    用C#写ExtJS代码的开源工具extsharp
    如何你是公司的HR,去招聘asp.net程序员,你会对前来面试的人问什么问题。
    ExtJS 3.0 Designer Preview (官方的IDE可视化工具)
    Asp.net ajax、Anthem.net、Ajax pro三大ajax框架那一种使用比较方便?易于配置?
    C#和ASP.net程序员招聘技能要求
    序列化上面创建的Person对象,使其成为一个JSON字符串
    10大加速Ajax开发的框架
    android 解决wifi断线不稳定的问题终极办法
  • 原文地址:https://www.cnblogs.com/philzhou/p/Ajax_JQuery_Datatable.html
Copyright © 2011-2022 走看看