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),除了注意粗体字以外,其他操作同上。紫色部分不能包含,如写了的话,浏览时会将脚本部分自动注释掉

  • 相关阅读:
    Tomcat配置JMX远程监控(Windown7 Linxu)
    Maven Tomcat:run 使用tomcat7
    关于C3P0容错和自动重连特性的研究
    密码算法记录
    Linxu 安装Nignx
    Linxu Yum方式安装Mysql
    Linxu
    tomcat结合nginx使用小结
    Tomcat性能优化(三) Executor配置
    深入学习C#匿名函数、委托、Lambda表达式、表达式树类型——Expression tree types
  • 原文地址:https://www.cnblogs.com/Jack-Chang/p/7095512.html
Copyright © 2011-2022 走看看