4

Is it possible to open the sharepoint 2010 javascript modal dialog from asp.net code behind? I have tried..

        System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">");
        System.Web.HttpContext.Current.Response.Write("var options =");
        System.Web.HttpContext.Current.Response.Write("{");
        System.Web.HttpContext.Current.Response.Write("    url: 'ok.aspx',");
        System.Web.HttpContext.Current.Response.Write("    width: 100,");
        System.Web.HttpContext.Current.Response.Write("    height: 100,");
        System.Web.HttpContext.Current.Response.Write("    allowMaximize: false,");
        System.Web.HttpContext.Current.Response.Write("    title: 'HATA'");
        System.Web.HttpContext.Current.Response.Write("};");
        System.Web.HttpContext.Current.Response.Write("modalDialog = SP.UI.ModalDialog.showModalDialog(options);");
        System.Web.HttpContext.Current.Response.Write("</script>");

.. and it fails.

user601653
  • 43
  • 1
  • 1
  • 3

3 Answers3

3

I'm guessing you want the dialog to open at page load, not as a result of a client-side button press (which is a little easier). You have to make sure the Sharepoint ECMA-libraries are loaded. It's easiest to use jQuery to register the opening of the dialog as a load event; if you use regular javascript in some browsers your load event will take place before all other load events, and it might fail because of it.

This would be your JS code:

$(document).ready(openDialog);
function openDialog() {
    var options = {
        url: 'ok.aspx',
        tite: 'HATA',
        allowMaximize: false,
        showClose: true,
        width: 100,
        height: 100,
        dialogReturnValueCallback: callback_openDialog
    };
    SP.UI.ModalDialog.showModalDialog(options);

    function callback_openDialog(dialogResult, returnValue) {
        //window.location.reload();
        console.log('openDialog result: ' + dialogResult);
        if (dialogResult != SP.UI.DialogResult.Cancel) {
            SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);
        }
    }
}

Use RegisterClientScriptBlock to include as a string or put the code in a .js file and include using RegisterClientScriptInclude.

To include JQuery add a mapped folder to layouts and add the JQuery file to it. Use the statement below to include it in your page.

Page.ClientScript.RegisterClientScriptInclude("JQuery", string.Format("{0}/_LAYOUTS/jquery-1.5.1.min.js", web.Site.Url));

I don't know if dialogReturnValueCallback is required, but since MSDN isn't exactly reliable in this sort of thing, I'm not sure if you can omit it.

Use the Firebug console, or the IE F12 console to debug your javascript, and if you have any problems with this let us know. (Please include error-messages if possible)

Janne Louw
  • 483
  • 2
  • 10
0

If you want to inject javascript onto your page, you can use RegisterClientScriptBlock

ClientScriptManager cs = Page.ClientScript;
cs.RegisterClientScriptBlock(this.GetType(), "your-key", "the text for your script", true);
Laurie
  • 5,197
  • 1
  • 19
  • 26
0

You can use OpenPopUpPageWithTitle method to open dialog in Sharepoint 2010.

Anders Rask
  • 17,949
  • 3
  • 38
  • 71