ButtonExtensions Module
Life With Nate
Nate's Poetry Page
Resume (PDF)
Nate's Code
ASP.NET 2.0/3.5
ButtonExtensions
CheckBoxValidator
ConditionalRequiredTextValidator
CSSImageMap
CustomStyle
DateTimePicker
DateTimePickerTemplated
LengthValidator
RestrictInputTextBox
RestrictInputValidator
Rollovers
RotatedTextHandler
RotatedTextImage
Rotators
SelectedCountValidator
SlideIntoView
StatesDropDownList
UniqueValueValidatorBase
VB.NET 2.0/3.5
Just For Fun
Windows Phone 7
Description
The ButtonExtensions Module contains several Extension Methods for the Button, ImageButton, and LinkButton controls to simplify the common task of adding a confirmation dialog using JavaScript when submitting a form.
Example
LinkButton Default
LinkButton Custom
Your most recently confirmed click was:
Properties & Methods
AddConfirmation()
This Extension Method extends the Button, ImageButton, and LinkButton by adding a JavaScript confirmation dialog that appears when the button is clicked. The message displayed by the confirmation is "Are you sure you want to click the X button?", where X is button's Text or AlternateText property.
AddConfirmation(ByVal msg As String)
This Extension Method extends the Button, ImageButton, and LinkButton by adding a JavaScript confirmation dialog that appears when the button is clicked. The message displayed by the confirmation is the value entered for the msg parameter.
msg
- The message displayed by the JavaScript confirmation
Source Code
ButtonExtensions.vb:
Imports System.Runtime.CompilerServices Namespace NathanSokalski Public Module ButtonExtensions <Extension()> Public Sub AddConfirmation(ByVal btn As Button) btn.AddConfirmation(String.Format("Are you sure you want to click the {0} button?", btn.Text)) End Sub <Extension()> Public Sub AddConfirmation(ByVal btn As Button, ByVal msg As String) btn.OnClientClick = String.Format("return window.confirm('{0}');", msg) End Sub <Extension()> Public Sub AddConfirmation(ByVal btn As ImageButton) btn.AddConfirmation(String.Format("Are you sure you want to click the {0} button?", btn.AlternateText)) End Sub <Extension()> Public Sub AddConfirmation(ByVal btn As ImageButton, ByVal msg As String) btn.OnClientClick = String.Format("return window.confirm('{0}');", msg) End Sub <Extension()> Public Sub AddConfirmation(ByVal btn As LinkButton) btn.AddConfirmation(String.Format("Are you sure you want to click the {0} button?", btn.Text)) End Sub <Extension()> Public Sub AddConfirmation(ByVal btn As LinkButton, ByVal msg As String) btn.OnClientClick = String.Format("return window.confirm('{0}');", msg) End Sub End Module End Namespace
Remarks
We all know that it is not hard to manually add a confirmation, either declaratively or programmatically, so why would I bother to make this Extension Method? Well, mostly just because I enjoy writing code, but it can make things easier when you want to dynamically generate the confirmation message.