Click here to Skip to main content
Licence 
First Posted 29 Jul 2002
Views 248,357
Bookmarked 72 times

HTML Wait-Confirm Button Server Control

By | 29 Jul 2002 | Article
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:

<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.

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:

<%@ 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

About the Author

Michael Griffith

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey19:23 7 Feb '12  
QuestionHow to pass the _Message value? Pinmemberyaroopig16:29 31 May '06  
Questionhow to create a dynamic button and call event Onclick ? Pinmemberhominhduc6:18 28 Mar '06  
GeneralUsing this concept with an Image Button PinmemberJP01111:11 8 Mar '06  
GeneralI did't get it Pinmemberwilsongb2:27 30 Apr '04  
QuestionHow to register the dll outside Visual Studio PinmemberZach Baker22:13 9 Apr '04  
GeneralConfirm Button PinsussJ Stocks19:27 2 Apr '04  
Questionhow to use confirm button ? Pinmembersusree2:17 16 Oct '03  
GeneralWait-Confirm Button PinmemberEric Trinidad8:01 10 Oct '03  
Generalcracking ibm hard dive paswsword Pinmemberbrain david1:23 9 Sep '03  
QuestionCheckboxes on a list?? PinsussAnonymous15:36 8 Aug '03  
GeneralDisable button too PinsussAnonymous3:20 27 Jun '03  
General578 PinsussAnonymous21:09 22 Jun '03  
QuestionHow to do a please wait PinsussAnonymous4:03 26 Feb '03  
GeneralAdapted your code for C# Pinmemberzoltix6:24 5 Feb '03  
GeneralLooing for a Page Loading ...... solution PinmemberLman6:10 29 Jan '03  
GeneralRe: Looing for a Page Loading ...... solution PinsussAnonymous0:18 27 May '03  
Generalmultiple buttons on page Pinmembermgriffith8:53 31 Jul '02  
GeneralNeeds to handle special chars in _Message PinmemberRichard_D0:18 30 Jul '02  
GeneralBroken when combined with validations controls PinmemberPete Bassett23:16 29 Jul '02  
GeneralRe: Broken when combined with validations controls PinmemberAndy Smith6:14 30 Jul '02  
you may want to check out my control library at http://www.metabuilders.com/
I have a suite of ConfirmedButton controls. source included.
I use the same trick that the Validation stuff uses to make it compatible.
GeneralRe: Broken when combined with validations controls Pinmembermgriffith12:00 30 Jul '02  
GeneralRe: Broken when combined with validations controls PinmemberPete Bassett23:42 30 Jul '02  
GeneralRe: Broken when combined with validations controls PinmemberAndy Smith4:45 31 Jul '02  
GeneralRe: Broken when combined with validations controls PinsussAnonymous4:24 12 Feb '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 30 Jul 2002
Article Copyright 2002 by Michael Griffith
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid