当前位置: 代码迷 >> Web前端 >> 打包一个树形菜单三――.Net封装控件
  详细解决方案

打包一个树形菜单三――.Net封装控件

热度:252   发布时间:2012-08-25 10:06:20.0
封装一个树形菜单三――.Net封装控件

Now I have the method to get a root of the? xml-format tree, and also a method to perform a dtree on website from the root. But now I have to make it a widget and more convient to use by other developers.

?

Here is the " helloo world " of asp.net widget.

?

1:create the "web widget" automatically by sdk

WebUserControl.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>

WebUserControl.ascx.cs:

using System;

public partial class WebUserControl : System.Web.UI.UserControl
{
    public string nameField { set; get; }
    protected void Page_Load(object sender, EventArgs e)
    {

        Response.Write("hello userControl " + nameField);
    }
}

?

The test file to call the WebUserControl widget:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testMain.aspx.cs" Inherits="tree_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="zyp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="dtree.js"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <zyp:WebUserControl runat="server"  nameField="zhuoyueping"/>
    </div>
    </form>
</body>
</html>

?then "hello userControl zhuoyueping " is outputed.

?

Okay~ Come back to my tree~~

?

== 还有5分钟12点。。。本来是打算今天晚上写文档的,又悲催了。。剩下把控件搞成dll,明天再说了。

看看调用先:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="testMain.aspx.cs" Inherits="tree_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register Src="TreeControl.ascx" TagName="TreeControl" TagPrefix="zyp" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript" src="dtree.js"></script>

</head>
<body>
    <form id="form1" runat="server">
    <div>
        <zyp:TreeControl runat="server" idField="name" nameField="name" xmlPath="F:\\menu.xml"/>
    </div>
    </form>
</body>
</html>
        <zyp:TreeControl runat="server" idField="name" nameField="name" xmlPath="F:\\menu.xml"/>
?

这句话话,可以指定的参数只比dtree少了一个pid,多了一个读取的xml路径,参数列表如下(跟dtree的一样)

Parameters

Name Type Description
idField Number Unique identity number.
pid Number Number refering to the parent node. The value for the root node has to be -1.
nameField String Text label for the node.
urlField String Url for the node.
titleField String Title for the node.
targetField String Target for the node.
iconField String Image file to use as the icon. Uses default if not specified.
iconOpenField String Image file to use as the open icon. Uses default if not specified.
openField Boolean Is the node open.

?

比如nameField,指定树种一个节点的名字,改名字从XML文件的拿个节点名读取。这里指定的是name,即从XML中name节点取值,作为该节点显示的名称。

<list>
	<class>com.Origin.Base.test.mywork.Tree.SimpleTreeReader</class>
   <menu>
	   	<name>a1</name>
	    <icon>a2</icon>
	    <url>a3</url>
	    <menu>
		   	<name>b1</name>
		    <icon>b2</icon>
		    <url>b3</url>
		    <menu>
		   		<name>f1</name>
		    	<icon>f2</icon>
		    	<url>f3</url>
	    </menu>
	    </menu>
   </menu>
   <menu>
	   	<name>c1</name>
	    <icon>c2</icon>
	    <url>c3</url>
	    <menu>
		   	<name>d1</name>
		    <icon>d2</icon>
		    <url>d3</url>
	    </menu>
   </menu>
</list>


?name节点的值会被显示出来作为节点的name,如图:

?

okay,看看控件的实现:

using System;
using System.Web;
using System.Web.UI;
using com.Origin.Base.test.mywork.Tree;
using System.Collections.Generic;

//如果加上namespace,则报错:神马inherts不对== 
  public partial class TreeControl : System.Web.UI.UserControl
  {
      public string idField { set; get; }
      public string nameField { set; get; }
      public string urlField { set; get; }
      public string titleField { set; get; }
      public string targetField { set; get; }
      public string iconField { set; get; }
      public string iconOpenField { set; get; }
      public string openField { set; get; }
      public string xmlPath { set; get; }

      private ITreeReaderFactory factory;

      protected void Page_Load(object sender, EventArgs e)
      {
          Response.Write("hello userControl" + null + " " + nameField);

          if (xmlPath == null) 
          {
              throw new Exception("Init XMLTreeRreaderFactory failed: xmlPath is missed");
          }

          factory = new XMLTreeRreaderFactory(@xmlPath);
          ITreeReader reader = factory.createTreeReader();
          ITreeNode root = reader.createTree();//得到了XML中的虚树根,该树根下挂着实际的子树

          //将该树渲染成dtree的js
        string str = "";
        str += "<script type='text/javascript' src='dtree.js'></script>";
        str += "<script type='text/javascript'>";
        str += " d = new dTree('d');";

        IList<ITreeNode> subNodes = root.getSubNodes();
        if (subNodes!=null)
        {
            for (int i = 0; i < subNodes.Count; i++)
            {
                str += convertToDTree(subNodes[i], null);
            }
        }
        //把树转换为dtree的格式
        str += convertToDTree(root, null);

        str += "document.write(d);";

        str += "</script>";
        
        Response.Write(str);
    }
    private string convertToDTree(ITreeNode root, string pid)
    {

        string name = (string)root.getAttribute("name");
        string url = (string)root.getAttribute("url");
        string add = "";
        string prefix = "d.add(";
        string suffix = ");";

        string str = initNodeString(root, pid);

        add = prefix + str + suffix;
        IList<ITreeNode> subNodes = root.getSubNodes();
        if (subNodes == null)
        {
            return add;
        }
        else
        {
            for (int i = 0; i < subNodes.Count; i++)
            {
                ITreeNode node = subNodes[i];
                
                add += convertToDTree(node, (string)root.getAttribute(idField));
            }
        }
        return add;
    }


    private string initNodeString(ITreeNode treeNode,string pid) 
    {
        if (openField != null)
        {
            return initNodeString((string)treeNode.getAttribute(idField),pid,(string)treeNode.getAttribute(nameField), (string)treeNode.getAttribute(urlField), (string)treeNode.getAttribute(titleField), (string)treeNode.getAttribute(targetField), (string)treeNode.getAttribute(iconField), (string)treeNode.getAttribute(iconOpenField), (string)treeNode.getAttribute(openField));
        }
        else if (iconOpenField != null)
        {
            return initNodeString((string)treeNode.getAttribute(idField), pid, (string)treeNode.getAttribute(nameField), (string)treeNode.getAttribute(urlField), (string)treeNode.getAttribute(titleField), (string)treeNode.getAttribute(targetField), (string)treeNode.getAttribute(iconField), (string)treeNode.getAttribute(iconOpenField));
        }
        else if (iconField != null)
        {
            return initNodeString((string)treeNode.getAttribute(idField), pid, (string)treeNode.getAttribute(nameField), (string)treeNode.getAttribute(urlField), (string)treeNode.getAttribute(titleField), (string)treeNode.getAttribute(targetField), (string)treeNode.getAttribute(iconField));
        }
        else if (targetField != null)
        {
            return initNodeString((string)treeNode.getAttribute(idField), pid, (string)treeNode.getAttribute(nameField), (string)treeNode.getAttribute(urlField), (string)treeNode.getAttribute(titleField), (string)treeNode.getAttribute(targetField));
        }
        else if (titleField != null)
        {
            return initNodeString((string)treeNode.getAttribute(idField), pid, (string)treeNode.getAttribute(nameField), (string)treeNode.getAttribute(urlField), (string)treeNode.getAttribute(titleField));
        }
        else if (urlField != null)
        {
            return initNodeString((string)treeNode.getAttribute(idField), pid, (string)treeNode.getAttribute(nameField), (string)treeNode.getAttribute(urlField));
        }
        else if (nameField != null)
        {
            return initNodeString((string)treeNode.getAttribute(idField), pid, (string)treeNode.getAttribute(nameField));
        }
        else {
            return null;
        }
   
    }
  
    private string initNodeString(string id,string pid,string name)
    {
        string str;
        if (pid == null)
        {
            str = "'" + id + "'" + "," + -1 + ",'" + name + "'";
        }
        else
        {
            str = "'" + id + "'" + ",'" + pid + "'" + ",'" + name + "'";
        }
        return str;

    }
    private string initNodeString(string id,string pid,string name,string url)
    {
        string str = initNodeString(id,pid,name) + ",'" + url + "'";
        return str;
      
    }
    private string initNodeString(string id, string pid, string name, string url, string title)
    {
        string str = initNodeString(id, pid, name, url) + ",'" + title + "'";
        return str;
    }
    private string initNodeString(string id, string pid, string name, string url, string title, string target)
    {
        string str = initNodeString(id, pid, name, url, title) + ",'" + target + "'";
        return str;

    }
    private string initNodeString(string id, string pid, string name, string url, string title, string target, string icon)
    {
        string str = initNodeString(id, pid, name, url, title, target) + ",'" + icon + "'";
        return str;

    }

    private string initNodeString(string id, string pid, string name, string url, string title, string target, string icon, string iconOpen)
    {
        string str = initNodeString(id, pid, name, url, title, target, icon) + ",'" + iconOpen + "'";
        return str;

    }
    private string initNodeString(string id, string pid, string name, string url, string title, string target, string icon, string iconOpen, string open)
    {
        string str = initNodeString(id, pid, name, url, title, target, icon, iconOpen) + "," + open;
        return str;

    }
 }

?

我自己写的时候都看花了。。。现在也12点过4分了。。

不过运气还好,一次调试就成功了~~

  相关解决方案