Click here to Skip to main content
12,349,554 members (28,834 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

105.4K views
16 bookmarked
Posted

Response.Redirect into a new window

, 23 Jan 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Response.Redirect into a new window
The only way to open a new window is for it to be initiated on the client side, whether it be through script or clicking on a link.

So the solution always proposed to this problem is to instead write out some script that opens the window, rather than using Response.Redirect:
<script type="text/javascript">
    window.open("foo.aspx");
</script>
Now the helper method which is used for redirecting to new page.

It's easy enough to write a little helper that abstracts the details away from us... while we're at it, we might as well add target and windowFeatures parameters. If we're going to open the new window with script, why not let you use all of the window.open parameters? For example, with windowFeatures you can specify whether the new window should have a menu bar, and what its width and height are.
    public static class ResponseHelper {
    public static void Redirect(string url, string target, string windowFeatures) {
        HttpContext context = HttpContext.Current;
 
        if ((String.IsNullOrEmpty(target) ||
            target.Equals("_self", StringComparison.OrdinalIgnoreCase)) &&
            String.IsNullOrEmpty(windowFeatures)) {
 
            context.Response.Redirect(url);
        }
        else {
            Page page = (Page)context.Handler;
            if (page == null) {
                throw new InvalidOperationException(
                    "Cannot redirect to new window outside Page context.");
            }
            url = page.ResolveClientUrl(url);
 
            string script;
            if (!String.IsNullOrEmpty(windowFeatures)) {
                script = @"window.open(""{0}"", ""{1}"", ""{2}"");";
            }
            else {
                script = @"window.open(""{0}"", ""{1}"");";
            }
 
            script = String.Format(script, url, target, windowFeatures);
            ScriptManager.RegisterStartupScript(page,
                typeof(Page),
                "Redirect",
                script,
                true);
        }
    }
}
Now you just call ResponseHelper.Redirect, and it figures out how to honor your wishes. If you don't specify a target or you specify the target to be "_self", then you must mean to redirect within the current window, so a regular Response.Redirect occurs. If you specify a different target, like "_blank", or if you specify window features, then you want to redirect to a new window, and we write out the appropriate script.
ResponseHelper.Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");
If there is any query for understanding this, then let me know.
Thank you.
@ChetanV@

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

hi
Currently I am working on Asp.net and sql Server 2005
just having 1+ year experianced!

You may also be interested in...

Comments and Discussions

 
QuestionThis is fantastic Pin
Isaac Samuel-BEJIDE29-Oct-15 23:18
memberIsaac Samuel-BEJIDE29-Oct-15 23:18 
BugError while adding query string in URL Pin
Member Nitin Patil9-Dec-14 17:47
memberMember Nitin Patil9-Dec-14 17:47 
QuestionFantastic Pin
Member 45862283-Dec-14 0:06
memberMember 45862283-Dec-14 0:06 
GeneralMy vote of 1 Pin
Selvin31-Jan-14 1:30
memberSelvin31-Jan-14 1:30 
QuestionError : Pin
EmadAldin Baybars29-Dec-13 3:15
memberEmadAldin Baybars29-Dec-13 3:15 
QuestionThis works really well Pin
TecNQ24-Oct-13 12:31
professionalTecNQ24-Oct-13 12:31 
GeneralMy vote of 5 Pin
pradeep655114-Nov-12 3:36
memberpradeep655114-Nov-12 3:36 
QuestionOpen in new Tab Pin
arunmisra15-Oct-12 14:09
memberarunmisra15-Oct-12 14:09 
Questionwindow.open starts a new session. Pin
Member 826662812-Jul-12 5:37
memberMember 826662812-Jul-12 5:37 
GeneralMy vote of 5 Pin
Member 71776624-May-12 5:00
memberMember 71776624-May-12 5:00 
GeneralMy vote of 5 Pin
Alaa S. Al Agamawi21-May-12 7:47
memberAlaa S. Al Agamawi21-May-12 7:47 
Questiongreat article, how to open windows in full screen without specifying the size? Pin
Vijay234516-Apr-12 11:49
memberVijay234516-Apr-12 11:49 
QuestionNew window set focus.......how? Pin
sconly13-Apr-12 0:26
membersconly13-Apr-12 0:26 
AnswerRe: New window set focus.......how? Pin
arunmisra15-Oct-12 14:06
memberarunmisra15-Oct-12 14:06 
GeneralIs there any way to do it that will not trigger the pop-up b... Pin
imanarchy30-Jan-12 12:11
memberimanarchy30-Jan-12 12:11 
QuestionRe: Is there any way to do it that will not trigger the pop-up b... Pin
Member 45862283-Dec-14 1:08
memberMember 45862283-Dec-14 1:08 
GeneralGood trick..but why were the alternatives removed..? I would... Pin
Abey Thomas21-Jan-12 1:36
memberAbey Thomas21-Jan-12 1:36 
GeneralRe: I think those alternatives(1,2 & 3) are spams that's why tho... Pin
thatraja21-Jan-12 17:52
mvpthatraja21-Jan-12 17:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 23 Jan 2012
Article Copyright 2012 by chetan virkar
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid