Click here to Skip to main content
15,882,017 members
Articles / Web Development / ASP.NET
Article

ModalDialogHelper Control in ASP.NET 1.1 and 2.0

Rate me:
Please Sign up or sign in to vote.
4.53/5 (17 votes)
16 Jun 20063 min read 1.2M   2.3K   74   63
An ASP.NET control that helps you to transfer data between a dialog page and its invoker page.

Introduction

This article shows an implementation of a server side control to transfer data between a modal dialog page and its invoker page.

Several days ago, I wrote an article "Transfer arguments between a modal page and an invoker page in ASP.NET 1.1". In that article, I had shown how to transfer an argument to a modal dialog and get the return value when the dialog is closed; I just want to explain how to implement that. Now, I wrap it into a server side control in ASP.NET 1.1. If you want to understand the way to implement this problem, you can check out my previous article the link of which is given above. Now, just follow me to see how to use this control :-). Download the source code and compile it. Create a new web application named TestDialogHelper. In WebForm1.aspx’s design window, right-click the toolbox and select "Add/Remove Item" to add the "DialogHelperControl.dll" that you have compiled. Then, an icon will be displayed in your toolbox like shown below:

Image 1

Drop it to your page, now you will see how the control looks at design-time:

Image 2

We want to invoke WebForm2.aspx as a modal dialog when we click Button1 on WebForm1.aspx. So, WebForm1.aspx is an invoker page. OK, set its ParentType to "InvokerPage" in the property grid:

Image 3

You should write the code to open the dialog page in the Click event of Button2, this control has an open method named "ShowDialog", and you can get the return value in the event "OnDialogReturn". See the code below:

C#
private void DialogHelperControl1_OnDialogReturn(string str)
{
    this.TextBox1.Text = str;
}

private void Button1_Click(object sender, 
                                System.EventArgs e)
{
    this.DialogHelperControl1.ShowDialog("WebForm2.aspx",
                                TextBox2.Text,500,300,false);
}

Here, your invoker page is completed. So easy! Now, you should build your dialog page "WebForm2.aspx". In this page, we implement a simple Add method and return the sum to the invoker page when it is closed. Set the property of the dialog helper control in the dialog page, like shown below:

Image 4

And in WebForm2.aspx, you can get the argument which is transferred from the invoker page in the event "OnArgumentSubmit". When all the things are done in this page, you should call the control’s "ReturnAndCloseDialog" method or "CloseDialog" method to return to the invoker page.

C#
private void Button1_Click(object sender, System.EventArgs e)
{
    TextBox3.Text = (Convert.ToInt32(TextBox1.Text)+
                      Convert.ToInt32(TextBox2.Text)).ToString();
}

private void Button2_Click(object sender, System.EventArgs e)
{
    this.DialogHelperControl1.ReturnAndCloseDialog(TextBox3.Text);
}

private void Button3_Click(object sender, System.EventArgs e)
{
    this.DialogHelperControl1.CloseDialog();
}

private void DialogHelperControl1_OnArgumentSubmit(string str)
{
    this.TextBox1.Text = str;
}

Here, all the processes of transferring are done. If you download the demo project, you will find two sub-folders in your unpackaged folder. Make a folder "TestDialogHelper" as a virtual folder with the same name as in your IIS, and the control’s project in another folder "DialogHelperControl". Open the solution, now you can compile the code and run it.

Image 5

Image 6

Image 7

[Updated]: My friend asked me why not set the element's value in the client on dialog return, because in many cases, we only need the return value to be put into an element's value (as <input>) in the client. So, I added two properties to implement this function. "AutoPostBackWhenDialogReturn" controls whether the page will submit when the user closes the dialog. "ClientScriptOnDialogReturn" is just the client script code to execute when the user closes the dialog. You will not use <script> and </script> to wrap the code. In fact, it is the code of the client function "__ClientScriptOnDialogReturn" which is registered in the "OnLoad" event of the control.

Image 8

Please let me know if there are bugs in this article. Your feedback and advice are most welcome.

History

  • 2005-08-22: Created version 1.
  • 2005-08-25: Added functions that allow the user to execute the script on the client on dialog return.
  • 2006-05-18: Added property DialogType that allows a user to open a dialog in two ways: modal and modeless.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
China China
When I encounter a problem, I am charmed.
When I make it out, I am cordial.

Enjoy your coding !

Comments and Discussions

 
GeneralRe: Undable to run in asp.net 1.1 Pin
Rouser28-Jun-06 14:40
Rouser28-Jun-06 14:40 
GeneralRe: Undable to run in asp.net 1.1 Pin
ravihd28-Jun-06 17:36
ravihd28-Jun-06 17:36 
GeneralCaching Pin
Joey Chömpff14-Jun-06 23:48
Joey Chömpff14-Jun-06 23:48 
GeneralRe: Caching Pin
Rouser15-Jun-06 15:49
Rouser15-Jun-06 15:49 
GeneralDialogHelper and FreeTextBox Pin
Shpexy24-May-06 23:50
Shpexy24-May-06 23:50 
GeneralDemo does not work properly in 1.1 Pin
zsim24-May-06 6:26
zsim24-May-06 6:26 
QuestionProblem in ASP.NET 1.1 - OnArgumentSubmit() Fails to assign value to controls. Pin
Diego Liñan15-May-06 7:59
Diego Liñan15-May-06 7:59 
AnswerRe: Problem in ASP.NET 1.1 - OnArgumentSubmit() Fails to assign value to controls. Pin
Rouser17-May-06 16:27
Rouser17-May-06 16:27 
GeneralRe: Problem in ASP.NET 1.1 - OnArgumentSubmit() Fails to assign value to controls. Pin
Diego Liñan18-May-06 5:33
Diego Liñan18-May-06 5:33 
GeneralRe: Problem in ASP.NET 1.1 - OnArgumentSubmit() Fails to assign value to controls. Pin
Rouser18-May-06 14:19
Rouser18-May-06 14:19 
Generaldocument.body is null or not an object Pin
Shpexy14-May-06 22:27
Shpexy14-May-06 22:27 
GeneralRe: document.body is null or not an object Pin
Rouser17-May-06 14:26
Rouser17-May-06 14:26 
GeneralAny Licence Issues to use this code Pin
Usman Shaik4-Apr-06 5:00
Usman Shaik4-Apr-06 5:00 
GeneralRe: Any Licence Issues to use this code Pin
Rouser4-Apr-06 18:43
Rouser4-Apr-06 18:43 
GeneralIt does not work for me. Shows 2 windows Pin
Vluzhko28-Nov-05 3:19
Vluzhko28-Nov-05 3:19 
GeneralRe: It does not work for me. Shows 2 windows Pin
Rouser28-Nov-05 4:14
Rouser28-Nov-05 4:14 
GeneralRe: It does not work for me. Shows 2 windows Pin
Vluzhko28-Nov-05 8:24
Vluzhko28-Nov-05 8:24 
GeneralMasterPage Pin
Rieni Miclis14-Nov-05 2:46
Rieni Miclis14-Nov-05 2:46 
GeneralRe: MasterPage Pin
Rouser14-Nov-05 5:24
Rouser14-Nov-05 5:24 
GeneralRe: MasterPage: I have a solution Pin
matthiaswoehlte24-May-06 5:32
matthiaswoehlte24-May-06 5:32 
GeneralRe: MasterPage: I have a solution [modified] Pin
Rouser25-May-06 14:16
Rouser25-May-06 14:16 
QuestionUsing with VB Web Application Pin
MacIT9-Sep-05 13:28
MacIT9-Sep-05 13:28 
AnswerRe: Using with VB Web Application Pin
Rouser12-Sep-05 18:43
Rouser12-Sep-05 18:43 
QuestionHow about a client side dialog control? Pin
Liu Junfeng30-Aug-05 19:52
Liu Junfeng30-Aug-05 19:52 
AnswerRe: How about a client side dialog control? Pin
Anonymous30-Aug-05 20:02
Anonymous30-Aug-05 20:02 

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.