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:
- The JavaScript used to get the selected MessageBox value doesn't work in NN.
- 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:
- Public properties (Text for the button's text value, and Message for the MessageBox).
- Postback Data - handles posting data back to the server
- 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
<DefaultProperty("Text"), ToolboxData("<{0}:ServerConfirmButton runat=server></{0}:ServerConfirmButton>")> Public Class ServerConfirmButton Inherits System.Web.UI.WebControls.WebControl Implements IPostBackDataHandler Implements IPostBackEventHandler
#Region "Properties"
Protected _strText As String <Bindable(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 <Bindable(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: '<INPUT type="hidden" name="hdn1" value="none"> 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("<!-- This component was created by www.timstall.com -->")
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
|
links: digg this technorati