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

        }

posted on 2013-03-12 10:23  AI_JJ  阅读(116)  评论(0)    收藏  举报

导航