再次刷新火灾Asp.Net按钮事件? GUID? [英] Asp.Net Button Event on refresh fires again??? GUID?

查看:143
本文介绍了再次刷新火灾Asp.Net按钮事件? GUID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里最明显的问题是,在刷新按钮事件回顾和重复的帖子在数据库中创建。我已经在这个网站对同样的问题,阅读其他问题。

The obvious issue here is that on refresh a button event is recalled and duplicate posts are created in the database. I have read other questions on this site about the very same issue.

显然,答案是每一个岗位创造一个GUID和检查,以确保该GUID是独一无二的?我真的不知道如何实现这一点。

Apparently the answer is for every post create a GUID and check to make sure the GUID is unique? I am not really sure how to implement this.

这是否意味着在刷新,它会尝试创建一个重复的职位具有相同GUID?

Does that mean on refresh, it will try and create a duplicate post with the same GUID?

如何实现一个GUID到你的数据库?或者,如果这不是答案,究竟是什么?

How do you implement a GUID into your database? Or if this is not the answer, what is?

感谢您!

推荐答案

下面是我用一个RefreshValidator控制。只需拖放您的网页上,并保存到数据库前检查Page.IsValid。您可以添加一个错误消息像其他验证,或者如果你想要做一些特别的东西赶上装修一新的事件。因为它是一个验证,GridView的之类的就已经利用它的通知 - 除了删除或取消操作(是的,我有一个解决了太...定制的GridView控件)

Here's a RefreshValidator control that I use. Just drop it on your page, and check Page.IsValid before saving to the database. You can add an error message like other validators, or catch the Refreshed event if you want to do something special. Since it's a Validator, GridViews and the like will already take notice of it - except for Delete or Cancel actions (yeah, I have a custom GridView that solves that too...)

在code是pretty简单 - 存储GUID到了ControlState-,并在会话的副本。在负荷,比较2。如果他们不一样 - 那么它是一个刷新。冲洗,重复,并创建一个新的GUID,并开始了。

The code is pretty simple - store a GUID into ControlState, and a copy in Session. On load, compare the 2. If they're not the same - then it's a refresh. Rinse, repeat, and create a new GUID and start over.

''' <summary>
''' A validator control that detects if the page has been refreshed
''' </summary>
''' <remarks>If <see cref="SessionState.HttpSessionState" /> is not available or is reset, validator will return Valid</remarks>
Public Class RefreshValidator
   Inherits BaseValidator

   Private isRefreshed As Boolean

   Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
      MyBase.OnInit(e)
      Page.RegisterRequiresControlState(Me)
   End Sub

   Protected Overrides Function SaveControlState() As Object
      Dim obj As Object = MyBase.SaveControlState()
      Return New System.Web.UI.Pair(_pageHashValue, obj)
   End Function

   Protected Overrides Sub LoadControlState(ByVal savedState As Object)
      Dim pair As System.Web.UI.Pair = TryCast(savedState, System.Web.UI.Pair)
      If pair IsNot Nothing Then
         _pageHashValue = TryCast(pair.First, String)
         MyBase.LoadControlState(pair.Second)
      Else
         MyBase.LoadControlState(savedState)
      End If
   End Sub

   Private _pageHashValue As String

   Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
      MyBase.OnLoad(e)
      If HttpContext.Current Is Nothing OrElse HttpContext.Current.Session Is Nothing Then
          isRefreshed = False
          Return
      End If

      ' Get hash value from session
       Dim currHashValue As String = CType(HttpContext.Current.Session(Me.UniqueID & ":pageHashValue"), String)

       If _pageHashValue Is Nothing OrElse currHashValue Is Nothing Then
           ' No page hash value - must be first render
           ' No current hash value. Session reset?
           isRefreshed = False
       ElseIf currHashValue = _pageHashValue Then
           ' Everything OK
           isRefreshed = False
       Else
           ' Was refreshed
           isRefreshed = True
       End If

       ' Build new values for form hash
       Dim newHashValue As String = Guid.NewGuid().ToString()
       _pageHashValue = newHashValue
       HttpContext.Current.Session(Me.UniqueID & ":pageHashValue") = newHashValue
   End Sub

   Protected Overrides Function ControlPropertiesValid() As Boolean
       Return True
   End Function

   Protected Overrides Function EvaluateIsValid() As Boolean
       If isRefreshed Then OnRefreshed(EventArgs.Empty)
       Return Not isRefreshed
   End Function

   Protected Overridable Sub OnRefreshed(ByVal e As EventArgs)
       RaiseEvent Refreshed(Me, e)
   End Sub

   ''' <summary>
   ''' Fires when page is detected as a refresh
   ''' </summary>
   Public Event Refreshed As EventHandler
End Class

这篇关于再次刷新火灾Asp.Net按钮事件? GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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