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.



Comments

  • 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!

    Reply
  • 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!

    Reply
  • 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!

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

    Reply
  • 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!

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

    Reply
  • xkvgB xqR eLdt

    Posted by CpQAFfWVZB on 06/21/2013 08:11pm

    buy vicodin buy vicodin 7.5 - vicodin withdrawal treatment at home

    Reply
  • HoxispClisp

    Posted by MarlDuartomma on 05/20/2013 09:36am

    nbkg clomid no rx buy flagyl cymbalta no rx atarax cipralex pharmacy yvge uszk fvoo dtnv

    Reply
  • Assughnub

    Posted by Nalkloonfek on 05/19/2013 02:36pm

    owgk cheap periactin zovirax usa cash loans lubbock tx celebrex for sale strattera no rx cash advance online savings account lamisil tablets instant payday loans reviews mjos ugoe oesn pqlh

    Reply
  • nundAvansax

    Posted by invoistut on 05/13/2013 07:51pm

    mdyr [url=http://pjatiklassnik.ru/oblegchenie/#4d5a]Скрылось в землю.[/url] [url=http://planeta-forum.ru/isterika/#9k8c]Скачать на телефон эй ты кто такой[/url] [url=http://podzatylnik.ru/question-568719.html]Какой рост на данный момент у yamada ryosuke[/url] gayj ovdn Конечно, не только он. Ну что там у вас? hcgg ikbx ужас

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

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