SharePoint 2010 Modal Dialog | Tweet |
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:
- 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.
- 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> |
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的更多相关文章
-
SharePoint 2010 Pop-Up Dialogs
转:http://kyleschaeffer.com/sharepoint/sharepoint-2010-pop-up-dialogs/ SharePoint 2010 makes it incre ...
-
Using SharePoint 2010 dialogs
转:http://www.techbubbles.com/sharepoint/using-sharepoint-2010-dialogs/ SharePoint 2010 dialogs are J ...
-
Upgrade from SharePoint 2010 to SharePoint 2016
[转]http://nikcharlebois.com/upgrade-from-sharepoint-2010-to-sharepoint-2016/ In this blog, I will go ...
-
SharePoint 2010 常用技巧及方法总结
1.代码调试确定进程cd c:\windows\system32\inetsrvappcmd list wppause注:保存成批处理文件,查看进程.bat,用的时候双击即可 2.类似列表新建打开方式 ...
-
使用SharePoint 2010 母版页
SharePoint 2010母版页所用的还是ASP.NET 2.0中的技术.通过该功能,实现了页面框架布局与实际内容的分离.虽然在本质上自定义母版页的过程和以前版本的SharePoint大致相同,但 ...
-
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 ...
-
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 ...
-
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 ...
-
使用SharePoint 2010的母版页
转:http://tanyanbo2.blog.163.com/blog/static/97339159201111591458902/ SharePoint 2010母版页所用的还是ASP.NET ...
随机推荐
-
Objective-c粒子动画
前面贴过几篇关于SpriteKit的案例文章,其中涉及到的动画都是材质类的图片切换或则常规的动画效果,没涉及到今天要说的粒子动画,今天说的粒子动画就是在游戏中实现更佳炫酷的效果必须使用的类,OC中粒子 ...
-
R连接Mysql时,中文显示为问号的解决方案
1.打开Mysql安装目录下的my.ini文件,将其中的default-character-set= 均设置为GBK 2.在管理工具——数据源(ODBC)中将创建好的数据源作如下设置:
-
SQL ALTER TABLE 语句在项目中的使用
1.在实际的项目开发过程中,之前已经创建好的实体类可能需要增加/删除字段,亦或是更改已有字段的属性,比如主键的增长策略从自增型改为UUID型,那么就会涉及到 SQL 中 alter table 语句的 ...
-
【LeetCode】20. Valid Parentheses
题目:
-
not enough actual parameters for macro &#39;min&#39;(QT与vs2010)
解决方案见以下: qdatetime.h:“min”宏的实参不足 | 浏览:73 | 更新:2015-01-06 12:36 百度经验:jingyan.baidu.com 最近用VS2012 中Qt5 ...
-
6.function自定义插件的方法和使用
Smarty插件本质上是一个function函数 有三种类型的插件: function modifier block functions 定义的方法有三种: 1.使用registerPlugin的方法 ...
-
TensorFlow之RNN:堆叠RNN、LSTM、GRU及双向LSTM
RNN(Recurrent Neural Networks,循环神经网络)是一种具有短期记忆能力的神经网络模型,可以处理任意长度的序列,在自然语言处理中的应用非常广泛,比如机器翻译.文本生成.问答系统 ...
-
Java并发-懒汉式单例设计模式加volatile的原因
懒汉式单例的double check.例一: class SingletonClass{ private static SingletonClass instance = null; private ...
-
Linux的sysctl 命令参数详解
Linux内核通过/proc虚拟文件系统向用户导出内核信息,用户也可以通过/proc文件系统或通过sysctl命令动态配置内核.比如,如果我们想启动NAT,除了加载模块.配置防火墙外,还需要启动内核转 ...
-
SolrCloud基本过程
转:http://www.data321.com/yunjisuan/20160514880/SolrZhiJieDuQuZKZhongDePeiZhiXin SolrCloud之分布式索引及与Zoo ...