RegisterStartupScript + AJAX

When coding within ASP.NET (C#), there is no way to “open in a new window” for a page.  You can use Response.Write to “move” to another page, but can’t actually do it unless you use “client-side” JavaScript.

And so, you need to “inject” some JavaScript into the ASP.NET page, using the RegisterStartupScript command (see the following syntax). 

This is good for any server-side validation prior to opening the page :

    string sUrl = @"http:\\\";

    string openScript = "'{0}', '{1}', 'resizable=yes,width=1024,height=768');"

    Page.ClientScript.RegisterStartupScript(typeof(String), "New Window", string.Format(openScript, strURL, "ReportViewer"), true);

    This will open a new browser window – using – as an example.

BUT – I was finding that my page wasn’t being re-directed, and not sure why.   I was a bit susipcious of the “AJAX” (Atlas) updatepanel on the page, within the Master page. 

The whole “partial postback” of a page might explain why there is no “startup script” being fired.

And – sure enough – you need to use the “AJAX” ScriptManager instead – see below :

    string sUrl = @"http:\\\";

    string openScript = "'{0}', '{1}', 'resizable=yes,width=1024,height=768');"

    ScriptManager.RegisterStartupScript(this, typeof(String), "New Window", string.Format(openScript, strURL, "ReportViewer"), true);

The only code change is highlighted in RED – and now works for any script blocks within an AJAX UpdatePanel.

*damn ajax* 🙂


One thought on “RegisterStartupScript + AJAX

  1. thank you so much. I have solved my problem but i need to open multiple window in the same page content. Please help me. Thank.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s