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);

  • 相关阅读:
    kubernetes之StatefulSet详解
    kubernetes调度之污点(taint)和容忍(toleration)
    Kubernetes调度之亲和与反亲和
    kubernetes调度之 PriorityClass
    kubernetes里的各种port解惑
    # kubernetes调度之nodeName与NodeSelector
    kubectl rollout回滚和autoscale自动扩容
    Kubernetes基本概念之Label
    kubernetes之多容器pod以及通信
    设计模式-装饰模式
  • 原文地址:https://www.cnblogs.com/youchim/p/6035670.html
Copyright © 2011-2022 走看看