Undvik att en ImageButton klickas mer än en gång

Ville minsta risken för att man skall kunna klicka mer än en gång om det tar lite längre tid att processa sidan man postar till. I min klass som ärver från ImageButton så adderar jag följande kod:
    Private Sub GeneralImageButton_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If Me.PreventDoubleClick Then
            Page.ClientScript.RegisterOnSubmitStatement(Me.GetType, 
                                   "PreventDoubleClick", "setTimeout('PreventDoubleClick()', 500);")
            Dim scriptCode As New StringBuilder()
            scriptCode.AppendLine("function PreventDoubleClick()")
            scriptCode.AppendLine("{")
            scriptCode.AppendFormat("   document.getElementById('{0}').disabled = true;", Me.ClientID)
            scriptCode.AppendLine()
            scriptCode.AppendLine("}")
            Page.ClientScript.RegisterClientScriptBlock(Me.GetType, _
                                    "PreventDoubleClickFunction", scriptCode.ToString(), True)
        End If
    End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s