469,945 Members | 2,306 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,945 developers. It's quick & easy.

click event is fired twice on an enhanced Button Control

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.Onclic k,
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.Onclic k,
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.GetPostBackEventReferenc e(control, confirmScript)
Return ("{if (typeof(Page_ClientValidate) != 'function' ||
Page_ClientValidate()) " + confirmScript + text1 + "} ")
End Function
Nov 6 '07 #1
0 2198

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Michael Johnson Jr. | last post: by
3 posts views Thread by Imran Aziz | last post: by
reply views Thread by Demetri | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.