- width: Set the width of the modal dialog
- height: Set the height of the modal dialog
- html: the ID HTML control or HTML content to be displayed in modal dialog
- url: Page url or relative path
- dialogReturnValueCallback: In case we want some code to run after the dialog is closed, set JavaScript method name
- allowMaximize: Set to true or false to show hide this option.
- showClose: Set to true or false to show or hide the close button
Examples: a. Sample code to show HTML content in the SharePoint 2010 modal dialog:
HTML code
// Modal Dialog HTML content
<div id="divModalDialogContent">
Hello World!
<input type="button" value="OK"onclick="SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 'Ok clicked'); return false;"
class="ms-ButtonHeightWidth" />
<input type="button" value="Cancel"onclick="SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, 'Cancel clicked'); return false;"
</div>
JavaScript Code
<script type="text/javascript">
// Call openDialog method on button click or on page load
function openDialog() {
var options = {
html: divModalDialogContent, // ID of the HTML tag
// or HTML content to be displayed in modal dialog
width: 600,
height: 300,
title: "My First Modal Dialog",
dialogReturnValueCallback: dialogCallbackMethod, // custom callback function
allowMaximize: true,
showClose: true
};
SP.UI.ModalDialog.showModalDialog(options);
}
//Results displayed if 'OK' or 'Cancel' button is clicked if the html content has 'OK' and 'Cancel' buttons
function onDialogClose(dialogResult, returnValue) {
if (dialogResult == SP.UI.DialogResult.OK) {
alert('Ok!');
if (dialogResult == SP.UI.DialogResult.cancel) {
alert('Cancel');
// Custom callback function after the dialog is closed
function dialogCallbackMethod() {
alert('Callback method of modal dialog!');
</script>
JavaScript code
url: "<Page Url>",
.ms-dlgContent
{
position: fixed!important;
a. Check current page is popup page or not
//Custom list or library form New, Edit or Display if(SPContext.Current.IsPopUI)
// Code
OR
//Layout pages you can ensure byquery string
if(Request.QueryString["IsDlg"]=="1")
//code
b. To close popup use mentioned below JavaScript code:
//Close popup on cancel button click
function CloseForm() {
window.frameElement.cancelPopUp();
return false;
//Commit/Refresh parent window on save button click
function SaveForm() {
window.frameElement.commitPopup();
c. use following server side code to close or save modal dialog:
//Put following code in the button click event, if update panel is not present in the page
ClientScript.RegisterClientScriptBlock(this.GetType(), Guid.NewGuid().ToString(),"CloseForm()", true);
// Put following code in the button click event, if update panel is present in the page
ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), Guid.NewGuid().ToString(),"CloseForm()", true);
//Pass copy of HTML content instead of content control ID
var cloneModalContent = document.createElement('div');
cloneModalContent.innerHTML = document.getElementById('divModalDialogContent').innerHTML;
html: cloneModalContent, //html content to be displayed in modal dialog
dialogReturnValueCallback: customOnDialogClose, //custom callback function
Richard Mueller edited Revision 1. Comment: Changed tag "Microsoft SharePoint 2010" to "SharePoint 2010", changed tag "SharePoint 2010 modalDialog" to "modalDialog"
Revision: edited tags