AJAX下不能使用RESPONSE.WRITE 脚本的解决方案
2008-07-21 15:26
447 查看
在使用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>
使用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>
相关文章推荐
- AJAX下不能使用RESPONSE.WRITE 脚本的解决方案
- Ajax中使用Response.Write输出javascript脚本出错的解决方案
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- Ajax中使用Response.Write输出javascript脚本出错的解决办法
- 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题
- 使用ckeditor在Ajax 序列化表单提交时不能获取 textarea 的值 解决方案
- 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题
- 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题
- 在atlas(ajax)中请不要使用Response.Write,请使用ClientScript.RegisterClientScriptBlock替换
- 使用AJAX调用Spring controller 并返回map对象,@ResponseBody 只能返回string的解决方案:
- 文件下载不能使用ajax的替代解决方案
- 解决AJAX中使用UpdatePanel后再用Response.Write();等无法弹出对话框问题 3法
- 在使用atals时不能在后台使用respose.write的解决方案
- updatePanel中的button的click事件中不能使用response.write()
- 解决AJAX中使用UpdatePanel后再用Response.Write();无法弹出对话框问题