62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
AjaxRefresh
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>
AjaxRefresh</h2>
<div id="currentTime">
</div>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
//每秒定时调用后台刷新数据
var date = new Date();
var mytime = date.getFullYear() + "/" + (date.getMonth() + 1) + "/" +
date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
$("#currentTime").html(mytime);
window.setInterval(AjaxRefreshContent, 1000);
})
//定时异步刷新数据库任务
var AjaxRefreshContent = function () {
$.ajax({
url: "/Test/GetCurrentTime",
dataType: "json",
type: "post",
success: function (result) {
$("#currentTime").html(result.currentTime);
}
})
}
</script>
</asp:Content>
public ActionResult AjaxRefresh()
{
return View();
}
/// <summary>
/// 获取当前时间
/// </summary>
/// <returns></returns>
public JsonResult GetCurrentTime()
{
//这里只是用时间做例子你可以自己选择你要选取的数据赋给前台定时刷新
return Json(new { currentTime = DateTime.Now.ToStandardString() });
}
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label runat="server" ID="lblResult"></asp:Label>
<asp:Button runat="server" ID="btnTest" Text="测试" />
</ContentTemplate>
</asp:UpdatePanel>
protected void Page_Init(object sender, EventArgs e) {
btnTest.Click += new EventHandler(btnTest_Click);
}
protected void btnTest_Click(object sender, EventArgs e) {
lblResult.Text = "- -!";
}