当前位置: 移动技术网 > IT编程>开发语言>c# > 如何在UpdatePanel中调用JS客户端脚本

如何在UpdatePanel中调用JS客户端脚本

2019年07月18日  | 移动技术网IT编程  | 我要评论
页面中加入了updatepanel后,response.write("<script>function dis (){alert('这是调用写在server的j
页面中加入了updatepanel后,response.write("<script>function dis (){alert('这是调用写在server的js');}</script>")来调用客户端脚本,无任何提示的无反应。如何在updatepanel中调用js客户端脚本呢?

方法:采用 scriptmanager.registerstartupscript(control controlid,type this.gettype(),string key,string script block)方法。
有人说controlid必须是updatepanel里的,其实不然,page控件就可以。

下面给出一个具体的实例:
复制代码 代码如下:

protected void page_load(object sender, eventargs e)
{
scriptmanager.registerstartupscript(btnjs, this.gettype(), "alert", "<script>function
dis (){alert('这是调用写在server的js,如用response.write()是不能实现此效果的!!! ');}</script>", false);

复制代码 代码如下:

<asp:scriptmanager id="scriptmanager1" runat="server" enablepartialrendering="true" >
</asp:scriptmanager>
<input id="btnjs" type="button" value="callserverjs" onclick="dis()" runat="server"/>
<asp:updatepanel id="updatepanel1" runat="server" updatemode="conditional" rendermode="block">
<contenttemplate>
<asp:button id="button6" runat="server" text="button" onclientclick="dis()"/>
</contenttemplate>
</asp:updatepanel>

注意:btnjs是updatepanel外的按钮 同时button6重用了服务端注册的脚本附带说一下,如果是在普通的aspx中希望在服务器端注册下客户端脚本,可以用
page.clientscript.registerstartupscript(this.gettype(), string key,string js block ,bool addscripttag),除了注意粗体字以外,其他操作同上。紫色部分不能包含,如写了的话,浏览时会将脚本部分自动注释掉!

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网