I have a generic function in one of my helper libraries that allows me to open a new browser window when a button is selected.
- April 20, 2015
I have a generic function in one of my helper libraries that allows me to open a new browser window when a button is selected. The button posts back to the server runs some code and opens a new window. I often use this sort of thing on a reporting page allowing me to post some search criteria and open a new window (a pdf report).
I found this type of helper function useful however I found that I was losing my CSS after the postback.
To avoid losing the css keep the following in mind:
ensure that your css files are included declarative in your aspx page (not through code behind)
use Page.ClientScript.RegisterStartupScript(this.GetType(),”name”,script); (Not Response.Write)
The end helper function looks something like the following:
/// <summary>
/// redirect to a new page from codebehind
/// </summary>
static public void RedirectToNewWindow(string url, System.Web.UI.Page page) {
string script = "<script>window.open('" + url + "');</script>";
page.ClientScript.RegisterStartupScript(page.GetType(), "redirectscript",script);
}