回发后维护DropDownList中的页码以刷新服务器超时

时间:2014-10-02 18:15:11

标签: javascript asp.net vb.net

我使用.Net,VB和webforms。我有一个弹出的计时器,允许用户在会话到期之前刷新他们的连接。应用程序中的三个页面使用完全相同的样式DropDownList来控制正在显示的内容。

当用户点击" ok"要刷新包含DropDownList的网页,用户会收到DropDownList只能选择一个项目的错误。

如何在回发后以与预弹出相同的状态进行页面渲染?



Protected Sub ddlPages_SelectedIndexChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
        'switch to page selected in pager
        Dim gvrPager As GridViewRow = gvSelectEvents.BottomPagerRow
        Dim ddlPages As DropDownList = DirectCast(gvrPager.Cells(0).FindControl("ddlPages"), DropDownList)

        gvSelectEvents.PageIndex = ddlPages.SelectedIndex

        'populate your grid
        gvSelectEvents.DataBind()
    End Sub

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="TimeOutControl.ascx.vb"
    Inherits="UserControls_TimeOutControl" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<script type="text/javascript">
    var DoLogout = 1;
    var WarnMills;
    var TimeoutMills;
    var WarnDurationMills;
    var RedirectURL;
    var _timeLeft;

    function StartTimeout(TimeoutValue, WarnValue, WarnDuration, URLValue) {
        TimeoutMills = TimeoutValue;
        WarnMills = WarnValue;
        WarnDurationMills = WarnDuration;
        RedirectURL = URLValue;
        setTimeout('UserTimeout()', TimeoutMills);
        setTimeout('WarnTimeout()', WarnMills);
    }

    function UserTimeout() {
        if (DoLogout == 1) {
            top.location.href = RedirectURL;
        }
        else {
            DoLogout = 1;
            setTimeout('UserTimeout()', TimeoutMills);
            setTimeout('WarnTimeout()', WarnMills);
        }
    }

    function WarnTimeout() {
        _timeLeft = (WarnDurationMills / 1000);
        updateCountDown();
        $find('mdlSessionTimeout').show();
    }

    function updateCountDown() {
        var min = Math.floor(_timeLeft / 60);
        var sec = _timeLeft % 60;
        if (sec < 10)
            sec = "0" + sec;

        document.getElementById("CountDownHolder").innerHTML = min + ":" + sec;

        if (_timeLeft > 0) {
            _timeLeft--;
            setTimeout('updateCountDown()', 1000);
        }
    }

    function PreserveSession() {
        DoLogout = 0;
        __doPostBack('btnPreserveSession', '');
    }
</script>
&#13;
<PagerTemplate>
                        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="images/firstpage.gif"
                            CommandArgument="First" CommandName="Page" />
                        <asp:ImageButton ID="ImageButton2" runat="server" ImageUrl="images/prevpage.gif"
                            CommandArgument="Prev" CommandName="Page" />
                        <span style="color: White;">Page</span>
                        <asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlPages_SelectedIndexChanged1">
                        </asp:DropDownList>
                        <span style="color: White;">of</span>
                        <asp:Label ID="lblPageCount" runat="server" ForeColor="White"></asp:Label>
                        <asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="images/nextpage.gif"
                            CommandArgument="Next" CommandName="Page" />
                        <asp:ImageButton ID="ImageButton4" runat="server" ImageUrl="images/lastpage.gif"
                            CommandArgument="Last" CommandName="Page" />
                    </PagerTemplate>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:1)

您无需刷新页面以保持会话处于活动状态。相反,您可以使用AJAX来调用具有EnableSession:=True 的.asmx文件中的方法。

代码示例:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class SessionKeepAlive
    Inherits System.Web.Services.WebService

    ' this function is to called every 19 minutes from the Master page
    ' to keep the session alive.
    <WebMethod(EnableSession:=True)> _
    <Script.Services.ScriptMethod(UseHttpGet:=True)> _
    Public Function Check() As String
        If HttpContext.Current.Session("username") Is Nothing Then
            Return "expired"
        Else
            Return "ok"
        End If
    End Function

End Class

如果您使用的是<asp:ScriptManager>,则AJAX的JavaScript可能如下所示:

<script type="text/javascript">                                                                               
    //<![CDATA[                                                                                           
    var keepAlive = function() {                                                                          
        Sys.Net.WebServiceProxy.invoke("SessionKeepAlive.asmx", "Check", true, {}, SessionKeepAlive_Callback);
    }                                                                                                     

    function SessionKeepAlive_Callback(result, eventArgs) {                                               
        if (result != 'ok') {                                                                         
            alert('Your session has timed out.\nPlease log in again.');                           
            window.location = 'login.aspx';                                                       
        };                                                                                            
    }                                                                                                     
    // Set 19 minute .NET session keep alive timer...                                                     
    window.setInterval(keepAlive, 19 * 60 * 1000);                                                        
    //]]>                                                                                                 
</script>                                                                                                     

您可以选择另一种在Web服务中调用该方法的方法,例如通过使用jQuery。