• Home /
  • Blog /
  • Open new window from code behind in ASP.NET
Open New Window From Code Behind In ASP.NET

Open new window from code behind in ASP.NET

In the application I am working on, I have a task: open a new window from code behind code when the user clicks in the calendar.

This is a straightforward task and can be done with target="_blank" property of the link. But the calendar is server control, so if the user clicks on some date, there is a postback to server and page refreshed. Postbacks are not very good for user experience, and it was a lot of complaints in the previous version of the application because users can't use the back button and can't use the refresh button...

But I found an easy solution how to open a new window from code behind code and avoid problems with a postback.

Using update panel to open a new window from code-behind in asp.net

If I put the calendar in the update panel, I still can use server events, but it will be no page refresh for the user.

The other problem is how to open a new window from ajax request? But it can be done using JavaScript and ScriptManager.RegisterClientScriptBlock method. This method renders JavaScript code after ajax request finished.

Source code: How to open a new window from the code-behind file

I use user control to wrap calendar code and put this control inside of UpdatePanel:

And this is code for opening new window:

To open a new window using JavaScript, you can use the function window.open(), below is the source code:

UpdatePanel required ScriptManager, so do not forget to include it after the form tag.

Yaplex - Freelance developer - white logo on black background

Yaplex ® is a custom software development company located in Mississauga, Ontario. Yaplex was founded in 2013 and provides website development, mobile development, and custom .NET software development services.

Contact Information

Mississauga, ON, Canada
Phone: +1 647 328 3809
Email: hello@yaplex.com