[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 54 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
Leave a Comment

(required) 

(required) 

(optional)

(required)