在使用ASP.Net AJax 開發程序時候 我們以往經常使用 response.write();不能使用。
使用ScriptManager來實現JS注冊即可
以前以下代碼現在不能在AJax環境下正確運行

protected void Button1_Click(object sender, EventArgs e)


...{

Response.Write("<script>window.open("http://www.baidu.com/");</script>");

}
我們修改以上代碼 讓他在AJax下運行起來
這裡的ScriptManager.RegisterStartupScript()為靜態方法,注冊客戶端腳本。第一個參數UpdatePanel1 為JS要輸出到的UpdatePanel,opennewwindow 腳本關鍵字

protected void Button1_Click(object sender, EventArgs e)


...{

//Response.Write("<script>window.open("http://www.baidu.com/");</script>");

ScriptManager.RegisterStartupScript(UpdatePanel1, typeof(UpdatePanel), "opennewwindow", "window.open("http://www.baidu.com/");", true);

}
前台ASPX代碼

<form id="form1" runat="server">

<div>

<ASP:ScriptManager id="ScriptManager1" runat="server">

</ASP:ScriptManager>

</div>

<ASP:UpdatePanel id="UpdatePanel1" runat="server">

<contenttemplate>

<asp:Button id="Button1" runat="server" Text="Button" OnClick="Button1_Click"></ASP:Button>

</contenttemplate>

</ASP:UpdatePanel>


</form>