How to Dynamically Include ASP.NET Controls into Modal Popup in your SharePoint Server Side Code

Ahamed Fazil Buhari
SharePoint Developer
Published On :   28 Aug 2016
Visit Count
Today :  3    Total :   2473
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!

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


SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.

Migratiin Tools for SharePoint