Friday, May 15, 2009

IE Open new window with focus using Update Panels

Every once in a while we need to open a new window from our main page and we want to make sure it has the focus so the user does not keep on clicking the same "open" button creating multiple pages in the background.

It sounds simple but if you are using "Update Panels" so avoid the annoying refresh look in your page and then you want to open a new window, this can be challenging. Instead here is a way to do it. I used a mix of javascript and c# code.

In my client javascript I defined a function that calls the window.open and pass the proper parameters to declare the page i would like to open.


//script language="javascript"//

function ReportDetailPopup(xCaseNumber) {
var w = window.open("ReportUIDetail.aspx?ExternalCaseNumber=" + xCaseNumber);
}

//script//


Now in my codebehind i call the "RegisterStartupScript" function to call my javascript function and then i set the focus on the Page recently called.

protected void grdResults_SelectedIndexChanged(object sender, EventArgs e)
{

string externalCaseNumber = "";
externalCaseNumber = grdResults.SelectedDataKey.Value.ToString();

ScriptManager.RegisterStartupScript(Page, Page.GetType(), "OpenNewWindow", "ReportDetailPopup('" + externalCaseNumber + "');", true);

Page.Focus();
}


I am certain that there are other ways to do it, but if you are entering the world of "update panels" you will find that many things don't work the way they should for obvious reasons. I will post more tricks to get the most out of update panels.

No comments:

Post a Comment