• 防止刷新页面提交的方法


    检查页面是否通过点击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检测即可。

  • 相关阅读:
    MAC OS下安装Minizip
    MAC OS下安装pkg_config
    计算机图形学-几何变换
    计算机图形学-图形系统
    Unix&Linux大学教程 读书笔记【1-3章】
    Mixins 混入选项操作
    watch监控,对比新值和旧值做出相应判断
    用.native修饰器来对外部组件进行构造器内部方法的调用以及用原生js获取构造器里的方法
    methods 方法选项
    computed 计算选项
  • 原文地址:https://www.cnblogs.com/ServiceboyNew/p/2393441.html
Copyright © 2020-2023  润新知