Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 08-22-2005 10:54 AM by Brendan Tompkins
Filed under:

[Advertisement]

Comments

Chris Wallace wrote re: An ASP.NET Modal Panel Control
on 08-22-2005 11:38 AM
Looks pretty sweet based on the demo. Nice work!
Raymond Lewallen wrote re: An ASP.NET Modal Panel Control
on 08-22-2005 11:56 AM
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?
Brendan Tompkins wrote re: An ASP.NET Modal Panel Control
on 08-22-2005 1:45 PM
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
Charles Fisher wrote re: An ASP.NET Modal Panel Control
on 08-22-2005 3:17 PM
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).
Brendan Tompkins wrote re: An ASP.NET Modal Panel Control
on 08-22-2005 3:41 PM
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
Sergio wrote re: An ASP.NET Modal Panel Control
on 08-22-2005 4:39 PM
Have you tried using an IFrame as the container instead of a DIV? The IFrame will block out the drop down lists.
Brendan Tompkins wrote re: An ASP.NET Modal Panel Control
on 08-22-2005 4:52 PM
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.


Sean Brennan wrote re: An ASP.NET Modal Panel Control
on 08-23-2005 7:09 PM
Wow! Great job. The demo is very impressive.

Christopher Steen wrote Link Listing - August 29, 2005
on 08-29-2005 11:59 PM
.NET Security Resources from the PAG
[Via: Anil
John ]
An ASP.NET Modal Panel Control [Via:
btompkins...
Gino Flores wrote re: Title Property
on 09-30-2005 12:48 PM
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?
Thomas Kern wrote re: An ASP.NET Modal Panel Control
on 10-22-2005 10:03 PM
hello,

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

cheers
Firoz Ansari wrote re: An ASP.NET Modal Panel Control
on 12-10-2005 12:09 AM
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
dev wrote re: An ASP.NET Modal Panel Control
on 01-19-2006 1:21 PM
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...
Andy L wrote re: An ASP.NET Modal Panel Control
on 02-21-2006 11:35 AM
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.
Andrew wrote ASP.Net 2.0
on 02-28-2006 9:22 AM
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';
ASP.net 2.0 blues wrote re: An ASP.NET Modal Panel Control
on 05-04-2006 2:28 PM
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
randal wrote re: An ASP.NET Modal Panel Control
on 05-18-2006 11:46 AM
Ted,

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

Cheers,
randal/
Imran Balouch wrote re: An ASP.NET Modal Panel Control
on 06-20-2006 11:36 AM
Thats awesome....
Great job boss...
Imran Balouch wrote re: An ASP.NET Modal Panel Control
on 06-21-2006 5:53 AM
Hi,

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

Regards
Edwin wrote re: An ASP.NET Modal Panel Control
on 08-23-2006 3:18 PM
how would you make the window resizeable?
Rushabh wrote re: An ASP.NET Modal Panel Control
on 09-18-2006 6:25 PM
Stephan wrote re: An ASP.NET Modal Panel Control
on 11-22-2006 3:09 AM

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

Eden Ridgway's Blog wrote Modal Popup Panel
on 01-03-2007 9:56 PM

Modal Popup Panel

vikas misra wrote re: An ASP.NET Modal Panel Control
on 01-04-2007 2:00 AM

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

Mandar wrote re: An ASP.NET Modal Panel Control
on 02-19-2007 11:13 AM

wow itz mindblowing!

JS@Thales re: ModalPanel wrote re: An ASP.NET Modal Panel Control
on 03-14-2007 3:34 PM

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>

saeed wrote re: An ASP.NET Modal Panel Control
on 03-29-2007 3:34 PM

Thanks lot ! :)

Freddy wrote re: An ASP.NET Modal Panel Control
on 04-21-2007 12:21 AM

Simply AMAZING !

Just need to block all other control from being focused

Steve wrote re: An ASP.NET Modal Panel Control
on 09-14-2007 9:14 AM

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?

toni wrote re: An ASP.NET Modal Panel Control
on 12-18-2007 3:35 PM

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

Mike Russo wrote re: An ASP.NET Modal Panel Control
on 12-29-2007 6:30 PM

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.

John Middleton wrote re: An ASP.NET Modal Panel Control
on 01-08-2008 6:35 PM

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

Gersy wrote re: An ASP.NET Modal Panel Control
on 03-03-2008 8:39 AM

hello, it's great control

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

Jeff wrote re: An ASP.NET Modal Panel Control
on 03-27-2008 8:45 PM

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

vicky wrote re: An ASP.NET Modal Panel Control
on 09-01-2008 11:04 PM
Kelton wrote re: An ASP.NET Modal Panel Control
on 05-11-2009 2:31 PM

Hi. In the future I'm going to keep here links to their sites. But I do not worry about the sites where my link is removed. So if you do not want to see a mountain of links, simply delete this message. After 2 weeks, I will come back and check.

Jacy wrote re: An ASP.NET Modal Panel Control
on 05-13-2009 8:11 PM

Hi everyone. It is curious that physical courage should be so common in the world and moral courage so rare. Help me! Could you help me find sites on the: Wall clock. I found only this - <a href="www.cartogis.org/.../synthroid">synthroid 30 mg</a>. Quality review of top online health products synthroid improve the action of your thyroid gland with synthroid synthroid should be taken in a single. Synthroid internet is your source for high quality medications go to there are synthroid internet is in educating and synthroid internet lottery go. THX :-(, Jacy from Kuwait.

Fai wrote re: An ASP.NET Modal Panel Control
on 05-22-2009 11:53 AM

Good afternoon. By the time I'd grown up, I naturally supposed that I'd be grown up. Help me! I can not find sites on the: Synthroid, phentermine safety, adipex non prescription, plendil pills x mg, obsolete anytime soon adding that of synthroid tumors and though regulating.. I found only this - <a href="http://turbo-tax.biz">turbo tax</a>. See our article - should patients with hypothyroidism be treated with both levothyroxine synthroid, etc and liothyronine cytomel. Do synthroid help you lose weight while maintaining and treating hypothyroidism? Best regards :cool:, Fai from Slovenia.

Scott Leslie wrote re: An ASP.NET Modal Panel Control
on 06-28-2009 4:35 PM

Demo page not working :-(

Add a Comment

(required)  
(optional)
(required)  
Remember Me?