Click here to Skip to main content
15,867,704 members
Articles / Web Development / ASP.NET
Article

HTML Wait-Confirm Button Server Control

Rate me:
Please Sign up or sign in to vote.
3.50/5 (17 votes)
29 Jul 20021 min read 285K   2.1K   72   28
An article showing a quick way to create a custom control that will prompt the user before submitting, and then will change the text of the button to "Please Wait.." and the mouse cursor will change to an hourglass.

Introduction

I'm providing the source code for a simple derived control that will add some useful functionality. Our goal is to make a submit button that prompts for confirmation before submitting, and if the user confirms then change the text on the button and change the mouse cursor to an hourglass. It has only been tested in Internet Explorer versions 5-6.

There are really only a few sections. The first thing needed is to create a compiled class file that will contain the instructions to write out the client side javascript, write out the html button, and modify the button to run the javascript when clicked.

The javascript we are going to be creating will look like this:

JavaScript
<script language="javascript">
<!--
function __doConfirm(btnWaiter) {
if (confirm("Save changes to database?")) {
 btnWaiter.setAttribute("value","Please Wait...");
 document.body.style.cursor="wait";
 return true;
} return false; }
-->
</script>

Our button will call this boolean function (return __doconfirm(this);) and only submit if it returns true.

Might as well get right to the code. Our class file will look like this, overriding the OnPreRender event to register the script and set the onclick attribute to call the client side function.

VB
Public Class ConfirmWaitButton
    Inherits System.Web.UI.WebControls.Button

    Public _Message As String

    Public Property Message() As String
        Get
            Return _Message
        End Get
        Set(ByVal Value As String)
            _Message = Value
        End Set
    End Property

    Protected Overrides Sub OnPreRender(ByVal e As EventArgs)
        Page.RegisterClientScriptBlock( _
         "__doAlert", _
         "<script language="" javascript"">" & vbCrLf & _
         "<!--" & vbCrLf & _
         "function __doConfirm(btnWaiter) {" & vbCrLf & _
         "if (confirm(""" & _Message & """)) {" & vbCrLf & _
         " btnWaiter.setAttribute(""value"",""Please Wait..."");" & vbCrLf & _
         " document.body.style.cursor=""wait"";" & vbCrLf & _
         " return true;" & vbCrLf & _
         "} return false; }" & vbCrLf & _
         "-->" & vbCrLf & _
         "</script>" _
        )

        Me.Attributes("onclick") = "return __doConfirm(this);"

        MyBase.OnPreRender(e)
    End Sub

End Class

And there it is! that's all it takes to create your own wait/confirm button. Add this as a class file to your VS.NET project and add one to a page like this:

HTML
<%@ Register TagPrefix="MG" Namespace="vbProject" Assembly="vbProject" %>

<MG:ConfirmWaitButton id="btnConfirmUpdate" runat="server" Text="Update" 
                         Message="Save changes to database?">
</MG:ConfirmWaitButton>

You will have access to all of the System.Web.UI.WebControls.Button's events, such as OnClick(). That's it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey7-Feb-12 19:23
professionalManoj Kumar Choubey7-Feb-12 19:23 
QuestionHow to pass the _Message value? Pin
yaroopig31-May-06 16:29
yaroopig31-May-06 16:29 
Questionhow to create a dynamic button and call event Onclick ? Pin
hominhduc28-Mar-06 6:18
hominhduc28-Mar-06 6:18 
GeneralUsing this concept with an Image Button Pin
JP0118-Mar-06 11:11
JP0118-Mar-06 11:11 
GeneralI did't get it Pin
wilsongb30-Apr-04 2:27
wilsongb30-Apr-04 2:27 
QuestionHow to register the dll outside Visual Studio Pin
Member 8487839-Apr-04 22:13
Member 8487839-Apr-04 22:13 
GeneralConfirm Button Pin
jez1234562-Apr-04 19:27
jez1234562-Apr-04 19:27 
Questionhow to use confirm button ? Pin
susree16-Oct-03 2:17
susree16-Oct-03 2:17 
GeneralWait-Confirm Button Pin
Member 63474110-Oct-03 8:01
Member 63474110-Oct-03 8:01 
Generalcracking ibm hard dive paswsword Pin
brain david9-Sep-03 1:23
brain david9-Sep-03 1:23 
QuestionCheckboxes on a list?? Pin
Anonymous8-Aug-03 15:36
Anonymous8-Aug-03 15:36 
GeneralDisable button too Pin
Anonymous27-Jun-03 3:20
Anonymous27-Jun-03 3:20 
General578 Pin
Anonymous22-Jun-03 21:09
Anonymous22-Jun-03 21:09 
QuestionHow to do a please wait Pin
Anonymous26-Feb-03 4:03
Anonymous26-Feb-03 4:03 
GeneralAdapted your code for C# Pin
zoltix5-Feb-03 6:24
zoltix5-Feb-03 6:24 
GeneralLooing for a Page Loading ...... solution Pin
Lman29-Jan-03 6:10
Lman29-Jan-03 6:10 
GeneralRe: Looing for a Page Loading ...... solution Pin
Anonymous27-May-03 0:18
Anonymous27-May-03 0:18 
Generalmultiple buttons on page Pin
mgriffith31-Jul-02 8:53
mgriffith31-Jul-02 8:53 
GeneralNeeds to handle special chars in _Message Pin
Richard Deeming30-Jul-02 0:18
mveRichard Deeming30-Jul-02 0:18 
GeneralBroken when combined with validations controls Pin
Pete Bassett29-Jul-02 23:16
Pete Bassett29-Jul-02 23:16 
GeneralRe: Broken when combined with validations controls Pin
Andy Smith30-Jul-02 6:14
Andy Smith30-Jul-02 6:14 
GeneralRe: Broken when combined with validations controls Pin
mgriffith30-Jul-02 12:00
mgriffith30-Jul-02 12:00 
very cool...i especially like the combobox. i actual created a simple control/app that mimicked the client side functionality of that one data drop down control, using microsoft's msxmlhttp object and some fun javascript (similar to yours). i'll have to dig that up and post it, and see if anyone could get some use out of it.

michael griffith
--------------------
mgriffith@lauren.com
mdg12@po.cwru.edu
GeneralRe: Broken when combined with validations controls Pin
Pete Bassett30-Jul-02 23:42
Pete Bassett30-Jul-02 23:42 
GeneralRe: Broken when combined with validations controls Pin
Andy Smith31-Jul-02 4:45
Andy Smith31-Jul-02 4:45 
GeneralRe: Broken when combined with validations controls Pin
Anonymous12-Feb-03 4:24
Anonymous12-Feb-03 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.