PromptButton.vb

Imports System.ComponentModel
Imports System.Web.UI.WebControls

<ToolboxData("<{0}:PromptButton runat=""server"" ConfirmMessage=""Hello!"" />")> _
Public Class PromptButton
  Inherits Button

  Private _ConfirmMessage As String = String.Empty

  ' Our confirmation message or String.Empty if no
  ' confirmation prompt should be displayed on the client.
  Public Overridable Property ConfirmMessage() As String
    Get
      Return _ConfirmMessage
    End Get
    Set(ByVal Value As String)
      _ConfirmMessage = Value
      ' Test for Nothing on this one place, instead of
      ' here and there throughout the implementation.
      If _ConfirmMessage Is Nothing Then
        _ConfirmMessage = String.Empty
      End If
    End Set
  End Property


  Protected Overrides Sub AddAttributesToRender( _
    ByVal writer As System.Web.UI.HtmlTextWriter)

    If _ConfirmMessage.Length > 0 Then
      ' Change embedded apostrophes to corresponding 
      ' escape sequences.
      Dim EscapedMessage As String = Replace(_ConfirmMessage, "'", "\'")
      writer.AddAttribute("onclick", _
        "return confirm('" & EscapedMessage & "');")
    End If
    MyBase.AddAttributesToRender(writer)
  End Sub

End Class