SharePoint 2010 Modal Dialog Tweet
 
Modal dialog play very important role to improve the user experience by reducing the number of postbacks. So, SharePoint 2010 comes up with in-build API to show modal dialog to improve the user experience. Here, I will explain you How to integrate SharePoint 2010 modal dialog with the application and some real time problems and their solutions. Before I go on and provide you with the details, Let us see some of the features that this new Modal Dialog provides.

Functionality provided by Modal Dialog: -
       - Display HTML content  in Modal Dialog
       - Display external url (e.g. http://www.google.com) or any application page of SharePoint in the form of an iframe
       - Show/Hide Close button
       - Show/Hide Maximize button

Mentioned below are the steps to integrate a modal dialog in the SharePoint 2010:

  1. The JavaScript files for the ECMAScript Object Model (SP.js, SP.Core.js, SP.Ribbon.js, and SP.Runtime.js ) are installed in the %ProgramFiles%\Common Files\Microsoft Shared\web server extensions\14\TEMPLATE\LAYOUTS directory. We need to add these JavaScript files in the page.
  2. To open a dialog we need to use the 'SP.UI.ModalDialog.showModalDialog' method from the ECMAScript Client Object model and we can pass following parameters as per requirement:

- 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;"

class="ms-ButtonHeightWidth" />

</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>

b. Sample code to open a web page or application page in the modal dialog:

JavaScript code

<script type="text/javascript">

function openDialog() {

var options = {

url: "<Page Url>",

width: 600,

height: 300,

title: "My First Modal Dialog",

};

SP.UI.ModalDialog.showModalDialog(options);

}

</script>

Mentioned below are real time scenarios, you may encounter while using out of box modal dialog of SharePoint 2010 :-
1. Scenario: If page is too long and with the movement of vertical scrollbar, modal popup also move with the scrollbar. Ideally position of modal dialog should be fix.

Solution: Put mentioned below CSS class in you page:

.ms-dlgContent

{

position: fixed!important;

}

2. Scenario: Display modal popup on page load depending. Sometimes the page don't show modal dialog and a JavaScript error message comes (error: showModalDialog does not exist in SP.UI.ModalDialog).

Solution:  Use following code- ExecuteOrDelayUntilScriptLoaded(<showModelMethodName>, "sp.js");  
              This method be make sure that js method showModalDialog is not called till sp.js is fully loaded.

3. Scenario: Sometime we need to close popup from server side and also parent window refresh is required after saving modal dialog data in the SharePoint.

Solution: Mentioned below is the sample code to implement above requirement-

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:

<script type="text/javascript">

//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();

return false;

}

</script>

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);

OR

// 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);

4. Scenario: After closing modal dialog, all controls disappear from the page.

Solution: If you assign HTML element id like we have done in our first example, you might encounter this issue, we should set copy of modal data instead of assigning ID. See sample code below:

<script type="text/javascript">

//Pass copy of HTML content instead of content control ID

function openDialog() {

var cloneModalContent = document.createElement('div');

cloneModalContent.innerHTML = document.getElementById('divModalDialogContent').innerHTML;

var options = {

html: cloneModalContent, //html content to be displayed in modal dialog

width: 600,

height: 300,

title: "My First Modal Dialog",

dialogReturnValueCallback: customOnDialogClose, //custom callback function

allowMaximize: true,

showClose: true

};

SP.UI.ModalDialog.showModalDialog(options);

}

</script>

Updated following real time scenario solution on 17 November, 2011:
 
a. To resolve position issue of modal dialog, we need to add "ms.ModelContent" css class. But correct CSS class name is "ms-dlgContent". Thank you "Athula Nidheesh" to rectify my mistake.

b. Sample code to save or close popup from server side.

c. After closing modal dialog, all controls disappear from the page.

SharePoint 2010 Modal Dialog的更多相关文章

  1. SharePoint 2010 Pop-Up Dialogs

    转:http://kyleschaeffer.com/sharepoint/sharepoint-2010-pop-up-dialogs/ SharePoint 2010 makes it incre ...

  2. Using SharePoint 2010 dialogs

    转:http://www.techbubbles.com/sharepoint/using-sharepoint-2010-dialogs/ SharePoint 2010 dialogs are J ...

  3. Upgrade from SharePoint 2010 to SharePoint 2016

    [转]http://nikcharlebois.com/upgrade-from-sharepoint-2010-to-sharepoint-2016/ In this blog, I will go ...

  4. SharePoint 2010 常用技巧及方法总结

    1.代码调试确定进程cd c:\windows\system32\inetsrvappcmd list wppause注:保存成批处理文件,查看进程.bat,用的时候双击即可 2.类似列表新建打开方式 ...

  5. 使用SharePoint 2010 母版页

    SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...

  6. Searching External Data in SharePoint 2010 Using Business Connectivity Services

    from:http://blogs.msdn.com/b/ericwhite/archive/2010/04/28/searching-external-data-in-sharepoint-2010 ...

  7. How to: Add SharePoint 2010 Search Web Parts to Web Part Gallery for Upgraded Site Collections

    When you upgrade to Microsoft SharePoint Server 2010, some of the new SharePoint Enterprise Search W ...

  8. Creating SharePoint 2010 Event Receivers in Visual Studio 2010

    转:http://msdn.microsoft.com/en-us/library/gg252010(v=office.14).aspx Summary:  Learn how to create a ...

  9. 使用SharePoint 2010的母版页

    转:http://tanyanbo2.blog.163.com/blog/static/97339159201111591458902/ SharePoint 2010母版页所用的还是ASP.NET ...

随机推荐

  1. 寒假学干货之------ 初学者关于fragment_main(碎片的困扰)

    我们在activity_main中编写的框架,会被fragment_main中的取代掉,是因为新版的ADT为了配合平板Android3.0开发 起作用的代码在MainActivity.java中 pa ...

  2. datatables.js 里面遇到的问题

    1. 假如需要A行的data 和 B行的data 合并 在B行 data:name 在A行的 render:function(){ return data+full.name   此时返回的就是A+B ...

  3. overthewire朝花夕拾

    bandit: cat特殊字符文件名 - cat ./- 空格 cat "abc def"  or cat abc\ def 列出隐藏文件:ll du -ab 递归列出文件大小,以 ...

  4. redo log

    1.redo log相关数据字典 v$log:display the redo log file information from the control file v$logfile:identif ...

  5. HTML5预览图片、异步上传文件

    注意啦:本文的代码都是以JQuery为示例,jq_开头的变量都是jq对象. 在HTML5中,我们可以在图片上传之前对图片进行预览,就像下面这么做 jq_upload_file.change(funct ...

  6. JQuery中$.ajax()方法参数详解及应用

    url: 要求为String类型的参数,(默认为当前页地址)发送请求的地址. type: 要求为String类型的参数,请求方式(post或get)默认为get.注意其他http请求方法,例如put和 ...

  7. Ubuntu下安装composer及配置

    1.下载最新composer wget -c https://getcomposer.org/composer.phar 2.可执行权限 chmod u+x composer.phar 3.放置到安装 ...

  8. link 与 @import之对比

    页面中使用CSS的方式主要有3种:行内添加定义style属性值,页面头部内嵌调用和外面链接调用,其中外面引用有两种:link和@import.外部引用CSS两种方式link和@import的方式分别是 ...

  9. ImageView.ScaleType设置图解

    图文相配很清晰的看出每个属性的效果, 感觉 CENTER_CROP 比较有用,长宽自动适应 ImageView ,整个图片自动缩略填充整个区域且居中显示(高宽不一定是view的尺寸),以前用JS在网页 ...

  10. L3-007. 天梯地图

    L3-007. 天梯地图 题目链接:https://www.patest.cn/contests/gplt/L3-007 Dijstra 这题是Dijstra的变形,麻烦的是两种最短路的相同距离时的选 ...