• Asp.net 后台调用js方法


     Response.Write("<script type='text/javascript'>alert("我要出来咯2");</script>");  

    HtmlGenericCOntrol是一个通用标签生成的类,可以用来生成js也可以用来生成任何其它的标签

     HtmlGenericControl script = new HtmlGenericControl("script");
    
                script.Attributes.Add("type", "text/javascript"); //IAttributeAccessor
    
     
    
                script.InnerHtml = "alert('我是用HtmlGenericControl生成的 script')";
    
     
    
                this.Header.Controls.Add(script);//把script 标签添加到head 标签里。

     

    自己封装的弹窗的类

    public static class PageEx
        {
           
            /// <summary>
            /// 
            /// </summary>
            /// <param name="page">页面</param>
            /// <param name="statement">脚本</param>
            /// <param name="type">类型:code js脚本代码,file js文件</param>
            /// <param name="location">放在form里的位置</param>
            public static void MessageBox(this Page  page,string statement,
                string type ="code",string location="bottom")
            {
                if (type=="code")
                {
                    page.ClientScript.RegisterStartupScript(page.GetType(),
                        Guid.NewGuid().ToString(), 
                        statement,true);    
                }
                else if (type == "file") 
                {
                    page.ClientScript.RegisterClientScriptInclude(
                        Guid.NewGuid().ToString(),
                        statement); 
                }
            }
        }
    View Code

    调用

     PageEx.MessageBox(this,"alert('mymsg')",location:"top");
    PageEx.MessageBox(this, "demo.js","file");  
    this.MessageBox("alert('mymsg')");

    ClientScript 可以方便地管理 JavaScript,

    应该说 ClientScript.RegisterClientScriptBlock 与 ClientScript.RegisterStartupScript 只有一点区别,

    那就是 RegisterClientScriptBlock 将脚本代码写在 <form> 之后,

    而 RegisterStartupScript 将代码写在 </form>(注意是结束标签)之前。

    public void RegisterClientScriptBlock(Type type, string key, string script)public void RegisterClientScriptBlock(Type type, string key, string script, bool addScriptTags)

    public void RegisterStartupScript(Type type, string key, string script)public void RegisterStartupScript(Type type, string key, string script, bool addScriptTags)

    可以看出二者语法相同。

    type 要注册的启动脚本的类型。

    key 要注册的启动脚本的键,也就是你自己给这段脚本起的名字。相同 key 的脚本被当作是重复的,对于这样的脚本只输出最先注册的,ClientScriptBlock 和 StartupScript 中的 key 相同不算是重复的。

    script 脚本代码。

    addScriptTags 是否添加 <script> 标签,如果脚本代码中不含 <script> 标签,则应该指定该值为 true,若不指定该值,会被当作 false 对待。

    clientscript

    //此段代码会放置到form的前面

    string alterstr= "alert('我要出来咯 clientscript')";

      this.ClientScript.RegisterClientScriptBlock(this.GetType()   , Guid.NewGuid().ToString(),alterstr, true);

    //此段代码会放置到form的后面

    this.ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "alter('我要出来咯 startscript')", true);

  • 相关阅读:
    Spring/Spring-Boot 学习 连接redis数据库
    Spring/Spring-Boot 学习 答疑
    Spring/Spring-Boot 学习 paoding-rose-jade 连接MySQL数据库
    Spring/Spring-Boot 学习 Bean注入的方式
    Spring/Spring-Boot 学习 @Autowired 与 @Resource
    博客园主题与代码高亮
    从不懂spring的开始的学习之旅
    nginx 缓存服务器配置
    jenkins + sonar代码质量自动化分析平台
    Linux下如何查看哪些进程占用的CPU内存资源最多
  • 原文地址:https://www.cnblogs.com/youchim/p/6035670.html
Copyright © 2020-2023  润新知