|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionOften when programming in ASP.NET, you want to pop up a dialog window that requests some kind of user input. When the user is finished with the dialog and closes it, you want the main form to fire a server event that performs some kind of refresh or other actions. Surprisingly, this task is not as easy as it seems, and a Google search turns up lots of confusing information. You can easily end up writing much more code than you need to. After some experimenting, I think I found a reasonably neat and tidy way to accomplish this, and so that I never have to do it again, I wrapped the code for opening and closing the dialog into a pair of server controls. This article presents a pair of server controls, DialogOpenerHere is the code for the using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Web;
namespace ExWebCtlLib
{
[DefaultProperty("Text"), ToolboxData("<ExWebCtlLib:DialogOpener
runat="server"></WebCtlLib:DialogOpener>")]
public class DialogOpener : System.Web.UI.WebControls.WebControl,
IPostBackEventHandler
{
The code is mostly very straightforward, just some simple properties with getters and setters. The interesting property is the <input id="ExFxDialogOpener" type="button" value="Image Gallery"
class="Button" onclick="javascript:OpenDialog(
'/html/Forms/Fx/ImageGallery.aspx?PostBackEventReference=
__doPostBack(%27_ctl15%24ImageGalleryOpener%27%2c%27%27)
&DialogParam=SelectImage', 'Dialog', 456, 580, 'yes' );">
The The script calls the JavaScript function function OpenDialog( url, name, height, width, scrollbars )
{
if( scrollbars == null )
scrollbars = "yes";
var top = (screen.height - height) / 2;
var left = (screen.width - width) / 2;
window.open( url, name, "width = " + width + ",
height = " + height + ", menubar = no,
scrollbars = " + scrollbars + ", toolbar = no,
location = no, directories = no, resizable = no,
top = " + top + ", left = " + left );
(Since we have standard JavaScript registration templates in our shop, DialogCloserThe Here's the code for public class DialogCloser : System.Web.UI.WebControls.WebControl
{
public void Close()
{
if( HttpContext.Current.Request[ "PostBackEventReference" ] == null )
Page.RegisterStartupScript("__close", "<script>window.close();</script>");
else
{
string script = String.Format("<script>window.opener.{0};
window.close();</script>", HttpContext.Current.Request[
"PostBackEventReference" ] );
Page.RegisterStartupScript( "__close", script );
}
}
}
Here's an example of the JavaScript that gets registered when <script>
window.opener.__doPostBack('_ctl15$ImageGalleryOpener','');
window.close();
</script>
Compiling the ControlsTo compile the controls, just create a couple of new server control classes in your own server control library. If you don't have a server control library, create a new project of type "Class Library", drop in the code, compile, then right click on your toolbox, click Add/Remove Items and browse to the .dll you just created. Click OK and the two controls will appear on your toolbox. Using the ControlsUsing the controls is easy. Create two aspx pages. One will be the main form and the other will be the dialog. Drop the Drop the DialogCloser.close();
Run the main form. Click on the DialogOpener button. Close the dialog. The ConclusionThis short article has shown you how to create a pair of useful server controls that you can drop on a pair of forms to handle dialog opening and closing, and calling a server event on the main form when the dialog closes. I hope this saves you some headache searching for the answer like I did!
|
||||||||||||||||||||||