click event is fired twice on an enhanced Button Control

R

Roberto Kohler

I created an enhanced Button Control in ASP NET 2.0 to add a delete
confirmation message and to disable the button if the user does not have
access.
The new button control has a "ConfirmMessage" property where you can set the
message you want displayed and a "SecurityKey" property to set the security
key for the button.
The securitykey works fine. The button is disabled if the user should not
have access to it.
The problem I have is with delete confirmation message. When the user
accepts the confirm message, the click event is fired twice.

If I look at the html source on the client, the button has 2 onclick
statements:
onclick="javascript:return confirm('Do you wish to delete this record ?');"
onclick="javascript:WebForm_DoPostBackWithOptions(new
WebForm_PostBackOptions(..."

I override the AddAttributesToRender with the attached code.

If I take out the writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
onClickScript) statement, then clicking on the button does nothing

Any help would be appreciated.

Protected Overloads Overrides Sub AddAttributesToRender(ByVal writer As
HtmlTextWriter)

writer.AddAttribute(HtmlTextWriterAttribute.Href, "#")

Dim onClickScript As String = "javascript:"
Dim actionScript As String = String.Empty

' Do not validate in design mode
If Not Me.DesignMode Then
If Not authorizedAccess(Me.SecurityKey.ToString, "E", "B")
Then
Me.Enabled = False
Else
If Me.ShowConfirm Then
actionScript = String.Concat("return confirm('",
EscapeJavaScript(ConfirmMessage), "');")
End If
End If
End If

If actionScript = String.Empty Then
actionScript =
Me.Page.ClientScript.GetPostBackEventReference(Me, "")
End If

If Me.Enabled AndAlso (Not (Me.Page Is Nothing)) Then
onClickScript += String.Concat(confirmScript, actionScript)
writer.AddAttribute(HtmlTextWriterAttribute.Onclick,
onClickScript)
End If

If ShowStatusBarText Then
writer.AddAttribute("onmouseover",
String.Concat("javascript:window.status='", EscapeJavaScript(StatusBarText),
"';return true;"))
writer.AddAttribute("onmouseout",
"javascript:window.status='';return true;")
End If

MyBase.AddAttributesToRender(writer)

End Sub

Protected Overridable Function GetClientValidatedPostback(ByVal
control As Control, ByVal confirmScript As String) As String
Dim text1 As String =
control.Page.ClientScript.GetPostBackEventReference(control, confirmScript)
Return ("{if (typeof(Page_ClientValidate) != 'function' ||
Page_ClientValidate()) " + confirmScript + text1 + "} ")
End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top