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