使用ajax让session永不过期

前端之家收集整理的这篇文章主要介绍了使用ajax让session永不过期前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

-----------------WebLogin.aspx----------------------------

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

</head>
<body>
<form id="form1" runat="server">
<div>
用户:<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox><br />
密码:<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox><br />
<asp:Button ID="Button1" runat="server" Text="登录" onclick="Button1_Click" />
</div>
</form>
</body>
</html>

---------------------------WebLogin.aspx.cs-------------------------------------

namespace WebApplication2
{
public partial class WebLogin : System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{

}

protected void Button1_Click(object sender,EventArgs e)
{
if (txtUserName.Text=="admin"&& txtPwd.Text=="888888")
{
Session["user"] = txtUserName.Text;
Response.Redirect("WebBlock.aspx");
}
}
}
}

--------------------------WebBlock.aspx----------------------------------

<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"
})
}

})
</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>

-----------------------WebBlock.aspx.cs-----------------------------------------------

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");
}
}
}
}

-------------------------Web.config----------------------------------------

<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
<sessionState timeout="1"/>
</system.web>
</configuration>

-------------------------------Error.aspx---------------------------------------------

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> 登录超时,重新登录 </div> </form> </body> </html>

原文链接:https://www.f2er.com/ajax/166250.html

猜你在找的Ajax相关文章