ClientScriptManager 管理客户端脚本
ClientScriptManager 类用于管理客户端脚本并将它们添加到 Web 应用程序中。可以从 Page 对象的 ClientScript 属性获取对 ClientScriptManager 类的引用。
程序源代码:ClientScriptManager.rar
向客户端添加脚本:
/// <summary>
/// 向客户端添加脚本
/// </summary>
/// <param name="page">要添加脚本的页面</param>
/// <param name="csName">标识脚本的键</param>
/// <param name="csText">脚本内容</param>
public void RegisterClientScriptBlock(Page page,string csName,string csText)
{
ClientScriptManager cs = page.ClientScript;
Type cstype = page.GetType();
if (!cs.IsClientScriptBlockRegistered(cstype, csName))
{
cs.RegisterClientScriptBlock(cstype, csName, csText, true);
}
}
/// 向客户端添加脚本
/// </summary>
/// <param name="page">要添加脚本的页面</param>
/// <param name="csName">标识脚本的键</param>
/// <param name="csText">脚本内容</param>
public void RegisterClientScriptBlock(Page page,string csName,string csText)
{
ClientScriptManager cs = page.ClientScript;
Type cstype = page.GetType();
if (!cs.IsClientScriptBlockRegistered(cstype, csName))
{
cs.RegisterClientScriptBlock(cstype, csName, csText, true);
}
}
例如:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager scriptManager = new ScriptManager();
scriptManager.RegisterClientScriptBlock(this, "TestAlert", "alert('测试 alert !');");
}
{
ScriptManager scriptManager = new ScriptManager();
scriptManager.RegisterClientScriptBlock(this, "TestAlert", "alert('测试 alert !');");
}
向客户端添加数组:
/// <summary>
/// 使用数组名称和数组值向 Page 对象注册 JavaScript 数组声明。
/// </summary>
/// <param name="page">添加脚本的页面</param>
/// <param name="arrayName">数组名称</param>
/// <param name="array">数组值</param>
public void RegisterArrayDeclaration(Page page, string arrayName, Array array)
{
StringBuilder sbArrayValue=new StringBuilder();
for (int i = 0; i < array.Length; i++)
{
if (i != 0)
{
sbArrayValue.Append(",\""+array.GetValue(i).ToString()+"\"");
}
else
{
sbArrayValue.Append("\"" + array.GetValue(i).ToString() + "\"");
}
}
ClientScriptManager cs = page.ClientScript;
cs.RegisterArrayDeclaration(arrayName, sbArrayValue.ToString());
}
/// 使用数组名称和数组值向 Page 对象注册 JavaScript 数组声明。
/// </summary>
/// <param name="page">添加脚本的页面</param>
/// <param name="arrayName">数组名称</param>
/// <param name="array">数组值</param>
public void RegisterArrayDeclaration(Page page, string arrayName, Array array)
{
StringBuilder sbArrayValue=new StringBuilder();
for (int i = 0; i < array.Length; i++)
{
if (i != 0)
{
sbArrayValue.Append(",\""+array.GetValue(i).ToString()+"\"");
}
else
{
sbArrayValue.Append("\"" + array.GetValue(i).ToString() + "\"");
}
}
ClientScriptManager cs = page.ClientScript;
cs.RegisterArrayDeclaration(arrayName, sbArrayValue.ToString());
}
例如:
protected void Page_Load(object sender, EventArgs e)
{
string arrayName = "testArray";
int[] array ={1,2,3 };
ScriptManager scriptManager = new ScriptManager();
scriptManager.RegisterArrayDeclaration(this, arrayName, array);
StringBuilder csText = new StringBuilder();
csText.Append("function doSum() {");
csText.Append("var arrSum=");
csText.Append("parseInt(" + arrayName + "[0])+");
csText.Append("parseInt(" + arrayName + "[1])+");
csText.Append("parseInt(" + arrayName + "[2]);");
csText.Append("alert(arrSum);");
csText.Append("}");
scriptManager.RegisterClientScriptBlock(this, "Sum", csText.ToString());
btnDoSum.OnClientClick = "doSum(); return false";
}
{
string arrayName = "testArray";
int[] array ={1,2,3 };
ScriptManager scriptManager = new ScriptManager();
scriptManager.RegisterArrayDeclaration(this, arrayName, array);
StringBuilder csText = new StringBuilder();
csText.Append("function doSum() {");
csText.Append("var arrSum=");
csText.Append("parseInt(" + arrayName + "[0])+");
csText.Append("parseInt(" + arrayName + "[1])+");
csText.Append("parseInt(" + arrayName + "[2]);");
csText.Append("alert(arrSum);");
csText.Append("}");
scriptManager.RegisterClientScriptBlock(this, "Sum", csText.ToString());
btnDoSum.OnClientClick = "doSum(); return false";
}
添加客户端脚本包含:
/// <summary>
/// 使用键和 URL 向 Page 对象注册客户端脚本包含。
/// </summary>
/// <param name="page">添加脚本的页面</param>
/// <param name="includeName">要注册的客户端脚本包含的键。 </param>
/// <param name="url">要注册的客户端脚本包含的 URL。</param>
public void RegisterClientScriptInclude(Page page, string includeName, string url)
{
ClientScriptManager cs = page.ClientScript;
cs.RegisterClientScriptInclude(includeName, url);
}
/// 使用键和 URL 向 Page 对象注册客户端脚本包含。
/// </summary>
/// <param name="page">添加脚本的页面</param>
/// <param name="includeName">要注册的客户端脚本包含的键。 </param>
/// <param name="url">要注册的客户端脚本包含的 URL。</param>
public void RegisterClientScriptInclude(Page page, string includeName, string url)
{
ClientScriptManager cs = page.ClientScript;
cs.RegisterClientScriptInclude(includeName, url);
}
例如:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager scriptManager = new ScriptManager();
scriptManager.RegisterClientScriptInclude(this, "testInclude", "JScript.js");
}
{
ScriptManager scriptManager = new ScriptManager();
scriptManager.RegisterClientScriptInclude(this, "testInclude", "JScript.js");
}