客户端脚本刚好在 Page 对象的
///
/// 注册页面所有脚本块
///
/// 某个控件类
private void GetScript(Control c) {
if (c.ID != null) {
GetComponent(c);
}
foreach(Control cc in c.Controls) {
GetScript(cc);
}
}
private void GetComponent(Control c) {
string strClass = c.GetType().ToString();
XmlNodeList xnl = doc.GetElementsByTagName("component");
if (xnl.Count > 0) {
for (int i = 0; i < xnl.Count; i++) {
XmlNode xn = xnl[i];
XmlAttributeCollection xac = xn.Attributes;
if (xac.Count > 0){
for (int j = 0; j < xac.Count; j++) {
XmlAttribute xa = xac[j];
if ((xa.Name).Equals("class") && ((xa.Value).Equals(strClass))) {
GetFileReference(c,xac.GetNamedItem("init-method").Value,xac.GetNamedItem("reference-file").Value);
}
else {
continue;
}
}
}
}
}
}
private void GetFileReference(Control c, string initMethod, string fileName) {
XmlNodeList xnl = doc.GetElementsByTagName("file-reference");
ArrayList fileNameList = new ArrayList();
fileNameList.Add(fileName);
if (xnl.Count > 0) {
for (int i = 0; i < xnl.Count; i++) {
XmlNode xn = xnl[i];
XmlAttributeCollection xac = xn.Attributes;
if (xac.Count > 0) {
for (int j = 0; j < xac.Count; j++) {
XmlAttribute xa = xac[j];
if ((xa.Name.Equals("name")) && (xa.Value.Equals(fileName))) {
if (xn.HasChildNodes) {
XmlNodeList xnlChild = xn.ChildNodes;
for (int k = 0; k < xnlChild.Count; k++) {
fileNameList.Add(xnlChild.Item(k).Attributes.GetNamedItem("name").Value);
}
}
}
}
}
}
}
OutputScript(c,initMethod,fileNameList);
}
private void OutputScript(Control c, string initMethod, ArrayList fileNameList) {
string fileName;
string filePath;
string sScript;
if (fileNameList.Count > 0) {
for (int i = 0; i < fileNameList.Count; i++) {
fileName = fileNameList[i].ToString();
filePath = GetFilePath(fileName);
if (filePath.Equals("")) return;
if (!this.IsClientScriptBlockRegistered(fileName)) {
sScript. = "
this.RegisterClientScriptBlock(fileName,sScript);
}
}
if (!this.IsStartupScriptRegistered(c.ID)) {
sScript. = "
this.RegisterStartupScript(c.ID,sScript);
}
}
}
private string GetFilePath(string fileName) {
string strFileName = "";
XmlNodeList xnl = doc.GetElementsByTagName("file-name");
if (xnl.Count > 0) {
for (int i = 0; i < xnl.Count; i++) {
XmlNode xn = xnl[i];
XmlAttributeCollection xac = xn.Attributes;
if (xac.Count > 0){
for (int j = 0;j < xac.Count; j++) {
XmlAttribute xa = xac[j];
if (xa.Name.Equals("name") && xa.Value.Equals(fileName)) {
strFileName = xac.GetNamedItem("src").Value;
}
}
}
}
}
return strFileName;
}
///
/// 获得脚本配置文件物理路径
///
///
private string GetXmlPhysicalApplicationPath() {
string rawCurrentPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
string strXmlPath = rawCurrentPath + "reference.xml";
return strXmlPath;
}
}
}
}
}
脚本配置文件如下:
<!-- 脚本文件对应的名称以及路径 -->
<!-- 脚本关联文件 -->
<!--控件-->
WebForm1.aspx.cs:
public class WebForm1 : Prolink.Web.UI.WebForm
{
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-349319/ ,如需转载,请注明出处,否则将追究法律责任。