Create a new tab or window for a web page from ASP.NET

I have a main page in which I want to be a kind of dispatch center for the user in my web application. Therefore, I want the main page to remain open, but when the user selects certain tasks, I can configure it so that a new tab or page is created with the task selected by him.

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

To give an example of what I am doing. I currently have a relay on my homepage that builds a list of LinkButtons. LinkButtons has text from a data source. When the user clicks the link button, I want to open a new page, and I want the session state to be saved on this new page.

Can this be done and how?

I am using ASP.NET version in .NET 3.5.

Edit: I don't care if there is its tab or window. I just want to create a new web page and save the existing one.

+1
source share
4 answers

You cannot change server-side user interface behavior (for example, using Server.Transfer or Response.Redirect) - it just doesn't work. So, you can do two ways:

  • If you know in advance what specific tasks require opening a new window / tab, set the target anchor point or form tags accordingly (or use the javascript window.open () function).
  • If the decision to open a new window / tab should be made on the server side, you can generate and return javascript that will do what you need.

Keep in mind that the new behavior of the tab / window is completely dependent on the browser and user settings, so there is no way to guarantee that this will happen - some settings may even make all links open in the same window, regardless of whether the target or the calling window.open ( )

If you use a cookie-based session, which is the default functionality, even in a new window or tab, the user will still use the same session, so you don’t have to do anything special to save the session state.

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

+3
source

This is what I did.

I needed to add a session state variable based on what they did, and in Firefox this gives a new tab, which is nice:

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

In the event handler of the item command for the submit 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 layout of Sheet.aspx:

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

I am pretty sure that this is impossible. Most tabbed browsers have a setting that you can configure that tells the browser that it should launch new pages - in a new window or in a new tab.

Given the fact that this is under user control, I don’t think there is currently a way to achieve this, regardless of browser. Although I would not be surprised if the object model opened by the IE DOM extensions allows you to do this.

0
source

It is not possible for new pages to open on a tab, or a new page is controlled by user settings in the browser, not by code.

0
source

All Articles