您的位置:首页 > Web前端 > JavaScript

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

2012-12-15 16:26 441 查看
页面中加入了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),除了注意粗体字以外,其他操作同上。紫色部分不能包含,如写了的话,浏览时会将脚本部分自动注释掉!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐