当前位置: 移动技术网 > IT编程>开发语言>.net > ScriptManager.RegisterStartupScript()方法在ajax页面无效的解决方法

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

仙羽幻境好看吗,2hhhh,男男生子过程图

如果不用ajax,cs中运行某段js代码方式可以是:
page.clientscript.registerstartupscript(page.gettype(), "", "<script>window.open('default2.aspx')</script>");
如果页面中使用了ajax ,则上述代码即使执行也无效果。应对这种情况我们通常采用:
scriptmanager.registerstartupscript(this.button1, this.gettype(), "alertscript", "window.open('default2.aspx');", true);
其中第一个参数为要注册脚本的控件id,试了一下,只要是本页面的就行。
第二个参数为注册脚本控件类型,是控件还是this的gettype()都可以,typeof(string)也没问题.
第三个脚本函数的名字,随便起。
第四个是脚本内容。
第五个是标明是否再添加脚本标签,如果第四个参数里包含了<script></script>标签,此处则为false,否则为true。

注意:aspx代码是这样的

<div>
<asp:updatepanel id="updatepanel1" runat="server">
<contenttemplate>
<asp:textbox runat="server" id="textbox2" >
</asp:textbox>
<asp:button runat="server" text="button" id="button1" nclick="button1_click" />
</contenttemplate>
<triggers>
<asp:postbacktrigger controlid="button1" />
</triggers>
</asp:updatepanel>
</div>

我在button1_click的事件里注册脚本,一定要加红色的部分,否则总是提示不能parse什么东西!

另外,js无法干涉cs代码。所以一旦脚本注册成功,js和cs代码会互不相干的各自运行。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网