Click here to Skip to main content
15,884,977 members
Articles / Web Development / ASP.NET

ShowModalDialog effect in ASP.NET web forms

Rate me:
Please Sign up or sign in to vote.
3.37/5 (13 votes)
5 Feb 2007CPOL2 min read 213.5K   5.1K   58   21
How to open modal a dialog window in a browser (Internet Explorer) and do something with the parent window after return.

Introduction

This article presents how to open a modal dialog window on a browser (IE only) which contains a web form. Submitting the form on the modal dialog window will refresh the main window content.

Background

I was working on a project to convert a Windows application into a Web-based application. The Windows application's main form displays a DataGrid. Clicking on a row of the DataGrid opens a modal dialog window which holds detailed information about that DataGrid row record. The user can edit the data on the modal dialog window, and clicking on an Update button closes the dialog and refreshes the DataGrid on the main form. The client wanted the same effect in the web application. After much R & D, I came with a solution which I want to share with you.

How to open a modal dialog window and how to get a return value from it?

Since I have no time, my source project contains all the code in one file (sorry! no separate business logic or data access logic classes given).

First of all, let's look at the JavaScript code for opening a modal window (the OpenModalDialog function in the Default.aspx page):

JavaScript
vReturnValue = window.showModalDialog(url,"#1","dialogHeight: 300px; dialogWidth: 600px;" +  
                    "dialogTop: 190px;  dialogLeft: 220px; edge: Raised; center: Yes;" + 
                    "help: No; resizable: No; status: No;");

The vReturnValue variable will contain the return value of the modal dialog window (when closed), which we can set in the ModalWindow form (in the detail.aspx page).

JavaScript
window.returnValue = true;
//(I am returning true to refresh Main page and false for no action)

You can give any value to be returned.

Postback from JavaScript

The most important thing for me is to refresh the grid when I get 'true' as the return value from the modal window. For that, I use (look in JavaScript's OpenModalDialog function):

JavaScript
__doPostBack(btnName,''); //(this will trigger 'btnName' butoon's Click event)

In the default.aspx page, I have used btnAddNew to add new data. Using the above JavaScript, I can fire the Click event of this button.

Note :To make this work, you have to set the default.aspx page's EnableEventValidation attribute to False. Setting this attribute to false enables us to fire a postback (server side events) using JavaScript.

Request

Any suggestions and additional functionality to improve this code is heartily welcomed. Feel free to ask any questions related to this article. Thanks...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead The One Technologies
India India
The One Technologies is a software development company in India. The One Technologies specializes in delivering state-of-the-art solutions in software development, web development, web application and Rich Internet Application (RIA) development, iPhone development, and robust and secure business processing infrastructures. Software development tools and technologies used extensively at The One Technologies include Microsoft .NET architecture, PHP/MySQL frameworks, Adobe Flex, Microsoft Silverlight, Microsoft SharePoint and BizTalk Servers, and Microsoft CRM and XRM platforms.

Comments and Discussions

 
GeneralNot working in ie8 when controls are inside updatepane asp.net 2 Pin
Member 20936071-Jul-10 12:35
Member 20936071-Jul-10 12:35 
GeneralMy vote of 1 Pin
Syed J Hashmi24-Jan-10 1:55
Syed J Hashmi24-Jan-10 1:55 
Questionwhy do dialogLeft and dialogTop not work with IE7 (works with firefox3.53)? Pin
lhq210024-Sep-09 8:24
lhq210024-Sep-09 8:24 
GeneralProblem with window.returnValue in safari Pin
mynameissuraj20-Oct-08 22:58
mynameissuraj20-Oct-08 22:58 
GeneralFiring other events for postback after the popup Closes Pin
Ammar Hassan5-Mar-08 22:26
Ammar Hassan5-Mar-08 22:26 
QuestionA standards compatible cross browser (non IE-only) version Pin
volkan.ozcelik15-Feb-07 7:53
volkan.ozcelik15-Feb-07 7:53 
Generalaccess to parent window from dialog Pin
mburnie17-Jul-06 21:53
professionalmburnie17-Jul-06 21:53 
Hi there

In addition to picking up the return value from a modal dialog you can also pass a reference for the parent window (or pretty much any reference or value for that matter) so you can access all of its controls, JavaScripts etc from within the dialog itself.

You need to make a small change to the showModalDialog() function parameters, change from:

vReturnValue = window.showModalDialog(url,"#1","dialogHeight: 300px; dialogWidth: 600px;
dialogTop: 190px; dialogLeft: 220px; edge: Raised; center: Yes;
help: No; resizable: No; status: No;");

to

vReturnValue = window.showModalDialog(url, window,"dialogHeight: 300px; dialogWidth: 600px;
dialogTop: 190px; dialogLeft: 220px; edge: Raised; center: Yes;
help: No; resizable: No; status: No;");

Replacing the "#1" with window means that you are passing a reference to the current window as a parameter as opposed to a string parameter. You could even create a wrapper object containing both the string parameter and a reference to the window if you want to and pass that through.

To access that parameter from the dialog you do the following:

var m_Parent = dialogArguments;

In the original sample m_Parent would have been set to "#1", but by passing a reference to the window as the parameter it now sets m_Parent as a reference type pointing to the parent window, with full access to that window.

Hope this helps

Mark Smile | :)
AnswerRe: access to parent window from dialog Pin
Kiran Beladiya18-Jul-06 2:40
professionalKiran Beladiya18-Jul-06 2:40 
GeneralRe: access to parent window from dialog Pin
mburnie18-Jul-06 4:24
professionalmburnie18-Jul-06 4:24 
GeneralRe: access to parent window from dialog Pin
light16918-Jul-06 21:51
light16918-Jul-06 21:51 
GeneralRe: access to parent window from dialog Pin
mburnie19-Jul-06 23:18
professionalmburnie19-Jul-06 23:18 
GeneralRe: access to parent window from dialog Pin
light16920-Jul-06 0:08
light16920-Jul-06 0:08 
GeneralRe: access to parent window from dialog Pin
mburnie20-Jul-06 2:02
professionalmburnie20-Jul-06 2:02 
GeneralRe: access to parent window from dialog Pin
Kiran Beladiya21-Jul-06 22:22
professionalKiran Beladiya21-Jul-06 22:22 
GeneralRe: access to parent window from dialog Pin
mburnie14-Aug-06 5:55
professionalmburnie14-Aug-06 5:55 
GeneralRe: access to parent window from dialog Pin
Paul Peeters23-Aug-06 9:13
Paul Peeters23-Aug-06 9:13 
GeneralRe: access to parent window from dialog Pin
anhtn5-Jun-08 15:47
anhtn5-Jun-08 15:47 
AnswerRe: access to parent window from dialog Pin
mburnie5-Jun-08 23:32
professionalmburnie5-Jun-08 23:32 
GeneralMessage Closed Pin
1-Sep-08 17:20
mayurmv1-Sep-08 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.