2013-10-17 58 views
2

我有一個問題,由於UpdatePanel。我從後面的代碼分配窗口超時(片段在頁面加載)。時間第一次正確分配。但是,當我更改dropdownlist或其他控件導致頁面加載,時間不分配。但是我希望每次頁面加載時都會重置會話時間。由於updatepanel沒有分配窗口setTimeout?

<script language="javascript" type="text/javascript"> 
alert(<%=mintTimeout%>); 
window.setTimeout("endSession();",<%=mintTimeout%>); 
function endSession() 
{ 
    alert("Your session has expired. You will be redirected to the login page."); 
} 

<form id="form1" runat="server"> 
<div> 
    <asp:ScriptManager ID="ScriptManager1" runat="server"> 
    </asp:ScriptManager> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <ContentTemplate> 
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> 
      <asp:ListItem>1</asp:ListItem> 
      <asp:ListItem>2</asp:ListItem> 
      <asp:ListItem>3</asp:ListItem> 
     </asp:DropDownList> 
     <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"> 
      <asp:ListItem>a</asp:ListItem> 
      <asp:ListItem>b</asp:ListItem> 
      <asp:ListItem>c</asp:ListItem> 
     </asp:DropDownList> 
     <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /> 
    </ContentTemplate> 
    </asp:UpdatePanel> 
</div> 
</form> 

和我的代碼背後,是

public int mintTimeout = 0; 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Session.Timeout = 1; 
     mintTimeout = (Session.Timeout) * 60000; 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 

    } 

請幫我通過這個獲得?

回答

1

您需要在每次回發中清除並重新分配超時。您可以使用pageLoad函數。改變你的腳本如下:

var sessionExpiredTimeout = null; 

function pageLoad() { 
    if(sessionExpiredTimeout){ 
     clearTimeout(sessionExpiredTimeout); 
    } 
    sessionExpiredTimeout = setTimeout(endSession, <%= mintTimeout %>); 
} 

function endSession() { 
    alert("Your session has expired. You will be redirected to the login page."); 
} 
+0

非常感謝。它的工作:) – Sudha