Datagrid Girl

Marcie, ASP.NET Datagrid Blogger Girl

showModalDialog and postbacks in ASP.NET

I learned something new today that my coworker Dennis spent several agonizing hours trying to solve.  He was using window.showModalDialog to open a modal window on the client from Javascript, but whenever that page posted back, it would spawn off a new window.  (A normal window.open worked fine, this was only a problem for modal windows).  Some searching turned up several people with the same problem, and the solution once found (here), was quite simple:  just include this line in the < HEAD > html of the modal window:

<base target=_self>

Hope this saves someone else some time!
Marcie

Comments

bestcomy said:

It's not work under IE 5.0
# August 11, 2005 9:07 PM

Marcie said:

Interesting, it works great for us under IE 6, and both <base> and showModalDialog were around in IE 5, so I'm surprised that it doesn't work there.
# August 11, 2005 9:13 PM

Ron Buckton said:

The standard solution I always employed was to load a dummy page in the modal dialog that only writes out an IFRAME to the window (height/width 100% and no border). The IFRAME is given a SRC tag of the actual page to be viewed. In this case, postbacks and whatnot will work fine in a showModalDialog or showModelessDialog.
# August 11, 2005 11:42 PM

MatHertel said:

I've run into this problem too and did the following:
* when entering the page i give window.name a unique name.
* i set the target of my links to this name

btw:
window.navigate(newUrl) or
window.location.href = newUrl will open a new window.
use window.open(newUrl, window.name) instead.

and:
disable all caching for pages that are used in modal dialogs. When IE has to show a url that's cached it loads from the cache without asking the server in some circumstances.

better than ron's approach but I can't check whether it works fine with IE 5.x
(got an upgrade some years ago)
# August 13, 2005 8:22 AM

Shake said:

Thank you so much MatHertel the window.name works great!!!

Btw, <base target=_self> didn't work for me :S
# August 25, 2005 1:07 PM

Elen said:

It solve my problem when i was working on progress bar with VB.net.

Thank a lot.

# July 4, 2007 5:32 PM

Anand said:

I want to write the AJAX response in the Modal Window. Has any one got the  solution for this?

Thanks In advance

# July 13, 2007 5:12 AM

Neeraj said:

Thanks Marcie for this solution.

<base target="_self" />

My team must have spent some 30 man hours on this problem.

btw, I saw some comments that this solution didn't work for them... Make sure they are using window.showModalDialog and not window.open.

# August 9, 2007 4:16 PM

Pradeep Bisht said:

Exact solution

thanks

# August 16, 2007 3:07 AM

Pavel said:

Thanks Marcie,

I spent 6 hours on this problem

and <base target="_self" />  resolve my problem

# August 17, 2007 6:46 AM

Dan O'Connell said:

Perfect.  Similar story to others: wasted a few hours on this, until I saw your solution.  Thanks.

# August 20, 2007 12:42 PM

Brent Campbell said:

Unbelievable, never thought that would be the resolution, again, more than a few hours wasted and thinking there was no solution. Cheers!

# September 7, 2007 6:27 AM

Ali said:

Wasted 5 minutes.  Did a google search.  Found this great article.  Very happy that I did not waste 30 man hours.  Thanks everyone.

# September 7, 2007 6:20 PM

Mahesh Nihalani said:

Worked great... Thanks...

# September 12, 2007 4:51 PM

Karu Anna said:

Base target solution worked fine for me after trying other things for hours. Thanks for your effort to find and post this solution.

# September 26, 2007 7:06 AM

Suresh said:

thanks .. saved my day.

# September 27, 2007 5:33 PM

Indika said:

Worked like a charm !

:-)

# September 28, 2007 3:25 AM

Kenny said:

Thanks for posting this - it works

# October 1, 2007 11:42 AM

Alberto said:

Works for me!!!

Uhul!!!

Thanks Marcie!

# October 3, 2007 11:07 AM

Hariprasad said:

Great solution,

really u saved my time. I was breaking my head since 2 days..

once again thanks,

hari

# October 9, 2007 3:24 AM

DotNetter said:

Works fine...  Good article...  Thanks a lot...

# October 16, 2007 2:12 AM

Anand said:

Working Fine

thnx

# November 14, 2007 11:10 AM

Naman said:

Why it is not solving my issue.. I have grid in modal popup.. When i click Edit button it is getting postback. All code gets executed but on poup page it is not coming in edit mode.. I have edit item template defined... and it is not visible on screen on click of edit button..

What to do ....????

# November 19, 2007 9:25 AM

veeru said:

Fantastic,

I was about to move to window.open.

saved my day

Thanks

# November 28, 2007 3:12 AM

LAKSMANAN said:

<base target=_self>

Works fine..

but the title of IE goes away.. when is posted...

is there any solution ? that will not make any change in the iE title

# December 3, 2007 4:58 AM

Dave Johnson said:

Another happy customer! <BASE Target=_self> works fine for me.

Thanks for saving me a lot of time.

# January 7, 2008 5:54 AM

Amit said:

thanks a lot , it is working fine. I spent three hours and breaking my head

# January 11, 2008 4:57 AM

Neelima said:

It's not working for me.

I'm using master pages.

# January 23, 2008 4:47 AM

Neelima said:

It's not working for me.

I'm using master pages.

function VendorContact(VendorId)

   {

     Options = "dialogTop:50px; dialogLeft:50px;dialogWidth:340px;dialogHeight:155px;help:no;scroll:no;status:no"

     OpenReportJS = window.showModalDialog("VendorContact.aspx?VID=" + VendorId,"_blank",Options) ;

   }  

And i'm getting the vendorid from xsl.

I got an error like "Object doesn't support this action".

I have been trying htis from past 5 hours.

Please help me out in giving the solution.

# January 23, 2008 5:30 AM

Roberto said:

thanks a lot , it is working fine. I spent three hours and breaking my head

# February 5, 2008 5:25 AM

Frederick Chapleau weBlog on IT. said:

The answer on how to do postback in modal / modaless diaglogs. weblogs.asp.net/.../archive

# February 6, 2008 12:24 AM

SAV said:

You've saved my life!

Thanks

# February 7, 2008 2:36 AM

Stronghold said:

Very good solution - i have been fighting with this several times - great - thanks :)

# February 7, 2008 6:26 AM

Len said:

<base target="_self" />

worked great

Many thanks

# February 15, 2008 5:03 PM

Panya said:

Love you marcie.

You save my day.

# February 23, 2008 2:07 PM

Renz said:

Really worked..Thanks :)

# February 25, 2008 1:58 AM

Beatriz said:

Lots of thanks!!!

Be careful, because it doesn't runs if smartnavigation is true.

Sorry for my english.... :(

# March 9, 2008 5:08 PM

Dan said:

When I use <base target="_self" /> I can't use the Virtual Earth Mapping control under IE 6

# March 24, 2008 3:14 PM

Irwin Y. Quintana Fitta said:

Thank you Marcie, you save my life.

Now I have a problem when I'm trying to access opener's controls from the child modal window but this problem doesn't appear when the child window was open by using "window.open" instead "window.showModalDialog".

Do anybody know how to solve this issue?

Thanks in advance.

# March 26, 2008 10:31 AM

Sathish said:

Thanks a lot Marcie!

Good solution for this issue.

Thanks a lot again.

For those who need information about  "showModalDialog" check the following link

msconline.maconstate.edu/.../jsdhtml07-05.htm

# March 28, 2008 5:16 AM

Ryan said:

What an easy solution, I was running out of ideas!  Thank you.

# April 8, 2008 4:30 PM

SP said:

It worked for me. Great solution. I was breaking my head on this for last two days. Thank You.

# April 24, 2008 4:27 PM

T said:

Thank you very much.

# May 12, 2008 10:06 PM

mahdokht said:

thanx u alot , it was great

# May 21, 2008 7:24 AM

eminently_addictive said:

yes <base target="_self" > works for submitting a request through a modal window without opening a new window.

But make sure you DO NOT HAVE any other property set like document.forms[0].target  = _self (I was doing it and it was not working)

Another thing i noticed is title disappears after the modal window refreshes !! someone mentioned it in the post but no one answered it ?? can anyone provide some inputs on it ??

# May 21, 2008 4:19 PM

eminently_addictive said:

To make sure the title does not disappear after a request is made from a modal window ... all you have to do is make sure the title is the first tag after ur head tag ..

<head>

<title>

ur title

</title>

<base target=_self>

.

.

</head>

Hope this helps anyone ... and thanks to all who posted their comments.

# May 21, 2008 4:32 PM

Deo said:

Hey thanks....

no matter what they said about your solution, it is the simplest and for me it's the best.

# June 6, 2008 11:51 AM

Joseph John said:

it worked for me. Great solution. I spent several hours and nothing. Thank you so much.

# June 16, 2008 6:14 PM

Norman said:

Thanks buddy..it worked for me

I was using .net 3.5 , C# asp.net

# June 25, 2008 6:41 AM

Harsh said:

Thanks a lot...

it was of a gr8 help 2 me...

# July 23, 2008 9:49 AM

Jolly said:

Thanks,

i help me lot

# August 7, 2008 8:55 AM

Navin said:

Hi,

Thanks a lot.

Navin

# August 18, 2008 9:40 AM

Steve said:

Anyone notice how the Modal window repositions itself to its original XY location after post back.

Does anyone have an easy fix other then read/write xy coords in a cookie on unload/load.

# August 19, 2008 2:12 PM

Steve said:

Figured it out. you need to set the window.dialogLeft and window.dialogTop on Unload to retain its position.

# August 20, 2008 9:24 AM

sandeepbhutani304 said:

I have got the same problem of postback with showModelessDialog. it opens a new window with url having JS code. Please provide help.

# August 21, 2008 8:11 AM

YoYo said:

The solution proposed by MatHertel (4th comment) work also great.

thanks

# August 26, 2008 7:29 AM

Rima said:

thanks a lot, it helped me.

# September 4, 2008 3:29 AM

Sunny said:

Thanx MatHertel ... you comments were like the words from an angel which solved my issue.

# September 8, 2008 11:27 AM

Soumya said:

Thank you...this really helped me.

# September 26, 2008 7:09 AM

Rajkumar said:

Lots of thanks man!!!

# October 6, 2008 8:06 AM

Sal said:

Thanks for this solution it has worked great

# October 14, 2008 9:26 AM

ajp said:

Hi

Base/self fix worked for me a treat - spent all day trying to stop my modal windows spawning new ones...

got so exasparated that i accidently ripped of the front of my desk draw when i got one pop up to many..

fix worked and am happy :)

now to fix my desk....

I was diagnosing the problem as a IE issue as firefox handled the javascript/html as i expected from the beginning :)

# November 6, 2008 12:17 PM

Rob B said:

Thank you soooo much!

# November 25, 2008 5:13 PM

Gino said:

Tnx dude,

You've saved me from allot of headache.

This solved my problem in 5 min.

# December 5, 2008 9:41 AM

rally25rs said:

Thanks! This worked for me, tested in Firefox and IE7, using ASP.NET Master Pages.

As a side note, the proper HTML would be:

<base target="_self"/>

Not that the browser would really care, but... :)

# December 5, 2008 9:58 AM

Sunil said:

After using <base target="_self"> ,still ther is same problem. Page going to be refreshed and open a new window?

Please give me some solution, if any ...Thanks

# December 15, 2008 7:24 AM

SRK said:

Thanks !!!

 Got the solution !!! Working for this since morning !!!

# January 2, 2009 5:59 AM

mike said:

Thanks DATAGRID Girl - I LOVE YOU :)

# January 5, 2009 2:28 PM

Lizzie said:

I read that ShowModalDialog would eventually be phased out of IE.  Are you all still using it?  I  tried to  use the Ajax Toolkit ModalPopup instead but it does not do what I need to do.

# January 26, 2009 1:32 PM

VinceB said:

make sure to also add the following in the aspx page

<%@ OutputCache Duration="1" VaryByParam="*"%>

or you will only hit the page_load on the first click

# February 2, 2009 2:16 PM

Mark said:

Genius -  saved me hours, thanks!

# February 12, 2009 4:25 AM

dongdong said:

I used <base target=_self>

It is useful

but if I want to popup a CSV in this model widows,It doesnt  work

who can tell me how should I do?

Thanks

# March 2, 2009 1:49 AM

Another solution said:

use iframe, submit form target to iframe

Ex:

<form method = "" action ="" target = "hiddenFrame">

</form>

.....

<iframe name="hiddenFrame" scrolling="no" frameborder="0" width="1" height="1">

</iframe>

# March 2, 2009 11:38 PM

Richard said:

thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks, thanks.....

# March 9, 2009 9:27 AM

Warren said:

What should one use in XHTML 1.1, where the base tag no longer has a target attribute?

# March 12, 2009 1:17 PM

Reden said:

Thanks, works.

For this to work

<head>

<base target="_self">

</head>

Property MaintainScrollPositionOnPostBack="True"

must be set at

the <%Page Language ....MaintainScroll....... %>

SmartNavigation is Obsolete.. But can still be effective.

Use MaintainScrollPositionOnPostBack instead.

# April 1, 2009 1:30 PM

Mubeen said:

thanks i saved a lot of time, it works

# April 9, 2009 5:28 AM

Mahesh said:

I need to open the new modal dialog from modal dialog which is already open. Above solution not worked for me.

If anyone have any other solution plz help me out .....

Thanks in advance

# May 8, 2009 7:58 AM

VJ said:

I need to implement modal windows with several form post backs. Initially, i had the problem that new windows were being spawned everytime a form post back was done inside the modal popup window. I added <base target="_self"/> to the modal windows to solve this problem but now I am getting script errors during certain postbacks in the modal windows.

If anyone has any solution to this, plz help me out,,,Thanks

# May 13, 2009 5:08 AM

Anuja said:

what if it is a master page?

# June 3, 2009 2:39 AM

Rajat said:

Thanks... <base target="_self" /> solved my problem... its very simple but unique.

# June 3, 2009 4:17 AM

Gaurav said:

Awsome!!!! Works like a charm!!! U Rock!!! :)

# June 3, 2009 2:21 PM

Avishay said:

Works like a charm, solved another headache.

# June 8, 2009 9:45 AM

Vİ said:

++AAAA

THANK YOU !

# June 22, 2009 4:12 AM

mahmoud said:

thnx very much , but now i have another problem

i can't find the opener object !!

can any 1 help ???

# June 24, 2009 8:52 AM

Swapnil said:

Hi <base target="_self" />  works great.

But still on post back, the contents of modal window (not the modal window) shifts a little from its original position both on horizontal and vertical axis.

I am a newbee to ASP.NET. Any explanations why this behaviour ?

# June 24, 2009 4:06 PM

Sunilwise said:

Thanks Alot Marcie,

it worked greatly for me..........

# July 29, 2009 1:37 AM

Lucas said:

Great discusstion, solve my problems

# August 22, 2009 10:35 PM

Harendra said:

grt i also spend 4 hours for new window problem and once again comes with title disappear problem... but this time thank god to this article....just take 5 minute...

# August 26, 2009 2:14 AM

X said:

Thanks,

  You save me. I think make load data from my aspx with JSON for resolve this problem.

# August 26, 2009 4:08 AM

Navin Patel said:

Hi,

If you should put <base target="_self" /> at the begining of head tag then, it works fine for all cases.

Navin

# September 24, 2009 5:35 PM

Christopher said:

Thanks...dude....solved my problem. Saved me lots of time. really appreciate it.

# September 25, 2009 5:16 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)