如何在UpdatePanel中调用JS客户端脚本
如何在UpdatePanel中调用JS客户端脚本
发布时间:2016-12-28 来源:查字典编辑
摘要:页面中加入了UpdatePanel后,Response.Write("functiondis(){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" 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),除了注意粗体字以外,其他操作同上。紫色部分不能包含,如写了的话,浏览时会将脚本部分自动注释掉!

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新C#教程学习
热门C#教程学习
编程开发子分类