AJAX下不能使用RESPONSE.WRITE 脚本的解决方案

本文介绍在ASP.NET AJAX环境中如何使用ScriptManager.RegisterStartupScript()方法替代Response.Write()以实现弹窗等效果。通过示例代码展示了如何在按钮点击事件中打开新窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用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>
        
&nbsp; &nbsp;
    
</form>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值