Dot Net Sparks C#.NET,ASP.NET,ADO.NET,LINQ,AJAX,JavaScript,SharePoint

/ 2

Open application page in SharePoint 2010 dialog framework using Code-Behind

In the previous post, I have explained you how to open application page in SharePoint 2010 dialog framework, where we used the script with in web part i.e., .ascx.

In this post I will explain you how to open the same application page in dialog framework using code-behind model with some modifications to the previous solution.

Here, we gonna do only two things, first we will add an anchor tag with attributes id= “anchor” and runat=”server” in the SP2010DFWebPartUserControl.ascx, in order to have the access in the code-behind. Next, in the code-behind file, we write some JavaScript and assign it to script variable and register the script to emit on page load.

In SP2010DFWebPartUserControl.ascx, just add the following code
<a id="anchor" runat="server">Click here</a>
And, in the SP2010DFWebPartUserControl.ascx.cs, add the following code in the page load event
  var script = @"function openMyDialog() {
            var options = {
            url: '/_layouts/SP2010DF/MyApplicationPage.aspx',
            width: 400,
            height: 250,
            title: 'My Dialog',
            allowMaximize: true,
            showClose: true
            };
             SP.UI.ModalDialog.showModalDialog(options);
            }  
//SP.SOD.executeOrDelayUntilScriptLoaded(display, 'sp.ui.dialog.js'); ";
//Updated the above statement by replacing "display" with "openMyDialog"
SP.SOD.executeOrDelayUntilScriptLoaded(openMyDialog, 'sp.ui.dialog.js'); ";

   //The following statement registers the script on page load
  Page.ClientScript.RegisterStartupScript(typeof(LayoutsPageBase), Guid.NewGuid().ToString(), script, true);
  anchor.HRef = "javascript:openMyDialog()";
That’s it. Now, you can deploy the solution and test it.

Comments (2)
  1. In the part where you put "SP.SOD.executeOrDelayUntilScriptLoaded(display, 'sp.ui.dialog.js'); did you you mean for "display" or "openMyDialog"? just curious. Thanks!

  2. Cory Loriot: Good catch! Actually, it must be "openMyDialog" instead of "display". I've updated the post. Thanks for your comment.

Leave a comment