zoukankan      html  css  js  c++  java
  • ASP.NET后台输出js

    一、使用RegisterClientScriptBlock输出js代码块

     StringBuilder sb = new StringBuilder();
      sb.Append("<input type="text" id="txtGo" name="txtGo" style=" 30px" >");
                    sb.Append("<input type="button" id="btnGo" name="btnGo" value="跳转" onclick="pagego();" >");
     string scriptstrs = "";
                    scriptstrs += "function pagego(){";
                    scriptstrs += " var val = document.getElementById('txtGo').value;";
                    scriptstrs +=" if (val >"+PageCount+") {alert('不能大于'+"+PageCount+");}";
                    scriptstrs += "else {window.location.href = 'WordManager.aspx?page='+val;}}";
                    if (!Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), "keys"))
                    {
    
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "keys", scriptstrs, true);
    
                    }

    二、使用 RegisterStartupScript输出一次性使用的js代码

          

      string scriptstrs = "<script>alert('欢迎光临!');</script>";
            if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(),"welcome"))
            {
                Page.ClientScript.RegisterStartupScript(this.GetType(), "welcome", scriptstrs);
            }

    三、使用RegisterClientScriptInclude后台输出已有js文件

    首先创建 js文件test.js

    if (!Page.ClientScript.IsClientScriptIncludeRegistered(this.GetType(), "keys"))//判断keys是否已注册过
    {
       Page.ClientScript.RegisterClientScriptInclude("keys", "testjs.js");     
    } 

    四、运用Response.Write("<script>alert('ww');</script>"); 方式也可输出简单js代码(不建议

  • 相关阅读:
    centos 安装python3.6 简易安装过程
    包装类型
    剑指offer二叉树中和为某一值的路径
    剑指offer 捡绳子
    按位与的技巧
    SpringBoot 登录拦截功能的实现
    AIO实现简单http服务器
    真题-数的分解
    重建二叉树
    旋转数组的最小数字
  • 原文地址:https://www.cnblogs.com/sunxi/p/6382194.html
Copyright © 2011-2022 走看看