天天看点

asp.net 动态添加JavaScript方法

  //在页面顶部添加脚本

        if (!Page.ClientScript.IsClientScriptBlockRegistered("myscriptKey"))//判断是否已经存在相同//键值的ScriptBlock

        {

            string myScript = @"function AlertHello(){alert('hello xuanhun!!');}";//脚本内容

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "myscriptKey", myScript,true);

        }

        //在页面底部添加脚本

        if(!Page.ClientScript.IsStartupScriptRegistered("footscriptkey"))

        string CallScript = @"AlertHello();";

        Page.ClientScript.RegisterStartupScript(this.GetType(), "footscriptKey", CallScript, true);

     // 添加对js文件的引用

        if(!Page.ClientScript.IsClientScriptIncludeRegistered("incluedeKey"))

            string IncludeScript = @"/js/ShowHello.js";

            Page.ClientScript.RegisterClientScriptInclude("includeKey", IncludeScript);

本文转自悬魂博客园博客,原文链接:http://www.cnblogs.com/xuanhun/archive/2009/04/05/1429665.html,如需转载请自行联系原作者

继续阅读