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
SharePoint Developer
Today  :   3     Total  :    931

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

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
     //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!');
 function closePopup() {
 //closing the popup

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

Happy Coding


blog comments powered by Disqus

SharePoint 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


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 in a word document.

Related Resources

Recent Tweets

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Twitter October 10, 09:26
How to Use Angular HTTP Services in SharePoint List -

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