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

标签 javascript asp.net vb.net

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

当用户单击“确定”刷新包含 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>
<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>

最佳答案

您无需刷新页面即可保持 session 处于事件状态。相反,您可以只使用 AJAX 调用 .asmx 文件中的方法,该文件具有 EnableSession:=True

代码示例:

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。

关于javascript - 回发刷新服务器超时后维护 DropDownList 中的页码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26166962/

相关文章:

javascript - 我的 node.js GET 调用从 mongo .find() 返回一个空数组

javascript - Vue-router嵌套路由未加载我的组件页面

javascript - 更改模式 Bootstrap 中的文本

javascript - 无法使用 axios 发送带 header 的 get 请求

javascript - Meteor 更新模板助手

c# - Linq 查询连接不起作用

javascript - 为什么 JS/JQuery 不读取文本框的值?

html - 从 windows server 2008 升级到 2012 后,ASP.net 文件下载在文件末尾附加 html

c++ - 来自 .dll 的函数在 vb.net 中失败,但在 vb6 中有效

c# - This.Task() 到 VB 出错,如何解决?