• Home /
  • Blog /
  • Open new window from code behind in ASP.NET
one-new-popup-asp-net.png

Open new window from code behind in ASP.NET

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

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

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

Using update panel to open new window from codebehind in asp.net

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

The other problem is how to open 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 new window from codebehind file

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

And this is code behind for opening new window:

To open new window using JavaScript you can use function window.open, this is source code:

Tips how to open new window from codebehind

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

Get latest updates directly into your inbox


Comments

Yaplex logo white text

Yaplex is an IT Consulting and .NET development company co-founded by .NET Developer Alex Shapovalov who lives in Mississauga and provides software development services in Toronto, Mississauga, and GTA.

Contact Information

Toronto, Canada
Phone: +1 647 328 3809