Sunday, October 5, 2008

AJAX: Opening a new web page/document using UpdatePanel

Recently, I'm experimenting with AJAX technology to be used in my web application project.
To get more details on AJAX, you can refer to AJAX main website http://www.asp.net/ajax/.

I'm trying to open up a new window when I click on a Button, not the Hyperlink.
Previously, in ASP.Net, I can just use a Response.Write with a javascript window.open function to do that.

Response.Write("<script>window.open('NewPage.aspx');</script>");

However, with AJAX and it's UpdatePanel, I'm getting an error when I pump in this statement in the code behind.

So, with the help of Google and some useful website link, I found this page, http://delroger.wordpress.com/2008/03/26/ajax-an-update-panel-and-opening-a-new-window/ which solves my problem.

To open up a new page/document, you can use RegisterClientScriptBlock function passing the UpdatePanel as the first parameter, the UpdatePanel's type as the second parameter, and so on.

In the example below, str is a variable that contains the javascript statement to open up the Default.aspx page. Then followed by ScriptManager.RegisterClientScriptBlock function. The NewClientScript name is just a name and you can name it anything you like.

string str;
str = "window.open('Default.aspx');";
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, UpdatePanel1.GetType(), "NewClientScript", str, true);


... continue exploring AJAX ...

No comments: