Header add

ASP.Net Response.Redirect or Server.Transfer: Open New Tab from Code Behind

In this article we will discuss ASP.Net Response.Redirect or Server.Transfer: Open New Tab from Code Behind. Before staring this article please go through our previous article of Difference Between Response.Redirect() and Server.Transfer() in ASP .NET.

To open a page in new Tab is a browser property and newer browsers will automatically open pages on new tab using this code but older browsers will still open it in new window unless the default settings are changed.




Set target property of FORM Tag

HTML FORM tag has target property which can be used to open a new window when the form is submitted to the server.

Keep on mind that as per W3C this method target is not a valid property in XHTML 1.1 though it works with all major browsers.

Let's Create an ASP .NET application will discuss in details.

File ⟹ New Project ⟹ Choose ASP .NET application ( We did the example in VS 2017)

ASP.Net Response.Redirect or Server.Transfer: Open New Tab from Code Behind
Fig-1 Select template as Empty

➥ After clicking OK button the Web Forms application is create for you. Then right click on the project then add a Web Form named as let's say "Default.aspx".

The following HTML Markup consists of a Form with 3 ASP.Net Button. The Button has been assigned with an OnClick event handler of Response.Redirect, Server.Transfer and another button we take for open in new tab and also an OnClientClick event handler.

When the 3rd Button is clicked, it first the SetTarget JavaScript function is called inside which the target property of the Form is set to _blank and after that the Server Side function is executed.

➥ Let's to move to code behind page and apply on the response.redirect and server.transfer.

You can see that we generate 3 button click event.

In 1st button we use Response.Redirect that redirects to another page Default1.aspx (We create another page).

In 2nd button we use Server.Transfer that redirects to another page Default1.aspx (We create another page).

In 3rd button we use the StringBuilder class, a JavaScript code is generated to open page in new Tab.
JavaScript window.open function to redirect to new Page in new Tab. Inside the Button Click event handler, using the StringBuilder class, a JavaScript code is generated to open page in new Tab.
Finally, the script is registered using ClientScript.RegisterStartupScript method.

Run the application and you can see the output like below;

ASP.Net Response.Redirect or Server.Transfer: Open New Tab from Code Behind
Fig-2
</> Find the Source Code in Github.com/CoreProgramm/





  
  Summary
 In this tutorial we discussed ASP.Net Response.Redirect or Server.Transfer: Open New Tab from Code Behind. If have any question related to this topic then give your feedback.


Post a Comment

Previous Post Next Post