Charles Lee

Using the JQuery dialog widget in a custom SharePoint web part

Published Wednesday, November 11, 2009 10:00 AM

If you have played around with the dialog widget in JQuery UI you may have noticed a couple of things.

  1. Like a lot of JavaScript libraries it messes with the DOM (Document Object Model) at what might be unexpected times and therefore it does not play well with SharePoint.

  2. It breaks the web part verb menu (the 'edit' drop down menu)

Actually it (like a lot of the JQuery UI widgets) is an incredibly powerful tool to your client side arsenal, and as long as you adhere to some best practice it will work perfectly happily within SharePoint, or any other ASP.Net site.

1. It messes with the DOM.

Specifically it does not play well with the ASP.Net page post back model. This is fairly well documented across the Interweb (although the actual solution is not).

The dialog widget is initiated via code similar to this:

$('#dialog').dialog();

When JQuery sees this it initiates the widget and actually moves your entire div tag to the root of the DOM (see screenshot below). Therefore outside of the form tag. So any ASP.Net controls which you may have in this div will not be included in any post backs therefore none of their events will fire.

JQueryDialogDOM

Never fear however as this is easily fixed by moving the parent element of your div, which will be another div which is created by JQuery and putting this at the root of the form instead (see code sample below).

2. It breaks the web part 'edit' menu.

Actually it doesn't. But if you have followed some code examples that are out there then it might. Essentially it may mess with this if your call to initiate the dialog is happening before the page has entirely loaded.

Fortunately JQuery has a handy way of ensuring that no matter where your code is defined on the page it will only run when the page has loaded up. This is simply accessed using:

$(document).ready();

Any function supplied to this method will be loaded in memory and run when the page has finished loading. How great is that!

As long as you follow these two items of best practice you should have no problems using the JQuery dialog widget from within SharePoint.

Code

The following code example is how I would use the dialog widget. At some point you need to run the initiateModalDialog method passing in the id of the div to use as a dialog. This will then run when the page is loaded and will initiate your dialog.

When you want to show it then call openModalDialog and optionally (because JQuery will render a nice close button in the top tight hand corner) you could call closeModalDialog to close it.

Note: If you are using this within a web part then make sure your selector (the id of your div) is unique to your web part in case you have multiple web parts on the same page.

function initiateModalDialog(selector) {

//initiates the dialog when the page has finished loading
$(document).ready(function() {    

    $('#' + selector).dialog({   
        autoOpen: false,   
        bgiframe: true,    
        draggable: false,   
        width: 600,   
        resizable: false,   
        modal: true,   
        show: 'slide' 

    });  

}); 

}

//Opens the selected div as a modal dialog
function openModalDialog(selector) {

    //Opens the dialog
    $('#' + selector).dialog('open');           

    //Appends the dialog to the ASP.Net form to allow postbacks
    $('#' + selector).parent().appendTo($("form:first"));   
}    

//Closes the selected div dialog
function closeModalDialog(selector) {           
    $('#' + selector).dialog('close');  
}

Obviously the options which are passed to the initiation method such as 'draggable' and 'resizable' are up to you, but for this to work you must set autoOpen to false otherwise your dialog will open straight away (unless that is what you intended of course).

For further information please read the JQuery UI dialog widget documentation.

by CharlesLee
Filed Under:

Comments

 

Using the JQuery dialog widget in a SharePoint or ASP.Net « F5 to Debug said:

November 17, 2009 7:36 AM
 

uberVU - social comments said:

This post was mentioned on Twitter by CharlesDLee: Using the #JQuery dialog widget in #SharePoint/#ASP.Net #Link http://bit.ly/1nXAPX
November 17, 2009 8:01 AM
 

Arjan’s World » LINKBLOG for Nov 18, 2009 said:

November 18, 2009 10:05 AM
 

Sanjeev Agarwal said:

Daily tech links for .net and related technologies - November 18-20, 2009 Web Development SharePoint
November 19, 2009 1:26 AM
You need to sign in to comment on this blog

About CharlesLee

Charles Lee is a software solutions developer for a large law firm based in Birmingham in the UK. He has been developing with ASP.NET since 2003, and currently focuses on WSS 3.0 and MOSS 2007 solutions to complex business problems.


















<November 2009>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345
Finding Stuff in SQL Server Database DDL
 You'd have thought that nothing would be easier than using SQL Server Management Studio (SSMS) for... Read more...

Mission Critical: SQL Server 2008 Performance Tuning Task List
 In which Buck Woody imagines how the US military would have tackled DBA checklists for... Read more...

Simple Query tuning with STATISTICS IO and Execution plans
 A great deal can be gleaned from the use of the STATISTICS IO and the execution plan, when you are... Read more...

Switching rows and columns in SQL
 When they use SQL Server, one the commoner questions that Ms Access programmers ask is 'Where's the... Read more...

Writing Efficient SQL: Set-Based Speed Phreakery
 Phil Factor's SQL Speed Phreak challenge is an event where coders battle to produce the fastest code to... Read more...