[karsten samaschke]

ASP.NET daily. Or weekly.

How to do a Response.Redirect to another frame

One of the most often asked questions is:

Is it possible to do a Response.Redirect to another frame or a new window?

There is just one answer to this question: No, it isn't. Nope. No chance. No way. Never. Forget it.

Why isn't this possible?

It is not possible because of frames and windows are client-side concepts. The server just don't knows about these concepts. And if it does not know about it - how should it be able to address a different window or frame?

The workaround

The only possible workaround is the usage of JavaScript. Simply register a client-side script doing the desired redirect by using window.open or <parentframe>.location.href. Or use targets in your forms.

Posted: Feb 23 2003, 11:40 PM by xxxkarsan3020 | with 77 comment(s)
Filed under:

Comments

Winanjaya said:

just put below codes into page load event

cmdPrint.Attributes.Add("OnClick", "javascript:window.open('yournewpage.aspx')")

GOOD LUCK
# June 24, 2003 3:50 AM

KrIstOfK said:

is there no way for a datagrid with 3 button colums to redirect by clicking only one of them to a new window?
# September 25, 2003 3:49 AM

DF said:

FDA
# November 12, 2003 2:37 AM

w said:

ww
# December 8, 2003 11:49 AM

Ahmed Abobakr said:

Just Do It By java script


Response.Write("<script>window.open("PageName.aspx","FrameName")</script>")



# January 3, 2004 4:58 AM

Ahmed Abobakr said:

oh sorry



Response.Write("<script>window.open("PageName.aspx","FrameName");</script>")



// i forget the semicolon
# January 3, 2004 4:59 AM

ddfd said:

dfdf
# January 10, 2004 4:53 AM

Brad said:

Forgets perfect. Thanks!
# January 13, 2004 8:41 AM

Brad said:

Works perfect I should say (brain crash). Thanks.
# January 13, 2004 8:41 AM

Bill said:

Ahmed, your solution rocks! Simple and entirely functional. Thanks.
# January 23, 2004 1:31 PM

4567 said:

34789
# February 10, 2004 4:43 AM

xsdfgh said:

rhjk
# February 10, 2004 4:44 AM

Us3r said:

Ahmeds dont workt at me.
This work

Response.Write("<script>window.open('PageName.aspx','FrameName');</script>")

thx ahmed
# February 17, 2004 6:52 PM

jules said:

You can also do this :
specify the target frame in your form tag like this : <form name=myForm target=myOtherFrame runat=server>...a button goes here....</form>

in the button fired event code, add this line :
response.redirect("myPage.aspx")

myPage.aspx will be loaded in the frame myFrame !
# February 19, 2004 10:39 AM

joep said:

Make a class called frames:

===============================================
Public Class Frames

Public Enum Frame
Main = 0
Menu = 1
Window = 2
End Enum

Public Shared Function Reload(ByVal Frame As Frame) As String
If Frame = Frame.Window Then
Return "<script language='javascript'>top.location.reload();</script>"
Else
Return "<script language='javascript'>top.frames[" & Frame & "].location.reload();</script>"
End If
End Function

Public Shared Function Load(ByVal Frame As Frame, ByVal Href As String) As String
If Frame = Frame.Window Then
Return "<script language='javascript'>top.location = '" & Href & "';</script>"
Else
Return "<script language='javascript'>top.frames[" & Frame & "].location = '" & Href & "';</script>"
End If
End Function

End Class
===============================================

If you want to reload, or load another page in a frame you just use:

response.write(Frames.reload(Frames.frame.main))

for example
# March 11, 2004 3:19 AM

asd said:

asd
# March 15, 2004 3:14 PM

asdasd said:

asdasd
# March 19, 2004 12:05 AM

slam said:

This is my line of code:

Response.Write("<script>window.open(\"_FooBar.aspx?Foo=1&Bar=2\",\"main\");</script>");

The solution posted by Ahmed works for redirecting to a .asp page. With exactly the same page and just change the extension to .aspx, I got back the error:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1002: ; expected

Source Error:
Line 1: <%
Line 2: Response.Write(Request.QueryString("Foo") & "<br />")
Line 3: Response.Write(Request.QueryString("Bar") & "<br />")
Line 4: %>

Does anyone has the clue?

Thanks
-slam
# March 20, 2004 11:28 AM

slam said:


I added a ; to the end of the line to fix the previous error, and another error comes back:

Compiler Error Message: CS0118: 'System.Web.HttpRequest.QueryString' denotes a 'property' where a 'method' was expected
# March 20, 2004 11:31 AM

Kartik Gajjar said:


Try following

Sub Signout_Click()
FormsAuthentication.SignOut()
Response.Write("<script>window.open('login.aspx','_top');<" & chr(47) & "script>")
End Sub
# March 26, 2004 7:02 AM

Dennis said:

Kartik Rules.
# March 31, 2004 7:51 PM

Focus said:

Ahmed solution works great except that my Google Toolbar stop it as a pop up. I'm just a noob in the asp.net world, is there another way?
# April 7, 2004 2:45 AM

Furball said:

I have a main.asp which contains an iFrame. From main.asp, I log in and the form is directed to login.asp for verification. After the login and password is verified correct, the user is supposed to remain on main.asp but the iFrame is supposed to load content.asp. How do I do a redirect from login.asp to load content.asp into the iFrame on the main.asp? Still a newbie. Haven't even touched .Net. Thanks!
# April 11, 2004 9:21 AM

howard said:

I have a function that I would like to open a page on a different form (main).
I have tryed to run the code ubove but I keep getting errors.
Here is what I have:
void MYSelectedChangeEventHandlerFunction(DropDownList DD)
{
string address = DD.SelectedItem.Value;
//Response.Redirect("\\main.htm");

Response.Write(" <script> window.open(address,"main"); </script> ")
}
I get an error when I try to compile:
CS1010: Newline in constant
Thanks,
# April 14, 2004 7:07 PM

Neil said:

Howard - You need single quotes for your 2nd parameter and a semi colon:

Response.Write(" <script> window.open(address,'main'); </script> ");
# April 15, 2004 6:31 AM

Neil said:

The above code won't work when SmartNavigation is on.

Also, I get runtime error "CS1009: Unrecognized escape sequence" on the destination page (I am calling a Crystal Report).
# April 15, 2004 6:48 AM

ML said:

<script language=javascript>
window.open("index.asp");
parent.location.href("../main.html");
</script>
# May 6, 2004 3:57 AM

ML said:

<script language=javascript>
window.open("index.asp?no=<%=no%>");
parent.location.href("../main.asp");
</script>
# May 6, 2004 3:58 AM

dt said:

Kartik's code works perfectly. Thanks man!
# May 6, 2004 4:48 AM

Fiona Lee said:

Is there a problem with this line?

I keep on getting the error: Newline in constant.

WebChartViewer.ImageMap = c.getHTMLImageMap("<script language='javascript'>window.open('LTFO_DG.aspx','_ExcelAll','height=500,width=600,resizable=Yes,scrollbars=Yes,menubar=Yes');</script>","","title='{value}% for Lead Time: {xLabel} '");

Thanks.
# June 10, 2004 10:25 PM

hs said:

hey ahmed..
url solution is just very smart..
good job
thanx
# June 15, 2004 6:24 AM

Fabricio Leon - CuencaEcuador said:

Response.Write("<script>window.parent.frames[2].location='http://lion/page.aspx?';</script>")

Esto es mas facil
This is very easy

frame[1] or frame[2] etc.. depend of place for present your page
# June 17, 2004 9:41 PM

Tony said:

THANK YOU ALL SO MUCH!  Ahmed Abobakr, you just saved my life.  And my job.

# July 27, 2007 1:29 PM

Hanaa said:

Guys n Girls,

You don't know how much reading your posts helped me!

Many Thanks.

# August 16, 2007 2:06 AM

daza said:

thanks you have help alot

# August 30, 2007 11:20 AM

vishal said:

Thank you so much. i getting more knowledge from this sites

# October 5, 2007 2:55 AM

lai said:

thank you Ahmed Abobakr and thanks this site owner.

# November 23, 2007 4:16 AM

Jean-luc said:

Another option for this would be to set the iFrame runat attribute to server. I.e.:

<iframe id="iFrame1" runat=server>        

</iframe>

Then in the code behind where you want to do the redirect, set the src attribute to the location you wish to redirect to, i.e.:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

iFrame1.Attributes("src") = "PagetoshowinIFRAME.aspx"

End Sub

Simple enough?

# December 19, 2007 12:06 PM

Memo said:

This works Beautiful. Thanks to Us3r and Ahmeds!!!!

# December 26, 2007 4:05 PM

davebauer said:

I dropped the idea of using frames or iframes altogether, and used one page with a table. This eliminated my challenges. I think sometimes we get stuck on trying to do something a certain way, and when we step back and look at the bigger picture,  a simple solution presents itself.

# January 6, 2008 6:00 AM

thegman said:

Kartik's code was spot on for me like and a great relief after tinkering with the page for 4 bloody hours!

Cheers K

G

# January 29, 2008 10:00 AM

Caty said:

Thank u wery much Jean-luc!

it is perfect!

thanks again!

# February 6, 2008 5:35 AM

KM Websol said:

check it out at KM Websol blog

www.webdatadesign.com.au/pt

# February 25, 2008 8:07 AM

satinder said:

jules thanks, you code worked out perfectly, i tried with your ahmed, unfortunately it didnt worked out for.thanks a lot guys!!:-))

# March 13, 2008 4:07 PM

g said:

Jules you are a life saver!

# May 21, 2008 5:06 AM

Vlad said:

Thank you  Ahmed, your solution works wonderfully.

# June 10, 2008 12:26 PM

Salman said:

Thanks Ahmed. Your solution worked

# June 23, 2008 5:08 PM

Eric said:

Ahmed, my man, the world needs more of your kind of solutions  - simple, elegant, and not unimportant:    W O R K I N  G!!.

Awesome, thanks a million, bro!

# August 30, 2008 12:53 PM

Pankaj said:

Great Job Man !!!

Salute !!

# December 30, 2008 7:26 AM

Fahad said:

Ahmed Abobakr

Thanks Alot....

# March 28, 2009 10:05 AM

nigger said:

Ahmed you my man !

# June 9, 2009 12:42 PM

REE said:

Thanks! You're postings helped me a lot of time being stucked in a very simple problem.

# July 9, 2009 2:09 PM

thanker man said:

Ahmed!! easy code and works

THANK!!

# August 9, 2009 2:29 PM

Alex76 said:

Promoting better mental wellness  for  women  is  important  to  everyone. ,

# October 22, 2009 9:36 PM

Charlie said:

for the best way to do this go to :

dotnetchris.wordpress.com/.../c-aspnet-responseredirect-open-into-new-window

well worth a look :)

# February 23, 2010 5:49 AM

msameer71 said:

i am not using frame in my application.

how can i response.redirect to another contentplaceholder

# March 23, 2010 5:23 AM

Ali said:

Ahmed,

You rock man. it works. Thanks

# April 14, 2010 2:04 PM

Doctor said:

create a name for the frame, ie name="framename"

in your form tag add:

target="framename"

simples

;)

# April 30, 2010 7:46 PM

Adeel Fakhar said:

Nice job. I got the same problem and i solved it after getting the solution provided in nice-tutorials.blogspot.com/.../redirection-from-iframe-in-aspnet.html "Redirection from IFrame in asp.net" . It is quite similiar with what Ahmed did.

# May 15, 2010 4:31 AM

Pradeep Deokar said:

Hey Friend , Great Job Bosss!!!!!!!!!! cheers Thanks A Lot Dude, u save my time bro

# May 19, 2010 3:31 AM

BlogSearcher said:

Nice article you got here. It would be great to read more about this theme. The only thing it would also be great to see on this blog is a few pics of any devices.

John Stepman

<a href="www.jammer-store.com/">cel phone jammer</a>

# July 7, 2010 11:48 PM

India escorts service said:

It is rather interesting for me to read that post. Thanx for it. I like such themes and anything connected to them. I definitely want to read a bit more on this blog soon.

Alex Swingfield

# July 18, 2010 4:45 AM

executive escorts said:

Pretty cool domain to track it in my view. The only question I have, why haven't you you place it to social media? That can bring much traffic to this page.

# July 22, 2010 3:55 PM

London escorts spanish said:

I definitely want to read a bit more on that site soon. BTW, rather good design you have here, but don’t you think it should be changed once in a few months?

Charlotte Funweather

# July 26, 2010 9:24 PM

brunette lady said:

Pretty cool site you've got here. Thanks for it. I like such themes and everything that is connected to this matter. I definitely want to read a bit more soon.

Julia Benedict

# July 28, 2010 4:36 PM

John Karver said:

It is rather interesting for me to read that post. Thanks for it. I like such themes and everything connected to them. I definitely want to read a bit more on this blog soon. BTW, pretty good design you have here, but how about changing it once in a few months?

John Karver

<a href="http://www.baccaratgirls.com">London escorts female</a>

# August 2, 2010 7:16 PM

escort beauty lugano said:

Keep on posting such themes. I like to read articles like that. BTW add more pics :)

# September 1, 2010 1:18 PM

sam said:

thanks

...For valuable in formation

# September 10, 2010 6:26 AM

puls said:

ahmed solution has problem with popup and

jules solution has problem when you have some buttons where you don't want to open a new window

# October 31, 2010 3:51 AM

Kate Benedict said:

Rather interesting blog you've got here. Thanx for it. I like such topics and anything that is connected to this matter. I would like to read a bit more soon.

Kate Benedict    

<a href="milanescorts.com/">escort milano add new link</a>

# March 8, 2011 2:30 AM

nakikiraan said:

waaaaaaaa..... ahmed's code didn't work on me...

# March 25, 2011 5:44 AM

Julia Kuree said:

It is extremely interesting for me to read the article. Thanks for it. I like such topics and everything connected to this matter. I definitely want to read more soon.  

Julia Kuree    

<a href="www.pickescort.com/">reviews escorts</a>

# April 19, 2011 1:24 PM

adel312 said:

''' <summary>

''' Redirecciona la pagina en una ventana nueva y no machaca la actual

''' </summary>

''' <param name="url"></param>

''' <param name="target">Para indicar si es nueva o la misma pagina _self o _blank</param>

''' <param name="windowFeatures"></param>

''' <remarks></remarks>

Public Shared Sub Redirect(ByVal url As String, ByVal target As String, ByVal windowFeatures As String)

Dim context As HttpContext = HttpContext.Current

If ([String].IsNullOrEmpty(target) OrElse target.Equals("_self", StringComparison.OrdinalIgnoreCase)) AndAlso [String].IsNullOrEmpty(windowFeatures) Then

context.Response.Redirect(url)

Else

Dim page As Page = DirectCast(context.Handler, Page)

If page Is Nothing Then

Throw New InvalidOperationException("No se puede redireccionar esta página.")

End If

url = page.ResolveClientUrl(url)

Dim script As String

If Not [String].IsNullOrEmpty(windowFeatures) Then

script = "window.open(""{0}"", ""{1}"", ""{2}"");"

Else

script = "window.open(""{0}"", ""{1}"");"

End If

script = [String].Format(script, url, target, windowFeatures)

ScriptManager.RegisterStartupScript(page, GetType(Page), "Redirect", script, True)

End If

End Sub

# May 17, 2011 6:30 AM

Arunviswam said:

Hi, I have a webpage which has 3frames and i have a button in the third frame. i need to click that button using C#. I have tried but i am not able to click the button. Please assist. Drop a mail to winviswam@yahoo.co.in

Thanks.

# July 31, 2011 2:01 PM

Alen said:

but some times it won't be work in chrome and mozilla

# September 23, 2011 5:58 AM

Refresh page with framesets on button click - Programmers Goodies said:

Pingback from  Refresh page with framesets on button click - Programmers Goodies

# November 15, 2011 12:10 PM

FlilIaGrailky said:

Конечно. Я согласен со всем выше сказанным. Можем пообщаться на эту тему. Здесь или в PM.            

Пока :-)    

megasto.com.ua/vanny-dlja-shinomontazha Ванны для шиномонтажа

# December 20, 2011 1:07 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)