<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestScreen.aspx.cs" Inherits="TestScreen" %>
<!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>
<link href="Script/bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen"/>
<script src="Script/bootstrap/js/bootstrap.js"></script>
<script src="Script/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
setInterval("myInterval()", 500000);//1000为1秒钟
function myInterval() {
}
window.onload = myfunction;
function myfunction() {
$.ajax({
//url: "Handler.ashx",
//type: "GET", 使用注销行可调用下方的一般处理程序
type: "POST",
url: "TestScreen.aspx/GetList",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
debugger
//for (var i = 0; i < data.length; i++) {
// debugger
// var newTr = "<tr><td>" + data[i].医院名称 + "</td><td>" + data[i].科室 + "</td><td>" + data[i].处方号 + "</td></tr>";
// $("#tb").append(newTr);
//}
var obj = $.parseJSON(data.d); //一般处理程序返回值直接装换$.parseJSON(data);
for (var i = 0; i < obj.length; i++) {
var newTr = "<tr class='success'><td>" + obj[i].医院名称 + "</td><td>" + obj[i].科室 + "</td><td>" + obj[i].处方号 + "</td><td>"+obj[i].姓名+"</td><td>"+obj[i].付数+"</td><td>"+obj[i].次数+"</td><td>"+obj[i].包装量+"</td><td>"+obj[i].当前状态+"</td></tr>";
$("#tb").append(newTr);
}
}
});
};
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table class="table table-condensed" id="tb" style="margin-left:20px;margin-right:20px">
<thead>
<tr>
<th>医院名称</th>
<th>科室</th>
<th>处方号</th>
<th>姓名</th>
<th>付数</th>
<th>次数</th>
<th>包装量</th>
<th>当前状态</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</form>
</body>
</html>
using AjaxPro;
using ModelInfo;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.Script.Serialization;
using System.Web.Services;
public partial class TestScreen : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//System.Timers.Timer t = new System.Timers.Timer(1000);
//t.Elapsed += new System.Timers.ElapsedEventHandler(theout);//到达时间的时候执行事件;
//t.AutoReset = true;//设置是执行一次(false)还是一直执行(true);
//t.Enabled = true;//是否执行System.Timers.Timer.Elapsed事件;
//BindLisView();
}
//private void theout(object sender, System.Timers.ElapsedEventArgs e)
//{
// //BindLisView();
//}
[WebMethod]
public static string GetList()
{
//ListView1.DataSource = null;
string siteName = ConfigurationManager.AppSettings["test"];
int topNum = Convert.ToInt32(siteName);
TisanInfoModel rm = new TisanInfoModel();
DataTable dt = rm.GetTisanInfio(1);
//ListView1.DataSource = date;
//ListView1.DataBind();
string strJson = DataTableToJson(dt);
return strJson;//返回一个json字符串
}
private static string DataTableToJson(DataTable table)
{
var JsonString = new StringBuilder();
if (table.Rows.Count > 0)
{
JsonString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
JsonString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JsonString.Append("}");
}
else
{
JsonString.Append("},");
}
}
JsonString.Append("]");
}
return JsonString.ToString();
}
}
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
ModelInfo.TisanInfoModel rm = new ModelInfo.TisanInfoModel();
System.Data.DataTable dt = rm.GetTisanInfio(2);
//ListView1.DataSource = date;
//ListView1.DataBind();
string sss=DataTableToJson(dt);
context.Response.Write(sss);
//return DataTableToJson(dt);
}
public string DataTableToJson(System.Data.DataTable table)
{
var JsonString = new System.Text.StringBuilder();
if (table.Rows.Count > 0)
{
JsonString.Append("[");
for (int i = 0; i < table.Rows.Count; i++)
{
JsonString.Append("{");
for (int j = 0; j < table.Columns.Count; j++)
{
if (j < table.Columns.Count - 1)
{
JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\",");
}
else if (j == table.Columns.Count - 1)
{
JsonString.Append("\"" + table.Columns[j].ColumnName.ToString() + "\":" + "\"" + table.Rows[i][j].ToString() + "\"");
}
}
if (i == table.Rows.Count - 1)
{
JsonString.Append("}");
}
else
{
JsonString.Append("},");
}
}
JsonString.Append("]");
}
return JsonString.ToString();
}
public bool IsReusable {
get {
return false;
}
}
}