zoukankan      html  css  js  c++  java
  • 防止刷新页面提交的方法

    检查页面是否通过点击submit按钮而回发的方法是使用IsPostBack,那么有时候我们需要检查一个页面是否被重复刷新过,以便防止重复提交数据。当然,判断方法很多,我这里简单说一下一个比较简单而且容易理解的办法——就是使用Session进行检查(因为Session放在服务端,而且第一次加载访问页面的时候可以进行一个记录,以后就直接判断该页面中的Session是否为null,如果不为null则说明已经刷新过了)。这里我封装了一个控件UserControl,其中代码如下

    [C#]

    namespace WebCSharp
    {
        public partial class RefreshCheck : System.Web.UI.UserControl
        {
            /// <summary>
            /// 是否刷新标识符
            /// </summary>
            public bool ReFreshCheck { get; set; }
            /// <summary>
            /// 获取父页面的类的名称:该名字唯一且不重复,Title可能重复
            /// </summary>
            string parentName = null;
    
            protected override void OnInit(EventArgs e)
            {
                if (Parent.Parent is Page)
                {
                    parentName = Parent.Parent.GetType().Name;
    
                    if (IsPostBack)                  //如果是回发,重新设置第一次加载,不算刷新
                    {
                        ReFreshCheck = false;
                        Session[parentName] = null;  //设置null,表示不是重复加载
                    }
                    else if (Request.UrlReferrer != null && Request.UrlReferrer.ToString() != Request.Url.ToString())
                    {
                        Session[parentName] = null;
                    }
                    else
                    {
                        //检测是否是其它页面Response.Redirect过来的,以免误操作
                        if (Session[parentName] == null)
                        {
                            ReFreshCheck = false;
                            Session[parentName] = true;
                        }
                        else
                        {
                            ReFreshCheck = true;
                        }
                    }
                }
                else
                {
                    throw new Exception("该控件检测本页面是否刷新,故必须放于本页面内!");
                }
            }
    
        }
    }

    [VB.NET]

    Namespace WebCSharp
        Public Partial Class RefreshCheck
            Inherits System.Web.UI.UserControl
            ''' <summary>
            ''' 是否刷新标识符
            ''' </summary>
            Public Property ReFreshCheck() As Boolean
                Get
                    Return m_ReFreshCheck
                End Get
                Set
                    m_ReFreshCheck = Value
                End Set
            End Property
            Private m_ReFreshCheck As Boolean
            ''' <summary>
            ''' 获取父页面的类的名称:该名字唯一且不重复,Title可能重复
            ''' </summary>
            Private parentName As String = Nothing
    
            Protected Overrides Sub OnInit(e As EventArgs)
                If TypeOf Parent.Parent Is Page Then
                    parentName = Parent.Parent.[GetType]().Name
    
                    If IsPostBack Then
                        '如果是回发,重新设置第一次加载,不算刷新
                        ReFreshCheck = False
                            '设置null,表示不是重复加载
                        Session(parentName) = Nothing
                    ElseIf Request.UrlReferrer IsNot Nothing AndAlso Request.UrlReferrer.ToString() <> Request.Url.ToString() Then
                        Session(parentName) = Nothing
                    Else
                        '检测是否是其它页面Response.Redirect过来的,以免误操作
                        If Session(parentName) Is Nothing Then
                            ReFreshCheck = False
                            Session(parentName) = True
                        Else
                            ReFreshCheck = True
                        End If
                    End If
                Else
                    Throw New Exception("该控件检测本页面是否刷新,故必须放于本页面内!")
                End If
            End Sub
        End Class
    End Namespace

    你只要拖拽此控件到任意需要检查的页面(必须放在Page中,建议在<div>下)。然后直接使用RefreshCheck检测即可。

  • 相关阅读:
    body.filters 出现未指明错误
    利用sql server创建可重复运行的存储过程
    有关如何阻止ASP.NET的按钮控件提交页面的小发现
    ASP.NET 2.0中减少ViewState大小的一点小发现
    DataSet 的 Tables 属性对表名大小写敏感性的解惑[翻译]
    利用Sql Server Management Studio 创建视图的问题
    在用数据绑定的时候我为什么不能把焦点移出(Tab out)我的控件?(译)
    什么是 Change Notification,为什么它很重要(译)
    Rms For Sharepoint
    sharepoint 2010 容量边界
  • 原文地址:https://www.cnblogs.com/ServiceboyNew/p/2393441.html
Copyright © 2011-2022 走看看