zoukankan      html  css  js  c++  java
  • UpdatePanel中用后台CS代码调用JS代码,先执行控件事件,后触发JS

    引用地址: http://www.cnblogs.com/silenkee/articles/1609831.html
     
    页面中加入了UpdatePanel后,Response.Write("<script>function dis (){alert('这是调用写在server的JS');}</script>")来调用客户端脚本,无任何提示的无反应。难道没有办法调用客户端脚本了?
         方法是有的,那就是采用 ScriptManager.RegisterStartupScript(Control controlId,Type this.GetType(),String key,String script block)方法。ps:有见到过帖子上说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="Button1" runat="server" Text="Button" OnClientClick="dis()"/>
                </ContentTemplate>     
    复制代码

            </asp:UpdatePanel>

    注意BtnJs是UpdatePanel外的按钮  同时Button1重用了服务端注册的脚本

        附带说一下,如果是在普通的aspx中希望在服务器端注册下客户端脚本,可以用

    Page.ClientScript.RegisterStartupScript(this.GetType(), String Key,String Js block ,Bool   AddScriptTag),除了注意粗体字以外,其他操作同上。紫色部分不能包含,如写了的话,浏览时会将脚本部分自动注释掉

  • 相关阅读:
    SpringMVC核心分发器DispatcherServlet分析[附带源码分析]
    SpringMVC异常处理机制详解[附带源码分析]
    SpringMVC重定向视图RedirectView小分析
    容器中使用iptables报错can't initialize iptables table Permission denied (you must be root)
    nohup介绍
    docker创建ceph集群
    maven单元测试设置代理
    Kubernetes服务之“运行单实例的有状态服务”
    关闭chrome浏览器的developer tools
    Kubernetes服务之StatefulSets简介
  • 原文地址:https://www.cnblogs.com/Jack-Chang/p/7095512.html
Copyright © 2011-2022 走看看