zoukankan      html  css  js  c++  java
  • 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);

  • 相关阅读:
    ERROR: HHH000123: IllegalArgumentException in class: com.tt.hibernate.helloworld.News, setter method of property: date
    Tomcat服务器重启失败:The server may already be running in another process, or a system process may be using the port.
    ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type=InnoDB' at line 7
    跨平台开发之阿里Weex框架环境搭建(一)
    fielderror里的fieldName代表的是jsp里的fieldName还是Action类的成员变量?(待解答)
    Virtualbox 安装
    下载CentOS镜像
    Express框架的创建
    安装MongoDB及所遇见的问题和解决方案
    javascript 插入DOM节点
  • 原文地址:https://www.cnblogs.com/youchim/p/6035670.html
Copyright © 2011-2022 走看看