利用前端Ajax过段时间加载一次后台防止Session过期
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="js/Jquery1.7.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
function post() {
$.ajax({
type: "post",
contentType: "application/json",
url: "WebBlock.aspx"
})
}
setInterval(post,30000)
})
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" Height="409px" TextMode="MultiLine"
Width="449px"></asp:TextBox>
<asp:Button ID="Button1"
runat="server" Text="保存" OnClientClick="" onclick="Button1_Click" />
</div>
</form>
</body>
</html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; namespace WebApplication2 { public partial class WebBlock : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { } protected void Button1_Click(object sender,EventArgs e) { if (Session["user"] != null) { FileStream stream = File.Open(@"d:\1.txt",FileMode.OpenOrCreate,FileAccess.Write);//向电脑中写入数据 StreamWriter writer = new StreamWriter(stream); writer.Write(TextBox1.Text); writer.Dispose(); stream.Dispose(); } else { Response.Redirect("Error.aspx");//当Session没传过来时执行此语句 } } } }
原文链接:https://www.f2er.com/ajax/166148.html