Ketone Cops

March 26, 2008

Ajax, an update panel and opening a new window

Filed under: ASP.NET 2.0, Ajax, UpdatePanel, programming — delroger @ 1:51 pm

Since this post is getting quite a few views, just thought I’d add a quick summary of this article if you don’t want to read it all. So here you go: if you want to open a new window from the code-behind page when your function or sub is being called from an AJAX Update Panel, you can do it like this (here your Update Panel is called UpdatePanel1 and you’re opening a PDF)…

ScriptManager.RegisterClientScriptBlock(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), "AnyScriptNameYouLike", "window.open('http://www.yourwebsite.com/YourFolder/YourDocumentOrWebpage.pdf');", True)

And here’s the rest of the original post…

I recently found a problem when using an Ajax Update Panel when I wanted to open a PDF document from the code-behind page.  Without the Ajax, I could simply have added a Response.Write with a javascript window.open function, or I could have used a Literal and written the javascript in much the same way, like this:

Response.Write("<script language='javascript'> window.open('http://website/folder/newdocument.pdf'); </script>")

or this (assuming a Literal control called ltlOpenPdf):

ltlOpenPdf.Text = "<script language=""javascript"">window.open('http://website/folder/newdocument.pdf');</script>"

However, the Response.Write is forbidden in Ajax and produces an error, while the Literal apparently did nothing at all.

Finally I worked out that I could open the new document by using a RegisterClientScriptBlock, most importantly passing the Update Panel as the first parameter where the Page is expected, and the Update Panel’s type as the second parameter, as you will see below.

The Update Panel is called UpdatePanel1. A new PDF has been created and saved to a folder called ‘pdffolder’ and the name of the PDF is dynamically created. The root of the website is also being added dynamically so that it will work equally on the development and the production server. (In reality, the name of the folder that contains the PDFs is also dynamically set, captured from the web.config file using System.Configuration.ConfigurationManager.AppSettings)

Dim docGuid As String = Guid.NewGuid().ToString()
'Do pdf creation stuff here...
Dim sb As StringBuilder = New StringBuilder("")
Dim strRoot As String
strRoot = Request.Url.GetLeftPart(UriPartial.Authority)
sb.Append("window.open('" + strRoot + "/pdffolder/" + docGuid + ".pdf" + "');")
ScriptManager.RegisterClientScriptBlock(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), "NewClientScript", sb.ToString(), True)

The only thing to watch out for with this is that it may be classified as a popup by some blockers and prevented from opening, but then that’s almost inevitable with a window created in code.

8 Comments »

  1. Thank you very much it helpedme a lot

    Comment by kemal — March 3, 2009 @ 7:09 am

  2. Thank you for this information. Came in very handy.

    Comment by Helene — March 19, 2009 @ 8:45 pm

  3. Great job man, you helped a lot of people with your article. It’s also nice (and unfortunatelly too rare) to see people that actually know what they are talking about.

    Comment by Cosmo — August 11, 2009 @ 1:56 pm

  4. thank you! helpful article, written clear and simple!

    Comment by Zax — September 18, 2009 @ 1:28 pm

  5. But what if we need to pass parameter while actual execution of code, e.g. while selecting a specific row in GridView i want to pass the value of first cell to window.opener.control object in SelectionChanged Event, the code given here work for static parameter while executing specific code only.

    Comment by Vimal Upadhyay — October 12, 2009 @ 10:56 am

  6. following is code
    ———–
    Protected Sub grdCust_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdCust.SelectedIndexChanged
    Response.Write(“window.opener.document.getElementById(‘” & hdnCustTextBox.Value & “‘).value=’” & grdCust.SelectedRow.Cells(2).Text & “‘;”)
    Response.Write(“window.opener.document.getElementById(‘” & hdnCustBtn.Value & “‘).click();”)
    Response.Write(“window.close()”)
    End Sub

    Comment by Vimal Upadhyay — October 12, 2009 @ 11:01 am

  7. mannn i was searching 4 this since last 2 daYS.
    THANKS A TON…!!!

    Comment by alok — November 2, 2009 @ 10:15 am

  8. Thank you very much! This helped me a lot!! I love how you wrote this, straight to the solution
    Greetings from Mexico! =D

    Comment by Hal Emmerich — November 3, 2009 @ 6:40 pm


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.