May 19, 2008

Delete Confirmation The Easy Way in DNN

In continuing my writing series on hidden nuggets in the DotNetNuke core I will show you a handy method from the ClientAPI that allows you to add a Javascript delete confirmation in a much easier manner than normal.  As with the previous articles of this nature please feel free to send in any suggestions on helpful, somewhat hidden features inside the DotNetNuke core.  Now enough with the introduction and on to the code.

The Old Way

First of all I wanted to start with the old way of adding a Javascript confirmation to a button called 'testButton'.  The confirmation text comes from the local resource file.  To accomplish this we would use something similar to the below.  Please note, that the string concatenation is not a best practice it was just done to allow for proper display on this site.

//Old way
string jS "javascript: return confirm('";
jS +Localization.GetString("Test"this.LocalResourceFile);
jS +"');";
testButton.Attributes.Add("OnClick", jS);

Now this isn't all that complex, but it takes up a good amount of space.

The New Way

By adding a simple reference to the "DotNetNuke.WebUtility.dll" assembly and the following using statement to the top of our .cs file we are able to use methods provided inside the ClientAPI for confirmation and other actions.

using DotNetNuke.UI.Utilities;

Now with the reference added and the using statement at the top we are able to add the same delete confirmation to our button using the following code.

//New way
ClientAPI.AddButtonConfirm(testButton, Localization.GetString("Test"this.LocalResourceFile));

In my book this is a much easier to read format and also requires a lot less work on your part to remember how to add the delete confirmation.  I ran across this today when looking through forum posts on the forums and thought I would share a bit here.  For those also working with Javascript on a regular basis in your modules you might also find the "ClientAPI.GetSafeJSString" method very helpful, it is a method that will take and make a string ok to be put inside a Javascript string.

tags: DNN, Tutorials
comments powered by Disqus

Content provided in this blog is provided "AS-IS" and the information should be used at your own discretion.  The thoughts and opinions expressed are the personal thoughts of Mitchel Sellers and do not reflect the opinions of his employer.

Content Copyright

Content in this blog is copyright protected.  Re-publishing on other websites is allowed as long as proper credit and backlink to the article is provided.  Any other re-publishing or distribution of this content is prohibited without written permission from Mitchel Sellers.