在使用ASP.NET AJAX 開發(fā)程序時(shí)候 我們以往經(jīng)常使用 response.write();不能使用。
使用ScriptManager來實(shí)現(xiàn)JS注冊(cè)即可
以前以下代碼現(xiàn)在不能在AJAX環(huán)境下正確運(yùn)行
protected void Button1_Click(object sender, EventArgs e)
...{
Response.Write("<script>window.open("http://www.baidu.com/");</script>");
}
我們修改以上代碼 讓他在AJAX下運(yùn)行起來
這里的ScriptManager.RegisterStartupScript()為靜態(tài)方法,注冊(cè)客戶端腳本。第一個(gè)參數(shù)UpdatePanel1 為JS要輸出到的UpdatePanel,opennewwindow 腳本關(guān)鍵字
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);
}
前臺(tái)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>
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)
點(diǎn)擊舉報(bào)。