动态添加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 2009-08-17 08:40  Lionheart Zhang  阅读(310)  评论(0编辑  收藏  举报

导航