SharePoint Pals
 | Sign In
How to Dynamically Include ASP.NET Controls into Modal Popup in your SharePoint Server Side Code
by Ahamed Fazil Buhari 28 Aug 2016
Author
SharePoint Developer
Visits   
Today  :   1     Total  :    1947

In this article, let us see how to insert ASP.NET controls into your SharePoint Modal popup dynamically. We can easily insert HTML element into our page or Modal pop up easily but when it comes to ASP.NET controls then we need to hit the server for that.

So creating and appending the ASP control elements dynamically in client side using append() html function is not a good option. Here, I’ve created a Farm solution for Visual Web Part in SharePoint 2010. This custom visual web part only shows the button, when the user clicks on that button it will show Modal popup with ASP.NET elements in it.

Another problem with this popup is that, if we close the popup window it will remove the elements inside the parent html. Please note, this issue arises only if we use html instead of URL in our Modal Popup options. In the below approach we gave you workaround for this.

 var options = {
         html: htmlVal,
         //url: "/../_layouts/Visual/MyPage.aspx"
 	}
 

Code inside the UserControl file (.ascx ), let’s consider that we have asp <asp:LinkButton> element and we need to show this only in the Modal Popup. Please include all relevant jQuery library and other js files if needed,

 <SharePoint:ScriptLink ID="jsMainPDF" Localizable="false" runat="server" Name="~Site/_layouts/MyVisual_ClientScript/Main.js">
 </SharePoint:ScriptLink>
 
 <div id="outerDiv">
 
 <button id="btnCreateReport" alt="Popup window" name="Popup Window" onclick="return openDialog();"></button>
 
 <div id="mainDiv" style="display: none;">
 <%-- _spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true; using this to avoid page refresh --%>
 <asp:LinkButton ID="lnk_btn" UseSubmitBehavior="false" runat="server" OnClientClick="_spFormOnSubmitCalled = false;_spSuppressFormOnSubmitWrapper=true;"
                     OnClick="btnCreate_Click">Download</asp:LinkButton>
 
 </div>
 </div>
 

Script in Main.js file,

 function openDialog() {
     SP.SOD.executeFunc('sp.js', 'SP.ClientContext', ShowDialog);
 }
 function ShowDialog() {    
     var htmlVal = '';
     
     //Duplicating the div which needs to be embedded into popup
     //clone(true), true indicates it copy with Event Handler as well 
     $("#mainDiv").css("display", "");
     htmlVal = document.getElementById('mainDiv');
 
     //this duplicate div will be available after we close the pop up
     $("#mainDiv").clone(true).css("display", "none").appendTo("#outerDiv");
 
 
     var options = {
         html: htmlVal,
         //url: "../../../_layouts/PMO_JS/PDF_ReportGeneration.aspx",
         title: "My Popup",
         width: 350,
         height: 150,
         showClose: true,
         allowMaximize: true,
         dialogReturnValueCallback: closePopup
     };
     SP.UI.ModalDialog.showModalDialog(options);
     //for not refreshing the page
     _spFormOnSubmitCalled = false;
     _spSuppressFormOnSubmitWrapper = true;
 
     return false;
 
 }
 //call this function in dialogReturnValueCallback when you want to refresh the page function refreshCallback(dialogResult, returnValue) {
     SP.UI.Notify.addNotification('Operation Successful!');
     SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
 }
 
 function closePopup() {
 //closing the popup
     SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel);
 }
 

Script has all the required comments. I don’t need to explain much about the script, hope this helps.

Happy Coding

Ahamed

blog comments powered by Disqus

SharePoint Pals

Pals
SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile

Training

Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community




SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to articles@sharepointpals.com in a word document.

Related Resources

Recent Tweets

Twitter January 15, 00:25
How To Enable Target Value And Actual Value In #D3 Gauge Chart https://t.co/VxSi4QnNrC

Twitter January 15, 00:24
How To Open #SharePoint List Hyperlink Column In Modal #Popup Window https://t.co/EQ7HkoZDkX

Twitter January 15, 00:24
Quick Introduction To #Asp.NetCore And It’s Features https://t.co/zAXObHCFpH

Twitter January 15, 00:22
How To Configure #PerformancePoint Services To Use Secure Store In #SharePoint 2013 https://t.co/LEwnUoI7EY

Twitter January 15, 00:21
How To Block Or Disable #Office365 Services https://t.co/Yvp2VPFIRP

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation