Display modal dialog using Object model SharePoint 2010

In this post we will see a short example of how to invoke a modal dialog from a server side object model.
Although you can add your javascript to open the dialog box in your application\custom page's aspx file, but in case if you want to add the script for the dialog and invoke the dialog from within the code file, we would do something like below.

//load the sp.ui.dialog.js' first

ExecuteOrDelayUntilScriptLoaded(showMyModalDialog, 'sp.ui.dialog.js');

//add javascript for dialog
String myJavascript = "var showMyModalDialog = function() { " +
var options = { "+
url: targetUrl,title: 'Custom dialog', width: 980, height: 580, dialogReturnValueCallback: function (dialogResult, returnValue) " +
{ if (dialogResult === 1) { // the user accepted the dialog } " +
+" else { // the user cancelled the dialog } " +
+ " } }; SP.UI.ModalDialog.showModalDialog(options); }; " +

// Register the script
this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "modalDialogBox", myJavascript);