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

2 Answers 2


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);

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);

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

  • +1 nice and simple (btw question is a bit vague) Jul 27, 2011 at 22:18
  • Definitely a bit vague, but I got the jist of what he was asking. :)
    – Rob D'Oria
    Jul 27, 2011 at 22:22
  • Thank you, this does answer my question. I just did not want to have tiny aspx pages for modal dialogs that were (apparently) already being used by SharePoint. The references to 'dialog.master' and the htmleditor.aspx' are just what I needed to point me in the right direction.
    – Tom Hill
    Jul 28, 2011 at 12:07
  • @TomHill: What was your solution in the end? I'm quite curious about this myself.
    – Stu Pegg
    Oct 6, 2011 at 7:27

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.


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

protected override void OnInit(EventArgs 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.

  • This answer is not correct. The master page itself doesn't have anything to do with whether it is rendered as a dialog or not. The SP2010 dialog framework uses SP.UI client namespace and objects. Please refer to my answer for details.
    – Rob D'Oria
    Jul 27, 2011 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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