Web Tip: Add Message and Confirmation Boxes Using JavaScript

Any time I have a Delete or Cancel button, I add a JavaScript confirmation dialog to keep the user from doing something he might regret. In other cases, a simple alert with an OK dialog is sufficient. To save myself some time, I've created the following two functions to do this work for me:

public void AddConfirmMessage(WebControl ctl, string message)
   ctl.Attributes.Add("onclick", "if ( ! confirm( '"
      + message + "' )) return false; ");

public void AddPopupMessage(WebControl ctl, string message)
   ctl.Attributes.Add("onclick", "alert( '" + message + "'); ");

They accept a generic WebControl object and edit the Attributes collection of that control, which adds the JavaScript code to the control from the code-behind. These functions will work with any control that derives from the WebControl class. Simply pass in the control and the message you want to display, and as long as the user has JavaScript enabled, the messages will appear.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic.


  • Im glad I now registered

    Posted by Cleveland Mccrory on 12/15/2016 12:46am

    Awesome article.

  • Im happy I now signed up

    Posted by Anderson Dougharty on 12/14/2016 11:55pm

    Very nice post. I just stumbled upon your weblog and wished to say that I've really loved browsing your weblog posts. In any case I'll be subscribing in your feed and I am hoping you write once more very soon!

  • Just wanted to say Hello!

    Posted by Felix Jennings on 12/14/2016 10:07pm

    May I simply just say what a relief to find somebody who truly understands what they're discussing over the internet. You definitely realize how to bring an issue to light and make it important. A lot more people have to check this out and understand this side of the story. I was surprised you're not more popular since you surely possess the gift.

  • Im glad I now registered

    Posted by Rubin Wentcher on 12/14/2016 09:03pm

    We are a bunch of volunteers and opening a new scheme in our community. Your website offered us with helpful information to work on. You've performed an impressive process and our whole neighborhood will likely be thankful to you.

  • Just wanted to say Hello!

    Posted by Norma Gomes on 08/25/2016 12:58am

    I quite like looking through a post that can make people think. Also, many thanks for permitting me to comment!

  • Just want to say Hi.

    Posted by Delmar Ballow on 08/24/2016 11:23am

    Very nice post. I simply stumbled upon your weblog and wished to say that I've truly loved surfing around your weblog posts. After all I will be subscribing in your rss feed and I am hoping you write once more soon!

  • Im glad I now signed up

    Posted by Raymon Jansen on 08/17/2016 06:49am

    Hey there! I've been reading your weblog for a long time now and finally got the bravery to go ahead and give you a shout out from Dallas Texas! Just wanted to tell you keep up the great job!

  • I am the new girl

    Posted by Anke Utz on 08/10/2016 04:42am

    Hello just wanted to give you a brief heads up and let you know a few of the images aren't loading properly. I'm not sure why but I think its a linking issue. I've tried it in two different browsers and both show the same outcome.

  • Just want to say Hello!

    Posted by Mildred Sweat on 07/27/2016 06:10am

    I was recommended this web site by my cousin. I'm not sure whether this post is written by him as nobody else know such detailed about my problem. You're wonderful! Thanks!

  • Lisseur GHD pour les filles de la mode

    Posted by lxdpdn375 on 07/16/2013 12:25pm

    Lorsque le mot settes bon marché dans le même théorème comme Journée Good Hair (GHD) GHD est le mot faux normalement pas si loin unna. Tout Dessverre qui a marqué GHD et semble trop beau pour être y intéressant de prix, normalement. Vous pouvez être désespérée pour GHD droit tige merke, mais vous devriez envisager d'autres rubans de nom aussi, car il est beaucoup plus sur le marché qui va donner deg le même vétérinaire results.I qu'il est souvent coutumier aux consommateurs un loquet en utsalgspris que le temps passe. La couche gjentatt de tendance avec les lecteurs DVD et autres appareils électroniques. C'est beaucoup moins cher de leur offrir une solution fiable sur le marché, c'est juste un notre web av magasiner. [url=http://bbeatsinorge.cabanova.com/]ghd pas cher[/url] GHD Styler Hair utilise des plaques en céramique chauffées à la fois les cheveux raides et boucle. Un bon entretien permettra d'assurer votre styler GHD dure aussi longtemps que possible. Nettoyage des résidus de produits coiffants de style devrait faire partie de l'entretien régulier. Il gardera le styler est à son meilleur. Vous pouvez facilement nettoyer votre styler GHD sans causer de défrisants damage.Ceramic sont préférés pour le salon et le style de la maison. [url=http://ghdpascherferfr.webgarden.com/]lisseur ghd styler[/url] GHD rettang 7 brosse avec des ions actifs est une innovation révolutionnaire dans les soins capillaires, et est conçu pour permettre aux femmes de toujours être en mesure de regarder leur meilleur out.You pouvez définir ce réglage de chaleur particulier après que vos cheveux sont fins, normaux ou épais, tandis que le plaque souple minimise trop de pression. Le résultat est beau, coiffures élégantes avec décoloration minime.

  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date