Click here to monitor SSC

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
 

142 Music, 142 6 Cds said:

May 20, 2010 2:36 PM
 

300ce Replacement Power, Promo Mercedes Benz 300ce said:

May 20, 2010 5:57 PM
 

Buy E 250 E150, 2007 Ford Econoline E 300 said:

May 20, 2010 7:10 PM
 

K15 K1500 Area Promotion, Buy K15 K1500 Pickup Tailgate Hinge Fuse Box Engine Wiring Harness said:

May 21, 2010 2:08 AM
 

Buy Netbook Acer Aspire One D250, Acer Aspire As5740 5513 said:

May 21, 2010 3:24 AM
 

Wagovan Radiator Suv, Order Wagovan Oxygen Sensor said:

May 21, 2010 3:50 AM
 

Youtube Lw200 Saturn, Used Lw200 Replacement Headlight Assembly 2003 Saturn said:

May 21, 2010 6:06 PM
 

Premier Transmission Ebay, At&t Premier Order Status said:

May 21, 2010 9:36 PM
 

2000 Toyota Tundra Euro Lights, 94 Toyota Tundra Aftermarket Parts said:

May 22, 2010 5:07 AM
 

Aftermarket Bobcat Parts, Mercury Bobcat Cheap Dealers said:

May 22, 2010 6:50 AM
 

Specialists Bronco Ii Auto Parts, Xj Special said:

May 22, 2010 8:57 AM
 

1998 Volvo V70 Awd Reliability, S40 Discount V70 Parts Volvo Catalytic Converter - 326.binggreen.com said:

May 23, 2010 5:45 AM
 

Regency Coupon Codes Disney, Regency Used 1998 Oldsmobile Anti Lock Brakes 3800 Series Ii - 96.zapstreaming.com said:

May 23, 2010 8:22 AM
 

Mercedes Benz C43 Auto Parts, Buy Lexus C43 Ml55 Amg - 163.unlockiphone30.net said:

May 24, 2010 5:17 PM
 

Nike Max said:

Nike air Max 2010 Footwear is named as "Heartbeat shoes".It includes a Full-length polyurethane midsole using a Nike air Max unit from the heel and forefoot for exceptional cushioning.Whenever you put on it to operate,it might provide you with a feeling that the wind at your back again.it may assists you fly by means of your routines with large time cushioning to get a soft landing.It includes a incredibly fine permeability that is quite excellent for any runner.
August 23, 2010 2:23 PM
 

Nike Max said:

Exclusively created for strength conditioning, thenike air max 2010 functions Nike Flywire connecting from your midsole for the laces for any supportive fit by means of the midfoot. A larger-volume Max air unit adds optimum cushioning and effect safety from the heel, whilst an exaggerated DiamondFLX pattern with delta nodes extends all through the sole for outstanding, multidirectional versatility. These exact same delta nodes deflect throughout effect prior to springing back again to their original shape for even additional cushioning and response.
August 23, 2010 2:24 PM
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
Automated Script-generation with Powershell and SMO
 In the first of a series of articles on automating the process of building, modifying and copying SQL... Read more...

Converting String Data to XML and XML to String Data
 We all appreciate that, in general, XML documents or fragments are held in strings as text markup. In... Read more...

Geek of the Week: Don Syme
 With the arrival of F# 3.0 Microsoft announced a wide range of improvements such as type providers that... Read more...

How to Document and Configure SQL Server Instance Settings
 Occasionally, when you install identical databases on two different SQL Server instances, they will... Read more...

What's the Point of Using VARCHAR(n) Anymore?
 The arrival of the (MAX) data types in SQL Server 2005 were one of the most popular feature for the... Read more...