根据脚本配置参数,从而获得脚本包含文件路径,以及关联脚本文件,从而注册包含文件脚本块以及脚本初始化脚本块配置文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<javaScript-reference>
<!-- 脚本文件对应的名称以及路径 -->
<file-name name="label" src="js/Label.js"></file-name>
<file-name name="input" src="js/Input.js"></file-name>
<file-name name="btnTextInput" src="js/BtnTextInput.js"></file-name>
<file-name name="table" src="js/Table.js"></file-name>
<file-name name="ControlUtils" src="js/ControlUtils.js"></file-name>
<file-name name="Utils" src="js/Utils.js"></file-name>
<file-name name="WebUtils" src="js/WebUtils.js"></file-name>
<!-- 脚本关联文件 -->
<file-reference name="table">
<file-name name="ControlUtils"></file-name>
<file-name name="Utils"></file-name>
<file-name name="WebUtils"></file-name>
</file-reference>
<file-referenct name="input">
<file-name name="ControlUtils"></file-name>
<file-name name="WebUtils"></file-name>
</file-referenct>
<!--控件-->
<component class="ProlinkClass.ProlinkLabel" reference-file="label" init-method="installLableDefaultListener"></component>
<component class="ProlinkClass.ProlinkInput" reference-file="input" init-method="installInputDefaultListener"></component>
<component class="ProlinkClass.ProlinkBtnTextInput" reference-file="btnTextInput" init-method="installBtnTextInputDefaultListener"></component>
<component class="ProlinkClass.ProlinkTabel" reference-file="table" init-method="installTableDefaultListener"></component>
</javaScript-reference>后台类:
using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Xml.XPath;namespace ProlinkClass
{
/// <summary>
/// CreateClientScriptBlock 的摘要说明。
/// </summary>
public class CreateClientScriptBlock
{
private Page page; public CreateClientScriptBlock()
{
//
// TODO: 在此处添加构造函数逻辑
//
page = (Page) System.Web.HttpContext.Current.Handler;
} /// <summary>
/// 生成客户端脚本类
/// </summary>
/// <param name="p">某个页面Page类</param>
public CreateClientScriptBlock(Page p){
page = p;
} /// <summary>
/// 输出所有脚本块
/// </summary>
public void OutputScripts(){
if (!page.IsStartupScriptRegistered("body")){
page.RegisterStartupScript("body","<script language=\"javascript\"><!--installBodyDefaultListener(document.body);//--></script>");
}
GetScript(page);
} /// <summary>
/// 注册页面所有脚本块
/// </summary>
/// <param name="c">某个控件类</param>
private void GetScript(Control c){
if (c.ID != null){
GetScriptContent(c);
}
foreach(Control cc in c.Controls){
GetScript(cc);
}
} /// <summary>
/// 获得页面所有脚本块,包括包含文件脚本块和初始化事件脚本块
/// </summary>
/// <param name="c">控件</param>
private void GetScriptContent(Control c){
string strClass = c.GetType().ToString();
XmlDocument doc = new XmlDocument();
doc.Load(GetXmlPhysicalApplicationPath());
XmlNodeList xnl = doc.GetElementsByTagName("component");
if (xnl.Count > 0){
for(int i = 0; i < xnl.Count; i++){
XmlNode xn = xnl[i];
for (int j = 0; j < xn.Attributes.Count;j++){
Attribute att = xn.Attributes[i];
if (att.Equals("")){
}
} }
}
} /// <summary>
/// 获得脚本配置文件物理路径
/// </summary>
/// <returns>返回物流路径字符串</returns>
private string GetXmlPhysicalApplicationPath(){
string rawCurrentPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
string strXmlPath = rawCurrentPath + "reference.xml";
return strXmlPath;
} }
}

解决方案 »

  1.   

    Problem in here: /// <summary>
    /// 获得页面所有脚本块,包括包含文件脚本块和初始化事件脚本块
    /// </summary>
    /// <param name="c">控件</param>
    private void GetScriptContent(Control c){
    string strClass = c.GetType().ToString();
    XmlDocument doc = new XmlDocument();
    doc.Load(GetXmlPhysicalApplicationPath());
    XmlNodeList xnl = doc.GetElementsByTagName("component");
    if (xnl.Count > 0){
    for(int i = 0; i < xnl.Count; i++){
    XmlNode xn = xnl[i];
    for (int j = 0; j < xn.Attributes.Count;j++){
    Attribute att = xn.Attributes[i];
    if (att.Equals("")){
    >>>>>>这边该如何写?
    if (!page.IsClientScriptBlockRegistered (strClass)){
        page.RegisterClientScriptBlock(strClass,xml配置文件中的src)
    }
    if (!page.IsStartupScriptRegistered (c.ID)){
        page.RegisterStartupScript(c.ID,xml配置文件中的init-method)
    }
    }
    } }
    }
    }传strClass到<component>节点中,匹配属性class,从而获得reference-file及init-method,然后根据reference到<file-name>节点中查找脚本文件所在的路径,最后再根据<file-reference>节点中的name属性查找关联文件
      

  2.   

    结果是:
    比如我用了二个自定义控件<cc1:prolinkinput id="PO_NO" runat="server" FieldName="PO_NO"></cc1:prolinkinput>
    <cc1:prolinktabel id="Table1" runat="server"></cc1:prolinktabel>其中prolinkinput引用的脚本文件有Input,ControlUtils,WebUtils
    这样这个控件注册的脚本块为
    <script language="javascript" src="js/Input.js"></script>
    <script language="javascript" src="js/ControlUtils.js"></script>
    <script language="javascript" src="js/WebUtils.js"></script><script language="javascript">
    installInputDefaultListener(document.all.PO_NO);
    </script>
    而prolinktable引用的脚本文件有Table,ControlUtils,Utils,WebUtils
    生成的脚本块为
    <script language="javascript" src="js/Table.js"></script>
    <script language="javascript" src="js/ControlUtils.js"></script>//这边已经注册过了就不用再注册
    <script language="javascript" src="js/WebUtils.js"></script>//这边已经注册过了就不用再注册
    <script language="javascript" src="js/Utils.js"></script><script language="javascript">
    installTableDefaultListener(Tabel1);
    </script>
      

  3.   

    >>>>>>>>>>>>>>>>下面代码可以实现这个功能,不过我感觉循环用的太多了,可能效率有问题
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Text;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    using System.Xml.XPath;namespace ProlinkClass
    {
    /// <summary>
    /// CreateClientScriptBlock 的摘要说明。
    /// </summary>
    public class CreateClientScriptBlock
    {
    private Page page;
    XmlDocument doc = new XmlDocument(); public CreateClientScriptBlock()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    page = (Page) System.Web.HttpContext.Current.Handler;
    } /// <summary>
    /// 生成客户端脚本类
    /// </summary>
    /// <param name="p">某个页面Page类</param>
    public CreateClientScriptBlock(Page p){
    page = p;
    } /// <summary>
    /// 输出所有脚本块
    /// </summary>
    public void OutputScripts(){
    if (!page.IsClientScriptBlockRegistered("body")){
    page.RegisterClientScriptBlock("body","<script language=\"javascript\" src=\"" + System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "js\\Body.js" + "\"></script>");
    }
    if (!page.IsStartupScriptRegistered("body")){
    page.RegisterStartupScript("body","<script language=\"javascript\"><!--installBodyDefaultListener(document.body);//--></script>");
    }
    doc.Load(GetXmlPhysicalApplicationPath());
    GetScript(page);
    } /// <summary>
    /// 注册页面所有脚本块
    /// </summary>
    /// <param name="c">某个控件类</param>
    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 (!page.IsClientScriptBlockRegistered(fileName)){
    sScript = "<script language=\"javascript\" src=\"" + System.Web.HttpContext.Current.Request.PhysicalApplicationPath + filePath + "\"></script>";
    page.RegisterClientScriptBlock(fileName,sScript);
    }
    }
    if (!page.IsStartupScriptRegistered(c.ID)){
    sScript = "<script language=\"javascript\"><!--" + initMethod + "(document.all." + c.ID + ");//--></script>";
    page.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;
    }
    /// <summary>
    /// 获得脚本配置文件物理路径
    /// </summary>
    /// <returns>返回物流路径字符串</returns>
    private string GetXmlPhysicalApplicationPath(){
    string rawCurrentPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
    string strXmlPath = rawCurrentPath + "reference.xml";
    return strXmlPath;
    } }
    }