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 @ 2009-04-05 00:03  玄魂  阅读(3020)  评论(1编辑  收藏  举报