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