CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

An ASP.NET Modal Panel Control

One UI element that has always been tricky to create in ASP.NET applications is the Modal Window.  Often, applications will present an entire page for simple property editing, in situations where a modal window is a more elegant, and straight-forward UI element.  This is probably due mainly to the fact that there are no intrinsic browser controls that work well for this, and are easy to deal with on the server side.

SDDs Are So Last Year

A while back, I proposed a solution for creating Modal Windows, which I called Sticky Draggable Divs (SDDs)

If you do Web Application development, you've no doubt struggled with the task of creating Modal forms.  You have different options for doing this, each with their own advantages and pitfalls.  Hosting your form in a new browser instance (window.open()) is really a kludge, and many users have popup blocking software which will break this approach. Using true Modal dialogs (window.showModalDialog()) doesn't work well from within ASP.NET. Using draggable DIV elements solves some of these issues, but has its own set of problems. Dragging DIV elements involves using DHTML and JavaScript and the script that you have to write to enable this can be tricky. Toss browsers other than IE into the mix, and you've got even more problems.

A big problem with this approach was that it relied on DHTML Behaviors which is IE only and can cause problems with browsers running with a high security level. After living with this solution for over a year, and it’s drawbacks, I finally looked for a better way.

A Better Solution

I’ve come up with a new solution which uses standard ASP.NET Controls, DHTML and cross-browser JavaScript. ModalPanel is implemented as a sub-classed ASP.NET Panel control.  Since it’s a Panel, it can be programmed in a way you’re probably already familiar with if you do ASP.NET development.

This control  adds a Title Bar, and optional close button to the Panel.  When the user drags the title bar, the entire panel is dragged.  It also uses cookies to remember it’s position on postbacks and return visits to the page.

You can experiment with it here at my demos site.

Implementation

Step 1: Reference my Tompkins.Web dll in your project. 

Download the source code here, the DLL here,  or an entire example project here.  Add a project reference to the DLL and optionally add the control to your toolbar in Visual Studio. 

You’ll  then  want to create an instance of the control on your page of course.

 <cc1:modalpanel id="ModalPanel1" title="Your Title Here" runat="server"></cc1:modalpanel>

Step 2: Add the CSS styles and images to your project. 

See the style.css, toolbar_close.gif and window_titlebg.gif files in either download for examples.

Step 3: Set Properties

There are some properties that you can set, such as the window’s title, and you can also alter the CSS classes used to style the thing. 

Step 4: Handle the WindowClosed Event, and show/hide the control.

The only event to concern yourself with is the WindowClosed event.

  public event EventHandler WindowClosed;

All your application must do is handle this event, and show/hide the control as you would with any other ASP.NET Panel…

   private void ModalPanel1_WindowClosed(object sender, System.EventArgs e)

    {

      this.ModalPanel1.Visible = false;

    }

You’re also going to want to add some code to show the panel in the first place, like so.

    private void ButtonPlus1_Click(object sender, System.EventArgs e)

    {

      this.ModalPanel1.Visible = true;

    }

That’s it!  If your have trouble getting it to drag properly, or if it looks all funky, make sure you have your CSS stylesheet referenced properly, and that you have the CSS classes defined as I show in my example.

-Brendan



Comments

Chris Wallace said:

Looks pretty sweet based on the demo. Nice work!
# August 22, 2005 11:38 AM

Raymond Lewallen said:

Very nice. I can think of a few places where this will come in handy. Does it "float" on top of other panels, so that it appears like a true modal window?
# August 22, 2005 11:56 AM

Brendan Tompkins said:

Raymond,

Yes, but windowed controls, such as DropDownLists will float on top of it. I'm working on a DDL that isn't windowed, but don't have it yet.

BTW, this is one of the last dhtml/javascript controls I think I'm going to bother writing. It takes sooo much time and effort, especially when there are great tools out there like ComponentArt and Telerik.

This was a case where I just couldn't find anything that did exactly what I wanted it to.

-B
# August 22, 2005 1:45 PM

Charles Fisher said:

Great work B!

One thing.... the modal panel does not stop you from clicking on the "Show Modal Dialog" button in the background. It may be more trouble than it's worth but you can probably just iterate through the document elements to disable/enable when appropriate.

Man... if I had the time, this would be kinda fun (writing a blog).
# August 22, 2005 3:17 PM

Brendan Tompkins said:

Chaz.. Yep you're right. In this sense, it's not truly Modal (or really modal at all) just a floating window.

I thought about what would have to be done to do this, you'd have to check the parent, and if it's not the modal window, drop the event.

May look at this if I get the time.

B
# August 22, 2005 3:41 PM

Sergio said:

Have you tried using an IFrame as the container instead of a DIV? The IFrame will block out the drop down lists.
# August 22, 2005 4:39 PM

Brendan Tompkins said:

Hi Sergio

As far as I can tell, the problem with an iframe, is that the controls you place inside it are no longer contained within the same aspx or ascx contol, and makes postback page processing hard.


# August 22, 2005 4:52 PM

Sean Brennan said:

Wow! Great job. The demo is very impressive.

# August 23, 2005 7:09 PM

Christopher Steen said:

.NET Security Resources from the PAG
[Via: Anil
John ]
An ASP.NET Modal Panel Control [Via:
btompkins...
# August 29, 2005 11:59 PM

Gino Flores said:

Very nice Brendan

While I was playing with the control I tried to set the title through code using:

ModalPanel1.Title = "test"

But, I was unable to do it ...
Is there anything else I have to do besides that line of code?
# September 30, 2005 12:48 PM

Thomas Kern said:

hello,

looks good, unfortunately the drag and drop feature doesnt work for me. i am using opera 8.5

cheers
# October 22, 2005 10:03 PM

Firoz Ansari said:

Hi Brendan,
I have tried this kind of mechanism to show dialog box before but its have one major problem when you move this child window over any window control like dropdown etc. in the page. Problem is that z-index of window control is infinity and so every time, if you window places above any dropdown/listbox, DIV layer will go behind the window control.

IMO, showModalDialog is a perfect solution to show modal dialog box.

Thanks
Firoz Ansari
# December 10, 2005 12:09 AM

dev said:

Looks Great,
I am trying to use model panel with asp.net 2.0 and VS2005.but unfortunately the drag and drop feature is not working for me.Is there any trick to resolve this and the window is displaying at right top corner.If any one experirenced with the same problems,Please save my time.thanks in advance.

---Dev...
# January 19, 2006 1:21 PM

Andy L said:

As mentioned earlier, an iframe will block out windowed controls such as dropdownlists.
To make it work with ASP.NET, just use the panel as usual but float it on top of an iframe of the same size.
# February 21, 2006 11:35 AM

Andrew said:

I'm working with ASP.Net 2.0, I found the drag function wasn't working in Firefox.

Its due to the position attributes attributes, in Firefox 1.5 it adds the "px" to the end, and needs it.

Modify the Move Javascript function, add the "px"...

z.style.left = parseInt(z.style.left) + (eventX - x) + 'px';
# February 28, 2006 9:22 AM

ASP.net 2.0 blues said:

Hi,

I am unable to make the drag function work in asp.net 2.0. Forgive my ignorance, but where and how do I edit the Javascript to put in the fix as below ? :

Modify the Move Javascript function, add the "px"...
z.style.left = parseInt(z.style.left) + (eventX - x) + 'px';

Thanks,

TED
# May 4, 2006 2:28 PM

randal said:

Ted,

Look in source file ModalPanel.cs line 92 and 93 (Inside the Scripts #region).

Cheers,
randal/
# May 18, 2006 11:46 AM

Imran Balouch said:

Thats awesome....
Great job boss...
# June 20, 2006 11:36 AM

Imran Balouch said:

Hi,

Is there any way that I can use this control in some ASP page.

Regards
# June 21, 2006 5:53 AM

Edwin said:

how would you make the window resizeable?
# August 23, 2006 3:18 PM

Rushabh said:

# September 18, 2006 6:25 PM

Stephan said:

Hi,

You should watch the demo on http://www.inchl.nl about asp.net modaldialogs. The freeware code on that website is really cool and covers all of the items in this forum.

See (executable) desktoprecording: http://www.inchl.nl/recordings/aspnet_modaldialogs.zip

Kind regards,

Stephan Smetsers

http://www.inchl.nl

# November 22, 2006 3:09 AM

Eden Ridgway's Blog said:

Modal Popup Panel

# January 3, 2007 9:56 PM

vikas misra said:

i downloaded modalpanelexample.zip and tested its floating fine but when i put controls

like button textbox on the page default.aspx it dosent works as modal i can type any thing in textboxes and can click any button ie its not working as modal window.i am using vs.net2005 can anyone resolve this issue

# January 4, 2007 2:00 AM

Mandar said:

wow itz mindblowing!

# February 19, 2007 11:13 AM

JS@Thales re: ModalPanel said:

Running IE 6.0.2900.2180 sp2 on XPPro...

Server Error in '/Brendan' Application.

--------------------------------------------------------------------------------

Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a <customErrors> tag within a "web.config" configuration file located in the root directory of the current web application. This <customErrors> tag should then have its "mode" attribute set to "Off".

<!-- Web.Config Configuration File -->

<configuration>

   <system.web>

       <customErrors mode="Off"/>

   </system.web>

</configuration>

Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's <customErrors> configuration tag to point to a custom error page URL.

<!-- Web.Config Configuration File -->

<configuration>

   <system.web>

       <customErrors mode="RemoteOnly" defaultRedirect="mycustompage.htm"/>

   </system.web>

</configuration>

# March 14, 2007 3:34 PM

saeed said:

Thanks lot ! :)

# March 29, 2007 3:34 PM

Freddy said:

Simply AMAZING !

Just need to block all other control from being focused

# April 21, 2007 12:21 AM

Steve said:

This is exactly what i was looking for for a quick and simple 'help' page.  For somereason though, it is not draggable when inside an AJAX update panel.  Any idea why?

# September 14, 2007 9:14 AM

toni said:

HI

All

i am able to get the user control on to share point portal but i am not able to see any controls on the sharepoint .its like the usere control is empty and when i am dragging and droping the control on the update pannel i am having issues like i am able to drop the timer tool from the ajax  extensions which stays still but all the other controld from standard tool box can be moved

# December 18, 2007 3:35 PM

Mike Russo said:

This is about the comment on using an IFRAME to block out drop down list. Use basically just use an empty IFRAME and shim it under the div layer to prevent the drop down from being on top. Controls you put on the DIV are not part of the IFRAME.

What I mean by shim is to position it and size it under the DIV tag as an empty container. When you move the DIV make sure the IFRAME shim moves as well.

Works in IE 5.5 and above. Not a concern in any other browsers.

# December 29, 2007 6:30 PM

John Middleton said:

Brendan,

I want to use your modal display to display a help.aspx file but have not been able to figure it out. Can you help?  Thanks.  -john

# January 8, 2008 6:35 PM

Gersy said:

hello, it's great control

but how to disable (Validation) when closing from close button ?????????????????/

# March 3, 2008 8:39 AM

Jeff said:

Hi Brendan,

When I quickly click drag and drag the mouse from the draggable toolbar of the modal panel, it seems to stay at it's place after I stop dragging and after that as soon as I move the mouse over the toolbar area, the div  starts moving with the mouse without even clicking over the toolbar. Please advice as to how we can prevent this problem

# March 27, 2008 8:45 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!