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

  • Im glad I now registered

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

    Awesome article.

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

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

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

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

    Posted by sertac_caliskan on 01/15/2007 05:06pm

    Thanks to this solution it is very useful

    Reply
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