维护页面数量DropDownList的回传后刷新服务器超时 [英] Maintaining page number in DropDownList after postback to refresh server timeout

查看:213
本文介绍了维护页面数量DropDownList的回传后刷新服务器超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我继承了这个Web应用程序。它采用.NET用VB。我有一个弹出来允许用户刷新服务器引导他们在他们连接的计时器。在应用程序页面的三个使用完全相同的风格的DropDownList为了控制正在显示的内容。

当用户在这三个页面中的一个,并显示超时弹出,当用户击的OK以刷新我得到DropDownList中只能有一个项目选择了一个错误。

我如何让它在相同的状态,这是在pre-弹出回传?

谢谢!

\r
\r

保护小组ddlPages_SelectedIndexChanged1(BYVAL发件人为对象,BYVAL E上System.EventArgs)\r
        切换到页面中选择寻呼机\r
        昏暗gvrPager作为GridViewRow = gvSelectEvents.BottomPagerRow\r
        昏暗ddlPages作为DropDownList的= DirectCast(gvrPager.Cells(0).FindControl(ddlPages),DropDownList的)\r
\r
        gvSelectEvents.PageIndex = ddlPages.SelectedIndex\r
\r
        填充您的网格\r
        gvSelectEvents.DataBind()\r
    结束小组

\r

<%@控制语言=VBAutoEventWireup =false的codeFILE =TimeOutControl.ascx.vb\r
    继承=UserControls_TimeOutControl%GT;\r
<%@注册大会=AjaxControlToolkit命名空间=AjaxControlToolkit标签preFIX =CC1%GT;\r
\r
<脚本类型=文/ JavaScript的>\r
    变种DoLogout = 1;\r
    VAR WarnMills;\r
    VAR TimeoutMills;\r
    VAR WarnDurationMills;\r
    VAR的redirectUrl;\r
    VAR _timeLeft;\r
\r
    功能StartTimeout(TimeoutValue,WarnValue,WarnDuration,URLValue){\r
        TimeoutMills = TimeoutValue;\r
        WarnMills = WarnValue;\r
        WarnDurationMills = WarnDuration;\r
        的redirectUrl = URLValue;\r
        的setTimeout('UserTimeout()',TimeoutMills);\r
        的setTimeout('WarnTimeout()',WarnMills);\r
    }\r
\r
    功能UserTimeout(){\r
        如果(DoLogout == 1){\r
            top.location.href =的redirectUrl;\r
        }\r
        其他{\r
            DoLogout = 1;\r
            的setTimeout('UserTimeout()',TimeoutMills);\r
            的setTimeout('WarnTimeout()',WarnMills);\r
        }\r
    }\r
\r
    功能WarnTimeout(){\r
        _timeLeft =(WarnDurationMills / 1000);\r
        updateCountDown();\r
        找到$('mdlSessionTimeout')显示()。\r
    }\r
\r
    功能updateCountDown(){\r
        变种分钟= Math.floor(_timeLeft / 60);\r
        VAR秒= _timeLeft%60;\r
        如果(秒-1; 10)\r
            秒=0+秒;\r
\r
        。的document.getElementById(CountDownHolder)的innerHTML = MIN +:+秒;\r
\r
        如果(_timeLeft大于0){\r
            _剩下的时间 - ;\r
            的setTimeout('updateCountDown()',1000);\r
        }\r
    }\r
\r
    函数preserveSession(){\r
        DoLogout = 0;\r
        __doPostBack('BTN preserveSession','');\r
    }\r
< / SCRIPT>

\r

< PagerTemplate>\r
                        < ASP:ImageButton的ID =ImageButton1=服务器的ImageUrl =图片/ firstpage.gif\r
                            CommandArgument =第一的CommandName =页面/>\r
                        < ASP:ImageButton的ID =ImageButton2=服务器的ImageUrl =图像/ prevpage.gif\r
                            CommandArgument =preV的CommandName =页面/>\r
                        <跨度风格=颜色:白色;>页面< / SPAN>\r
                        < ASP:DropDownList的ID =ddlPages=服务器的AutoPostBack =真OnSelectedIndexChanged =ddlPages_SelectedIndexChanged1>\r
                        < / ASP:DropDownList的>\r
                        <跨度风格=颜色:白色;> / SPAN>&LT的;\r
                        < ASP:标签ID =lblPageCount=服务器前景色=白>< / ASP:标签>\r
                        < ASP:ImageButton的ID =ImageButton3=服务器的ImageUrl =图片/ nextpage.gif\r
                            CommandArgument =下一步的CommandName =页面/>\r
                        < ASP:ImageButton的ID =ImageButton4=服务器的ImageUrl =图片/ lastpage.gif\r
                            CommandArgument =最后的CommandName =页面/>\r
                    < / PagerTemplate>

\r

\r
\r


解决方案

您不需要刷新页面,以保持会话活跃。相反,你可以只使用AJAX调用一个方法在.asmx文件的具有 EnableSession:= TRUE

有关的code的一个例子:

 导入System.Web.Services
进口System.Web.Services.Protocols
进口System.ComponentModel要允许此Web服务从脚本调用,使用ASP.NET AJAX,取消注释以下行。
&所述; System.Web.Script.Services.ScriptService()> _
< System.Web.Services.WebService(命名空间:=htt​​p://tempuri.org/)> _
< System.Web.Services.WebServiceBinding(ConformsTo:= WsiProfiles.BasicProfile1_1)GT; _
<的ToolboxItem(假)> _
公共类SessionKeepAlive
    继承System.Web.Services.WebService    '这个功能是从母版页每19分钟称为
    保持会话活着。
    <的WebMethod(EnableSession:= TRUE)> _
    < Script.Services.ScriptMethod(UseHttpGet:= TRUE)> _
    公共功能检查()作为字符串
        如果HttpContext.Current.Session(用户名)是没有那么
            返回过期
        其他
            返回OK
        万一
    结束功能末级

如果你使用的是< ASP:ScriptManager的> 时,JavaScript的AJAX可能看起来像:

 <脚本类型=文/ JavaScript的>
    //<![CDATA [
    VAR的keepalive =功能(){
        Sys.Net.WebServiceProxy.invoke(SessionKeepAlive.asmx,检查,真实,{},SessionKeepAlive_Callback);
    }    功能SessionKeepAlive_Callback(结果,EventArgs的){
        如果(结果!='OK'){
            警报('您的会话超时\\ n请再次登录。');
            window.location的='login.aspx的';
        };
    }
    //设置19分钟.NET会话保持活定时器...
    window.setInterval(KEEPALIVE,19 * 60 * 1000);
    //]]>
< / SCRIPT>

您可以选择调用该方法在Web服务不同的方式,例如通过使用jQuery。

Hello I've inherited this web app. It uses .net with VB. I have a timer that pops up to allow the user to refresh their connection before the server boots them. Three of the pages in the app use the exact same style DropDownList in order to control the content that is being displayed.

When the user is on one of these three pages and the timeout pop up is displayed, when the user hit's ok to refresh I get an error that the dropdownlist can only have one item selected.

How do I make it postback in the same state that it was in pre-popup?

Thanks!

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>

解决方案

You don't need to refresh the page to keep the session active. Instead, you can just use AJAX to call a method in an .asmx file which has EnableSession:=True.

An example of the code for that:

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

And if you were using an <asp:ScriptManager>, the JavaScript for the AJAX could look like:

<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>                                                                                                     

You might choose a different way of calling the method in the web service, e.g. by using jQuery.

这篇关于维护页面数量DropDownList的回传后刷新服务器超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆