2

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?

Kevin
  • 4,798
  • 19
  • 73
  • 120
  • 1
    possible duplicate of [Response.Redirect to new window](http://stackoverflow.com/questions/104601/response-redirect-to-new-window) – alergy Jul 16 '13 at 14:47
  • Is there no way to do this without using JavaScript? We work in a very secure environment with JS turned off. All of our software is developed in-house for in-house use. So, my conundrum is that I cannot use javascript. There's really no way to open a new web page with passed in parameters from within an asp.net page without JS? – Kevin Jul 16 '13 at 15:00
  • 2
    "Every browser has javascript disabled" -- Then how in the world are you using .NET? ASP:Button's REQUIRE Javascript to function. Are you sure JS is disabled, or do you simply have a constraint that you can't add custom JS not generated by the framework?? – Graham Jul 16 '13 at 15:12
  • Graham, that could be the case. We've been told not to use any javascript code. – Kevin Jul 16 '13 at 15:14

4 Answers4

1

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.

Kevin
  • 4,798
  • 19
  • 73
  • 120
0

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

}

Raghubar
  • 2,768
  • 1
  • 21
  • 31
0

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.

Steven V
  • 16,357
  • 3
  • 63
  • 76
  • As @alergy pointed out, there's a question already. The accepted answer will probably work to get around a popup blocker. – Steven V Jul 16 '13 at 14:53
  • I've looked at the thread that @alergy noted, but it uses javascript. I'm trying to stay away from that, if I can. Our site is secure and doesn't allow javascript to be enabled. – Kevin Jul 16 '13 at 15:05
  • @JavaRox Well, in that case, `window.open` won't work. You probably need to look into using the HTML attribute `target="_blank"`. But getting that implemented correctly is going to be a challenge with webforms because there is only one global `
    ` tag and can't be nested. http://stackoverflow.com/a/896731/254973
    – Steven V Jul 16 '13 at 15:11
  • Thanks, Steven. I flagged my question for deletion. I'll do a little more research and see what I can find out. I do thank you for your efforts in trying to help me! – Kevin Jul 16 '13 at 15:12
  • @JavaRox and for what it's worth, Webforms aren't going to be very pleasant to work with when Javascript is disabled. Webforms uses Javascript extensively to handle the postbacks using the `__doPostBack()` function. – Steven V Jul 16 '13 at 15:15
  • I may have mispoke, Steven. We aren't allowed to use javascript code, but it does appear to be enabled. We just can't use it in our code. – Kevin Jul 16 '13 at 15:17
  • Also, just out of curiosity, why can't I just open a new window using a simple statement, like what's in my code above? That code does exactly what I want, with the exception that it opens in the current window instead of a new one. Being new to asp.net and web programming in general, I don't know why this is so hard? Can someone explain? – Kevin Jul 16 '13 at 15:30
  • 3
    @JavaRox, the simplest way of explaining it is that code that runs on the web server is completely detached from your browser. The server simply takes in a request through IIS, executes your .NET code, and sends back an HTML response based on your Page class and markup. The server itself has no concept of popup windows, or 'new' windows, or tabs. Its simply `Request` => `Response`. Its up to the User Agent (the browser) to decide when/how to spawn popups, new windows, and the like. That's where JavaScript comes in. – Graham Jul 16 '13 at 20:52
  • 2
    I urge you to brush up on ASP.NET MVC, as it demonstrates this reality in a much more transparent way. – Graham Jul 16 '13 at 20:52
  • Graham, thanks so much for that information! I'm kind of learning asp.net in a trial by fire method. I was thrown into this, so I have a lot to learn. I do thank you for taking the time to explain this to me! – Kevin Jul 17 '13 at 14:36
0

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