ScriptManager.RegisterStartupScript()方法在ajax页面无效

如果不用Ajax,在CS文件中运行某段js代码是可以的:

Page.ClientScriptRegisterStarupScript(Page.GetType(),"","<script>window.open('default.aspx')</script>");

如果页面中使用了ajax,则上述代码执行无效。应对这种情况,通常采用:

ScriptManager.RegisterStartupScript(this.Button1,this.GetType(),"alertScript","window.open('default.aspx');",rue);

其中,第一个参数为要注册脚本的控件ID.

第二个参数为,注册脚本控件的类型,是控件还是this.GetType()都可以。typeof(string)也没问题。

第三个参数为,脚本函数的名字,随便起。

第四个参数为,脚本内容。

第五个参数为,是否添加脚本标签,如果第四个参数里面包含了<script></script>标签,此处为false,否则为ture.

aspx代码如下:

<div>

       <asp:UpdatePanel ID="UpdatePanel1" runat="server">

       <ContentTemplate>

           <asp:TextBox runate="server" ID="TextBox1">

           </asp:TextBox>

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

        </ContentTemplate>

         <Triggers>

             <asp:PostBackTrigger ConrolID="Button1"/>

         </Triggers>

       </asp:UpdatePanel>

<div>

注意:在Button1_Click的事件中注册脚本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值