Kevin
Kevin

Reputation: 4848

Using Window.Open instead of Response.Redirect to open new window?

My page code looks like this:

<asp:Button ID="btnSearch" runat="server" Text="Search" onclick="btnSearch_Click"/>

My method looks like this:

protected void btnSearch_Click(object sender, EventArgs e)
    {
        var value = lblGraphicNameValue.Text.ToString();
        Response.Redirect("Search.aspx?txtGraphicName=" +
                           value);
    }

Currently, when the user press the 'Search' button the page refreshes and loads the Search.aspx page. What I'd like to happen is have the Search.aspx open in a new window, instead. I've looked at using Window.Open, but I'm not sure if this is the correct route, or if I can use the same method of passing in my variable (querystring). Can someone point me in the right direction? What I have works, I just want it to open in a new page while leaving the prior page alone.

EDIT: I should mention that I cannot use javascript (secure environment, every browser has javascript disabled).

From what I'm reading, it seems to indicate that opening a new web page from within an asp.net page and having parms passed in is not do-able without javascript? Is this correct?

Upvotes: 2

Views: 13922

Answers (4)

Waqas Ahmed
Waqas Ahmed

Reputation: 1

A better option would be to create a javascript function like:

    function PreviewPOSTransaction(Id)
    {

        if (Id != null)
        { 

            window.open('POSTransReport.aspx?TransID=' + Id);
            return true;
        }
    }

</script>

and call this function on button "OnClientClick" event like:

OnClientClick="PreviewPOSTransaction(1);

Upvotes: 0

Kevin
Kevin

Reputation: 4848

This code below ultimately does exactly what I needed it to:

<a href="<%= this.ResolveUrl("Search.aspx?id=" + lblGraphicNameValue.Text.Remove(lblGraphicNameValue.Text.Length -4)) %>"
                                                                                target="_blank">Search Related</a>

This code does three things:

  • 1) Opens Search in new page.
  • 2) Truncates the search value by four characters (I only needed part of the search string)
  • 3) Passes in parameter to new page.

This accomplished exactly what I needed without resorting to custom classes or javascript, although it did make me have to use a link instead of a button.

Upvotes: 1

Steven V
Steven V

Reputation: 16585

I think your on the right track, but you're confusing server side code, and client side code. window.open is a Javascript function which works on the client side. So you'll need to render some Javascript from C# to make the window popup. Try:

protected void btnSearch_Click(object sender, EventArgs e)
{
    var value = lblGraphicNameValue.Text.ToString();
    ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('Search.aspx?txtGraphicName={0}');</script>", value));
}

That will re-render the page, and then add a script on pageload that will popup the window. A little warning, this will probably be blocked by a browser popup blocker. If you want to get around that, you can probably achieve this without posting back to the server by using Javascript.

Upvotes: 0

Raghubar
Raghubar

Reputation: 2788

Use this class.

ResponseHelper .Redirect("popup.aspx", "_blank", "menubar=0,width=100,height=100");



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);
    }
}

}

Upvotes: 0

Related Questions