Web Tip: Add Message and Confirmation Boxes Using JavaScript

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

  • I am the new guy

    Posted by Eldon Spielvogel on 04/07/2017 10:53am

    Good day! This post couldn't be written any better! Reading this post reminds me of my previous room mate! He always kept chatting about this. I will forward this write-up to him. Fairly certain he will have a good read. Many thanks for sharing!

    Reply
  • I am the new guy

    Posted by Barry Kornweibel on 04/07/2017 03:16am

    Hello, i read your blog occasionally and i own a similar one and i was just wondering if you get a lot of spam remarks? If so how do you prevent it, any plugin or anything you can suggest? I get so much lately it's driving me insane so any support is very much appreciated.

    Reply
  • Im happy I finally signed up

    Posted by Kelle Gorman on 04/06/2017 07:03pm

    Hey! I just wanted to ask if you ever have any issues with hackers? My last blog (wordpress) was hacked and I ended up losing a few months of hard work due to no backup. Do you have any solutions to prevent hackers?

    Reply
  • I am the new girl

    Posted by Isabell Colleano on 03/07/2017 09:51pm

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog? My website is in the exact same niche as yours and my users would definitely benefit from some of the information you provide here. Please let me know if this ok with you. Regards!

    • Re:I am the new girl

      Posted by on 03/08/2017 07:22am

      We don not allow full copying of our posts, but you can reference and link to any of our content within the confines of fair use.

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