dev Session和计时器 完成 弹窗无刷新
js代码
var pcLogin;
var tbLogin;
var timer; //声明一个定时器变量
//window.onload = function () {
// pcLogin = ASPxClientPopupControl.Cast('pcLogin');
// tbLogin = ASPxClientButton.Cast('tbLogin');
//}
function pcLogin_Init(s, e) {
ShowLoginWindow();
}
var sesstionTime; //什么一个变量去接收这个值
function Setimeout(time) { //这个方法可以拿到Web.config 从的Session 过期值
sesstionTime = time;
}
//隐藏弹窗
function CloceLoginWindow() {
pcLogin.Hide();
}
function ShowLoginWindow() {debugger
clearInterval(timer);//初始化定时器时间
var dateTime = sesstionTime;//Session过期时间赋值给dateTime;
timer = setInterval(function () { //定时器时间设定
pcLogin.Show();
}, dateTime * 60000); //设置时间 dateTime 1秒 = 1000毫秒 这是 定时器的 计算单位 为毫秒 600000
}
//登录按钮
function Start() {
debugger
var tbLogin = tbLogins.GetText(); //获取Textbox值
var tbPassword = tbPasswords.GetText();
$.ajax({
url: '/ajax/Gps.ashx?action=LoginVerification',
data: { tbLogin: tbLogin, tbPassword: tbPassword },
type: 'post',
dataType: 'json',
success: function (r) {
if (r.info == "ok") {
CloceLoginWindow();
//window.location.reload(); 强制刷新页面的实现代码
} else {
alert("用户名或密码不正确 .");
pcLogin.Show();
}
}
});
}
后端代码 如果你的后端代码是写在 IHttpHandler 里面的化 请引用 using System.Web.SessionState;
不然 SessionHelper.InsertSessionUser(user);获取不到Session的值
private void LoginVerification(HttpContext context)
{
PlatformUser user = null;
var tbLogin = context.Request.Form["tbLogin"];
var tbPassword = context.Request.Form["tbPassword"];
string encyptPass = DESEncrypt.Encrypt(tbPassword, GpsSetting.EncryptKey);
AccountDAL account = new AccountDAL();
user = account.GetValidUser(tbLogin, encyptPass);
string state = string.Empty;
if (user != null)
{
SessionHelper.InsertSessionUser(user);
state = "ok";
}
JsonResult jr = new JsonResult
{
info = state,
//data = dt
};
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
context.Response.Write(JsonConvert.SerializeObject(jr, timeFormat));
context.Response.End();
}
还有前端代码
<script>
Setimeout(<%=Session.Timeout%>) 这句代码是获取 Web.config 里面 Session 设置的 到期值 这样js可以成功拿到
</script>
<%-- 登录页面 --%>
<dx:ASPxPopupControl ID="pcLogin" runat="server" ClientInstanceName="pcLogin"
CloseAction="None" Width="300px" Height="200px" ShowCloseButton="False" CloseOnEscape="True" Modal="True"
PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
HeaderText="登录" AllowDragging="True" PopupAnimationType="None" PopupAction="None">
<%-- <ClientSideEvents PopUp="function(s,e) { ASPxClientEdit.ClearGroup('entryGroup'); tbLogin.Focus(); }" />--%>
<ClientSideEvents Init="pcLogin_Init" />
<ContentCollection>
<dx:PopupControlContentControl runat="server">
<dx:ASPxFormLayout ID="ASPxFormLayout1" runat="server" ColCount="2" AlignItemCaptionsInAllGroups="True" Width="100%">
<Items>
<dx:LayoutItem ColSpan="2" Caption="用户名">
<LayoutItemNestedControlCollection>
<dx:LayoutItemNestedControlContainer runat="server">
<dx:ASPxTextBox ID="tbLogin" runat="server" TextField="tbLoginName" Width="100%" ClientInstanceName="tbLogins">
<ValidationSettings EnableCustomValidation="True" ValidationGroup="entryGroup" SetFocusOnError="True"
ErrorDisplayMode="Text" ErrorTextPosition="Bottom" CausesValidation="True">
<RequiredField ErrorText="用户名必填." IsRequired="True" />
<RegularExpression ErrorText="Login required" />
<ErrorFrameStyle Font-Size="10px">
<ErrorTextPaddings PaddingLeft="0px" />
</ErrorFrameStyle>
</ValidationSettings>
</dx:ASPxTextBox>
</dx:LayoutItemNestedControlContainer>
</LayoutItemNestedControlCollection>
</dx:LayoutItem>
<dx:LayoutItem ColSpan="2" Caption="密码">
<LayoutItemNestedControlCollection>
<dx:LayoutItemNestedControlContainer runat="server">
<dx:ASPxTextBox ID="tbPassword" runat="server" Width="100%" ClientInstanceName="tbPasswords" Password="True">
<ValidationSettings EnableCustomValidation="True" ValidationGroup="entryGroup" SetFocusOnError="True"
ErrorDisplayMode="Text" ErrorTextPosition="Bottom">
<RequiredField ErrorText="密码必填." IsRequired="True" />
<ErrorFrameStyle Font-Size="10px">
<ErrorTextPaddings PaddingLeft="0px" />
</ErrorFrameStyle>
</ValidationSettings>
</dx:ASPxTextBox>
</dx:LayoutItemNestedControlContainer>
</LayoutItemNestedControlCollection>
</dx:LayoutItem>
<dx:LayoutItem HorizontalAlign="Right" ShowCaption="False">
<LayoutItemNestedControlCollection>
<dx:LayoutItemNestedControlContainer runat="server">
<%--<dx:ASPxButton ID="btOK" ClientSideEvents-Click="" runat="server" Text="登录" Width="80px" AutoPostBack="False" Style="float: left; margin-left: 70px" OnClick="btOK_Click">
<%-- <ClientSideEvents Click="function(s, e) { if(ASPxClientEdit.ValidateGroup('entryGroup')) pcLogin.Hide(); }" />--%>
<%-- </dx:ASPxButton>--%>
<dx:ASPxButton ID="btnSearch" ClientInstanceName="btnSearch" Width="80px" runat="server" Text="登录" AutoPostBack="false">
<ClientSideEvents Click="function(){
Start();
}" />
</dx:ASPxButton>
</dx:LayoutItemNestedControlContainer>
</LayoutItemNestedControlCollection>
</dx:LayoutItem>
<dx:LayoutItem HorizontalAlign="Right" ShowCaption="False">
<LayoutItemNestedControlCollection>
<dx:LayoutItemNestedControlContainer runat="server">
<dx:ASPxButton ID="btCancel" runat="server" Text="取消" Width="80px" AutoPostBack="False" Style="float: left; margin-left: 8px" OnClick="btCancel_Click">
<%-- <ClientSideEvents Click="function(s, e) { pcLogin.Hide(); }" />--%>
</dx:ASPxButton>
</dx:LayoutItemNestedControlContainer>
</LayoutItemNestedControlCollection>
</dx:LayoutItem>
</Items>
</dx:ASPxFormLayout>
</dx:PopupControlContentControl>
</ContentCollection>
<ContentStyle>
<Paddings PaddingBottom="5px" />
</ContentStyle>
</dx:ASPxPopupControl>
Web.config
<sessionState mode="InProc" timeout="60"/>
SessionHelper
using GpsWeb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace GpsWeb.Helper
{
public class SessionHelper
{
public static void InsertSessionUser(PlatformUser user)
{
HttpContext ctx = HttpContext.Current;
ctx.Session[GpsSetting.SessionKey] = user;
}
public static PlatformUser GetSessionUser()
{
HttpContext ctx = HttpContext.Current;
var obj = ctx.Session[GpsSetting.SessionKey];
if (obj == null) return null;
return (PlatformUser)ctx.Session[GpsSetting.SessionKey];
}
}
}