Click here to Skip to main content
Licence CPOL
First Posted 12 Jul 2006
Views 132,724
Downloads 1,974
Bookmarked 54 times

ShowModalDialog effect in ASP.NET web forms

By Kiran Beladiya | 5 Feb 2007
How to open modal a dialog window in a browser (Internet Explorer) and do something with the parent window after return.
 
Part of The SQL Zone sponsored by
See Also
3 votes, 30.0%
1

2
1 vote, 10.0%
3
3 votes, 30.0%
4
3 votes, 30.0%
5
3.27/5 - 10 votes
μ 3.27, σa 2.98 [?]

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

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

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

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

About the Author

Kiran Beladiya

Software Developer (Senior)
Tatvasoft Company
India India

Member
TatvaSoft is a software development company in India. The company has acquired a CMMi maturity level 3 and Microsoft Gold Certified Partnership. TatvaSoft 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 TatvaSoft include Microsoft .NET architecture, PHP/MySQL frameworks, Adobe Flex, Microsoft Silverlight, Microsoft SharePoint and BizTalk Servers, and Microsoft CRM and XRM platforms.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralCross Browser showModalDialog Replacement PinmemberRoman G.10:41 24 Mar '11  
GeneralNot working in ie8 when controls are inside updatepane asp.net 2 PinmemberMember 209360713:35 1 Jul '10  
GeneralMy vote of 1 PinmemberSyed Javed2:55 24 Jan '10  
Questionwhy do dialogLeft and dialogTop not work with IE7 (works with firefox3.53)? Pinmemberlhq21009:24 24 Sep '09  
GeneralProblem with window.returnValue in safari Pinmembermynameissuraj23:58 20 Oct '08  
GeneralRe: Problem with window.returnValue in safari PinmemberRoman G.7:53 22 May '11  
GeneralFiring other events for postback after the popup Closes PinmemberAmmar Hassan23:26 5 Mar '08  
QuestionA standards compatible cross browser (non IE-only) version Pinmembervolkan.ozcelik8:53 15 Feb '07  
Generalaccess to parent window from dialog Pinmembermburnie22:53 17 Jul '06  
AnswerRe: access to parent window from dialog PinmemberKiran Beladiya3:40 18 Jul '06  
GeneralRe: access to parent window from dialog Pinmembermburnie5:24 18 Jul '06  
GeneralRe: access to parent window from dialog Pinmemberlight16922:51 18 Jul '06  
GeneralRe: access to parent window from dialog Pinmembermburnie0:18 20 Jul '06  
GeneralRe: access to parent window from dialog Pinmemberlight1691:08 20 Jul '06  
Thx for your repy.
Actually ,there's nothing wrong with M_parent or btnName . The problem is _doPostBack cannot excute.
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="A1.aspx.cs" Inherits="Test_A1" EnableEventValidation="false" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<META http-equiv="Pragma" content="no-cache">
 
</head>

<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" .....
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ....



 <asp:LinkButton ID="Refresh" runat="server" OnClick="LinkButton1_Click" Visible="true">Refresh 
<asp:LinkButton ID="Add" runat="server" OnClientClick="showItem();" CausesValidation="False" EnableViewState="False">Add
<input id="Button1" type="button" value="button" önclick="javascript:__doPostBack('Refresh','')" />
 
</form>
<script language="javascript">
function showItem()
{
window.showModalDialog('A2.aspx',window,'dialogWidth:600px;dialogHeight:500px;dialogLeft:300px;dialogTop:400px;resizable:yes;status:no');
 
__doPostBack('Refresh','');
document.getElementById('Button1').click();
return true;

}
 
</script>

</body>
</html>
 
In Refresh Link Button I call Databind to refresh GridView. But it doesn't work of _doPostBack()
 
But when I add
 
protected void Add_Click(object sender, EventArgs e)
{
// string strScript = "<script >\n" +
//" var strReturn = window.showModalDialog('A2.aspx','desc','dialogWidth:423px;dialogHeight=355px');\n" +
//" __doPostBack('Refresh',strReturn);" +
//"</script>";
// if (!this.IsClientScriptBlockRegistered("OpenDialogScript"))
// this.RegisterStartupScript("OpenDialog", strScript);
 
}
 
to Add button , not run showItem javascript . It's Ok of _doPostBack. I don't know why.
 
I add a new button to call _dopostback, I click it mannually ,it's OK,
but I use
document.getElementById('Button1').click();
 
It doesn't work!
 
Confused | :confused:
 


GeneralRe: access to parent window from dialog Pinmembermburnie3:02 20 Jul '06  
GeneralRe: access to parent window from dialog PinmemberKiran Beladiya23:22 21 Jul '06  
GeneralRe: access to parent window from dialog Pinmembermburnie6:55 14 Aug '06  
GeneralRe: access to parent window from dialog PinmemberPaul Peeters10:13 23 Aug '06  
GeneralRe: access to parent window from dialog Pinmemberanhtn16:47 5 Jun '08  
AnswerRe: access to parent window from dialog Pinmembermburnie0:32 6 Jun '08  
GeneralRe: access to parent window from dialog Pinmembermayurmv18:20 1 Sep '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 5 Feb 2007
Article Copyright 2006 by Kiran Beladiya
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid