views:

3782

answers:

4

I have a main page that I want to be a sort of dispatch center for the user in my web application. So I want the main page the stay open, but when the user selects certain tasks I can set it up so that a new tab or page is created with the task that they selected.

I know how to transfer control to another page, but how can I do something like a Server.Transfer only make that new page appear in a new tab and keep the existing page open in the old tab/window?

To give an example of what I'm doing. I currently have a repeater on my main page which constructs a list of LinkButtons. The LinkButtons have text from the data source. Once the user clicks on a link button, I want to open a new page and I want Session state to be preserved with this new page.

Can this be done, and how?

I'm using the version of ASP.NET in .NET 3.5.

Edit: I don't care whether its a tab or window. I just want to be able to spawn a new web page and keep the existing one open.

A: 

I'm almost positive this is not possible. Most tabbed browsers have a setting that you can tweak that tells the browser where it should launch new pages - in a brand new window or in a new tab.

Given the fact that this is under the user's control I don't think there is currently a way to get this to happen in a browser-independent fashion. Although I would not be surprised if the object model exposed by IE's DOM extensions allows you to do it.

mjmarsh
A: 

Its not possibe, wether new pages open in a tab, or a new page is controlled by the users settings in the browser, not from the code.

Sam Cogan
+2  A: 

You can't change UI behavior server-side (for example, by using Server.Transfer or Response.Redirect) - it just doesn't work that way. So, there are two ways you can go about this:

  1. If you know ahead of time which specific tasks require opening a new window/tab, set the target of anchor or form tags appropriately (or use javascript's window.open() function).
  2. If the decision to open a new window/tab needs to be made server side, you can generate and return javascript that does what you need.

Keep in mind that new tab/window behavior is completely dependent on the browser and the user's settings, so there's no way to guarantee which will happen - some settings can even force all links to be opened in the same window, regardless of the target or calling window.open().

If you are using a cookie-based session, which is the default functionality, even in a new window or tab, the user will still be using the same session, so you shouldn't have to do anything special to preserve the session state.

If you are using a url-based (or cookieless) session key, you will need to make sure you format the url appropriately using the key when opening new windows with javascript. I'm not positive, but I believe simply that using the target attribute and relative urls should automatically generate the appropriate url.

Daniel Schaffer
A: 

This is what I did.

I needed to add a Session state variable based on what selection they made, and in Firefox, it results in a new tab which is nice:

In the dispatch page, I just use Server.Transfer to go to the new page (after adding the session state), however, on the new page, I run an onload script. Here is my code:

In the Item Command Event Handler for the dispatch page:

protected void RecentButtonsRepeater_ItemCommand(object source,
    RepeaterCommandEventArgs e)
{
    string code = ((LinkButton)e.Item.Controls[1]).Text;
    string sql = "SELECT RunID FROM ProductionRuns WHERE RunCode = @code";
    SqlConnection sqlconn = new SqlConnection(QCDataPath);
    sqlconn.Open();
    SqlCommand sqlcomm = new SqlCommand(sql, sqlconn);
    sqlcomm.Parameters.AddWithValue("@code", code);
    SqlDataReader sdr = sqlcomm.ExecuteReader();

    sdr.Read();

    int id = sdr.GetInt32(0);

    sdr.Close();
    sqlconn.Close();

    Session["RunID"] = id;
    Server.Transfer("Sheet.aspx");
}

And in the Sheet.aspx markup:

<script type="text/javascript">
    function ReOpenScoreHome() {
        window.open("Scoresheets.aspx", "reopenwindow");
    }
</script>
</head>
<body onload="ReOpenScoreHome()">
Tony Peterson