Question

I need a simple 'Yes/No' modal dialog for a SharePoint web part, and I do not really want to re-invent the wheel here. SharePoint uses some very nice modal dialogs for this purpose, is it possible to re-use them, instead of creating a litter of little aspx pages for the ShowModalDialog() function

Was it helpful?

Solution

You actually don't have to use dialog.master, as that by itself doesn't help you use the 2010 dialog framework. the default.master works just fine because it has CSS that hides the ribbon bar, navigation, and other UI components that you don't want in a dialog (...unless you explicity expose them).

You need to run some javascript code to launch the dialog (...would go in the parent page):

function showModalDialog(title) {
    var options = SP.UI.$create_DialogOptions();
    options.title = title;
    // width and height are optional as the framework autosizes the dialog to the content
    options.width = 600;
    options.height = 485;
    options.url = "/_layouts/htmleditor.aspx";
    options.allowMaximize = true;
    // you can specify an optional callback
    options.dialogReturnValueCallback = Function.createDelegate(null, dialogCallback);
    SP.UI.ModalDialog.showModalDialog(options); 
}

Assuming you have the standard OK and Cancel buttons in your dialog page (...can be a base SharePoint application page created in a VS2010 project), you'd just wire up the events for those buttons and within the code-behind you can emit some javascript to close the dialog and optionally retuan a value to the parent page:

    void CancelDialog_Click(object sender, EventArgs e)
    {
        if (this.IsDialogMode)
            // in commonModalDialogClose the zero denotes Cancel and a one denotes OK.  The 2nd argument is an optional value to return to the parent page's callback function...assuming one was defined.
            this.ClientScript.RegisterClientScriptBlock(this.GetType(), "DismissRecoverPasswordDialog", "window.frameElement.commonModalDialogClose(0, \"\");", true);
        else
            Response.Redirect(ReferralUrl.Value);
    }

Hope that helps...please don't hesitate to ask ant follow-up questions you have.

OTHER TIPS

Yes, you can reuse them. It's all about using the .master file that SharePoint ships. Master page like dialog.master are designed for pages which are displayed in popup and you should reuse them rather than writing your own. It comes with OK and Cancel buttons and placeholders in which you can place your content. In your page you will have to refer them with MasterPageFile attribute because they are not in master page gallery.

MasterPageFile="~/_layouts/dialog.master"

And in your code behind, you can wire the code on click of "Ok" button like this:

protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            ((DialogMaster)this.Page.Master).OkButton.Click += new EventHandler(this.OkButtonClicked);            
        }

You can change the Text property of OkButton to "Yes" and Cancel button to "No" if that makes sense in your case.

Reusing Popup Dialogs As far as reusing is concerned, you can use query string parameters to manipulate the content on the page. For example, with yes/no button you typically display a message. you can store the message in resource files and pass resource key in query string to the aspx dialog page to display different message.

Also if you think you can reuse existing sharepoint dialog box, you can examine the query string parameters it expects and send your own parameters and use sharepoint's popup pages.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top