Saturday, April 30, 2005

Source Code for MessageBox Button

[This was originally posted at http://timstall.dotnetdevelopersjournal.com/source_code_for_messagebox_button.htm]

A couple people have requested the source code for the ServerConfirm button I wrote. This adds a yes-no MessageBox to a regular html button. Clicking either yes or no triggers the appropriate server event.

Note that I wrote this version over a year ago, and there are some changes I would make:

  1. The JavaScript used to get the selected MessageBox value doesn't work in NN.
  2. This is a custom-rendered control, which is created entirely from scratch - i.e. just html controls. It could be made as an extended control instead. This would still be compiled to a DLL and be reusable among ASP.Net projects (unlike UserControls), but would give the additional advantage of keeping all the WebControl.Button's extra properties that merely the Html button lacks.

I'll cover these in a future blog post. FYI, other blog posts related to this are:

With that all said, here's the source code below. It has three main regions:

  1. Public properties (Text for the button's text value, and Message for the MessageBox).
  2. Postback Data - handles posting data back to the server
  3. Postback Events - has a yes and no event

The Render method renders all the necessary Html and JavaScript to create the button.

Imports System.ComponentModel
Imports System.Web.UI

Namespace Stall.WebControls

    "Text"
), ToolboxData("<{0}:ServerConfirmButton runat=server>")> Public Class ServerConfirmButton
        Inherits System.Web.UI.WebControls.WebControl
        Implements IPostBackDataHandler
        Implements IPostBackEventHandler

#Region
"Properties"

        Protected _strText As
String
        True), Category("Appearance"), DefaultValue("")> Property [Text]() As
String
           
Get
                Return _strText
            End
Get

            Set(ByVal Value As String)
                _strText = Value
            End
Set
        End
Property

        Protected _strMessage As
String
        True), Category("Appearance"), DefaultValue("")> Property Message() As
String
           
Get
                Return _strMessage
            End
Get

            Set(ByVal Value As String)
                _strMessage = Value
            End
Set
        End
Property

#End Region

        Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
           
'output.Write([Text])

            Dim strHdnName As String = Me.UniqueID

           
'generate hidden field to store answer:
           
'
            output.WriteBeginTag("Input")
            output.WriteAttribute("type", "hidden")
            output.WriteAttribute("name", strHdnName)
            output.WriteAttribute("value", "none")
            output.Write(HtmlTextWriter.TagRightChar)

           
'generate button:
            output.WriteBeginTag("Input")
            output.WriteAttribute("value", Me.Text)
            output.WriteAttribute("type", "button")
            output.WriteAttribute("onclick", "javascript:if (confirm('" & _strMessage & "')) {document.all.item('" & strHdnName & "').value = 'Yes';} else{document.all.item('" & strHdnName & "').value = 'No';}" & Page.GetPostBackClientEvent(Me, "EventClicked"))
            output.Write(HtmlTextWriter.TagRightChar)

           
'write out source of control:
            output.Write("")

        End
Sub

#Region
"PostBack Data"

        Protected _blnConfirm As Boolean =
False

        Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements System.Web.UI.IPostBackDataHandler.LoadPostData

           
'Note: a field with Me.UniqueID must be declared for this to fire.
           
'Get the hidden field value so we know which event to trigger
            If (postCollection(postDataKey).Equals("Yes"))
Then
                _blnConfirm =
True
           
Else
                _blnConfirm =
False
            End
If

        End
Function

        Public Sub RaisePostDataChangedEvent() Implements System.Web.UI.IPostBackDataHandler.RaisePostDataChangedEvent
           
'Get the hidden field value so we know which event to trigger
        End
Sub

#End Region

#Region
"PostBack Events"

        Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements System.Web.UI.IPostBackEventHandler.RaisePostBackEvent
           
'check the hidden field to determine which event to raise.
            If (_blnConfirm)
Then
                OnConfirmYes(EventArgs.Empty)
           
Else
                OnConfirmNo(EventArgs.Empty)
            End
If
        End
Sub

        Event ConfirmYes(ByVal sender As Object, ByVal e As EventArgs)

        Protected Overridable Sub OnConfirmYes(ByVal e As EventArgs)
           
'raise the event
            RaiseEvent ConfirmYes(Me, EventArgs.Empty)
        End
Sub

        Event ConfirmNo(ByVal sender As Object, ByVal e As EventArgs)

        Protected Overridable Sub OnConfirmNo(ByVal e As EventArgs)
            RaiseEvent ConfirmNo(Me, EventArgs.Empty)
        End
Sub

#End Region

    End
Class

End
Namespace

No comments:

Post a Comment