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

Ahamed Fazil Buhari
 
Senior Developer
August 28, 2016
 
Rate this article
 
Views
5476

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

Author Info

Ahamed Fazil Buhari
 
Senior Developer
 
Rate this article
 
Ahamed is a Senior Developer and he has very good experience in the field of Microsoft Technologies, especially SharePoint, Azure, M365, SPFx, .NET and client side scripting - JavaScript, TypeScript, ...read more
 

Leave a comment