1

Im trying to open a dialog with the following code :

printModal()
{
    var options = {
                    url: "www.hotmail.com",
                    width: 550,
                    height: 200 
                  }; 
    SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);
}

this code is found in the .ascx of a visual webpart that i need to create. But when i click on the button that should call the following function from the backend with the following code :

Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", " printModal();", true);

did the same procedure in an application page and it worked without an issue but when doing it inside a webpart its never working. Can anybody enlighten me where the problem lies?

  • If you open up your browser's dev tools, then view the console, are you getting any JavaScript errors? – Omar Stewey Sep 4 '14 at 14:27
  • yes "printModal is not defined" – user2987348 Sep 4 '14 at 14:28
1

That means the RegisterStartupScript block is executing before your printModal function is defined. It was probably working in the application page because the function was defined before the script was getting called.

You can fix this by just declaring your printModal function in the code-behind, using something like RegisterClientScriptBlock before calling it.

Here are some examples:

http://www.ashokraja.me/articles/How-to-Programmatically-Show-or-Hide-a-Modal-Popup-Dialog-with-Server-Side-Code-in-Share-Point-2013

http://msdn.microsoft.com/en-us/library/bahh2fef(v=vs.110).aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.