本人因工作需要,需要实现页面的局部刷新功能,在网上找了很多资料都不是很完整,本人自己结合网上资料和自己经验多次实验写了一个,在这里记录一下,方便以后查阅。
1.引用工具:Newtonsoft.Json.dll
2.下载Newtonsoft.Json.dll:https://www.cnblogs.com/youmeng/p/4874897.html,如下图
实现table数据的自动刷新
aspx页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="AJAX.WebForm5" %>
对应的CS代码如下:
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace AJAX
{
public partial class WebForm5 : System.Web.UI.Page
{
string action = "";
SQLHelper sql = new SQLHelper();
protected void Page_Load(object sender, EventArgs e)
{
action = Request["action"] == null ? "" : Request["action"];
switch (action)
{
case "Showcn":
ShowCn();
break;
}
}
private void ShowCn()
{
DataTable dr = sql.f2();//连接数据库并返回DataTable的值
if (dr != null)
{
if (dr.Rows.Count != 0)
{
string strJson = ToJson(dr);
WriteJson(strJson);
}
}
}
public static string ToJson(object obj)
{
return NewtonsoftJson(obj);
}
public static string NewtonsoftJson(object obj)
{
return Newtonsoft.Json.JsonConvert.SerializeObject(obj, Newtonsoft.Json.Formatting.None);
}
private static void WriteJson(string str)
{
HttpContext.Current.Response.Write(str);
//HttpContext.Current.Response.ContentType = "text/plain"; //设置MIME格式
HttpContext.Current.Response.End();
}
}
}
2.绑定echart图表
部分代码如下:
上面代码为aspx页面部分代码,cs部分与上面的cs部分雷同,页面布局请自行调整,下图为绑定的echart图表:
总结:本篇文章主要实现ajax动态加载数据,实现实时刷新的功能。
参考链接:https://www.cnblogs.com/youmeng/p/4874897.html



