Ketone Cops

March 26, 2008

Ajax, an update panel and opening a new window

Filed under: Ajax,ASP.NET 2.0,programming,UpdatePanel — 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", "'');", 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 function, or I could have used a Literal and written the javascript in much the same way, like this:

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

or this (assuming a Literal control called ltlOpenPdf):

ltlOpenPdf.Text = "<script language=""javascript"">'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("'" + 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.



  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();”)
    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

  9. You are wonderful….this saved me a lot of grief!

    Comment by Craig Slomczewski — November 12, 2009 @ 6:55 pm

  10. Thks so much. you are great!

    Comment by oscar andres — February 17, 2010 @ 3:04 pm

  11. Yu, saved my day! 😀

    Comment by Ivuso — March 2, 2010 @ 9:50 am

  12. Very clever! It pays to know how things really work and not just spend your time trying to make somthing happen.

    Thanks a lot!

    Comment by Mike J — May 3, 2010 @ 10:03 pm

  13. Thank you so much it helped me a lot:)

    Comment by Jake — September 16, 2010 @ 5:35 pm

  14. I have been searching for that for ages. Thank you very much!

    Comment by Shime — October 18, 2010 @ 9:04 am

  15. Awesome! I had 3 aspx pages which I combined using an Ajax tabpanel and poof… the pdf open links didn’t work anymore. Your post resolve it! Great job!

    Comment by Regin — April 28, 2011 @ 2:20 pm

  16. Thank You! Thank You! I have been trying to figure this out for a couple of hours now. I found a lot of articles that demonstrated this technique but left out one MAJOR detail. Using the UpdatePanel as the first paramter to RegisterClientScriptBlock. That’s why mine wasn’t working. Thanks for your attention to detail!

    Comment by Mike — May 3, 2011 @ 7:45 pm

  17. Thanks ,
    Dim sb As StringBuilder = New StringBuilder(“”)
    Dim strRoot As String
    strRoot = Request.Url.GetLeftPart(UriPartial.Authority)
    ScriptManager.RegisterClientScriptBlock(Me.UpdatePanel1, Me.UpdatePanel1.GetType(), “NewClientScript”, sb.ToString(), True)

    Comment by Muneer — December 5, 2011 @ 4:03 am

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at

%d bloggers like this: