Tuesday, July 3, 2012

How to open a new browser window from the code behind file - ASP.NET & C#

Sometimes we face this situation where we want to fill some values into some variables and we want to send those values to a web page where it will open in a new window. If you want to do all this in a single button click, there is something you should think about.

In javascript, there is window.open() method, which you  can use to open the new window in any way you prefer. But since that would be a client side event, it will run before the server side events. Which means if you write all the session variable assigning part in your server side click event which is,

it will fire after executing client side click event which is,
So as a solution to that, this is the code snippet to open a new window from the code behind file under the server side click event.
ClientScript.RegisterStartupScript(this.GetType(), "PopupWindow", "<script language='javascript'>window.open('YourPage.aspx','Title','width=600,height=300')</script>");
Happy Coding.