• 对客户端树控件的包装


    树控件在项目被经常使用,dtree.js这个脚本也被广泛使用,在服务端用起来有些不太方便,我写了以下一个类,是对dtree.js的包装。

    代码
    1 using System;
    2  using System.Collections.Generic;
    3  using System.Linq;
    4  using System.Text;
    5 using System.Web.UI.WebControls;
    6
    7 namespace Library.WebControls
    8 {
    9 publicclass Tree : Node
    10 {
    11 //仅有一个根结点
    12 privatestatic Node _rootNode =null;
    13 privateconstint _treePid =-1;
    14 privateconstint _id =0;
    15 privatereadonlystaticstring _openstream =@"a.openTo({0},true,false);";
    16 privatestring _keepopennodestream =string.Empty;
    17
    18 public Node Root
    19 {
    20 get
    21 {
    22 return _rootNode;
    23 }
    24 }
    25
    26 public Tree(string nodeText)
    27 {
    28 _rootNode =new Node(_id, _treePid, nodeText);
    29 //新建树节点时重新分配节点集
    30 Node._nodeList =new List<Node>();
    31 //树根
    32 base.Add(_rootNode);
    33 }
    34
    35 ///<summary>
    36 /// 新建时默认打一个节点
    37 ///</summary>
    38 ///<param name="value"></param>
    39 publicvoid KeepOpenNode(int value)
    40 {
    41 _keepopennodestream =string.Format(_openstream, value);
    42 }
    43
    44 ///<summary>
    45 /// 渲染树节构
    46 ///</summary>
    47 ///<returns></returns>
    48 publicnewstring Render()
    49 {
    50 StringBuilder sb =new StringBuilder();
    51 sb.Append("a = new dTree('a');");
    52 sb.Append("a.config.useCookies=true;");
    53 //递归渲染叶
    54 sb.Append(base.Render());
    55 if (!string.IsNullOrEmpty(_keepopennodestream))
    56 {
    57 sb.Append(_keepopennodestream);
    58 }
    59 return sb.ToString();
    60 }
    61
    62 }
    63
    64 publicclass Node
    65 {
    66
    67 #region 内部量
    68 privateint _id;
    69 privateint _pid;
    70 privatestring _text;
    71 privatestring _url ="about:blank";
    72 privatestring _mouseovertip ="";
    73 privatestring _target ="";
    74 privatestring _nodestruct =@"a.add({0},{1},'{2}','{3}','{4}','{5}');";
    75 privatestring _writestream ="";
    76 ///<summary>
    77 /// 全树仅一个节点集
    78 ///</summary>
    79 internalstatic List<Node> _nodeList =null;
    80 #endregion
    81
    82 #region 外部属性
    83 publicint Id
    84 {
    85 set
    86 {
    87 _id = value;
    88 }
    89 get
    90 {
    91 return _id;
    92 }
    93 }
    94
    95 publicint Pid
    96 {
    97 set
    98 {
    99 _pid = value;
    100 }
    101 get
    102 {
    103 return _pid;
    104 }
    105 }
    106
    107 publicstring Text
    108 {
    109 set
    110 {
    111 _text = value;
    112 }
    113 get
    114 {
    115 if (string.IsNullOrEmpty(_text)) return"";
    116 return _text;
    117 }
    118 }
    119
    120 publicstring Url
    121 {
    122 set
    123 {
    124 _url = value;
    125 }
    126 get
    127 {
    128 if (string.IsNullOrEmpty(_url)) return"about:blank";
    129 return _url;
    130 }
    131 }
    132 publicstring MouseOverTip
    133 {
    134 set
    135 {
    136 _mouseovertip = value;
    137 }
    138 get
    139 {
    140 if (string.IsNullOrEmpty(_mouseovertip)) return"";
    141 return _mouseovertip;
    142 }
    143 }
    144 publicstring TarGet
    145 {
    146 set
    147 {
    148 _target = value;
    149 }
    150 get
    151 {
    152 if (string.IsNullOrEmpty(_target)) return"";
    153 return _target;
    154 }
    155 }
    156
    157 internalint Count
    158 {
    159 get
    160 {
    161 return _nodeList.Count;
    162 }
    163 }
    164
    165 #endregion
    166
    167 #region 构造 Node()
    168 public Node()
    169 { }
    170
    171 ///<summary>
    172 /// 构造一个node点
    173 ///</summary>
    174 ///<param name="id">当前ID号</param>
    175 ///<param name="pid">父ID号</param>
    176 ///<param name="text">显示文本</param>
    177 public Node(int id, int pid, string text)
    178 {
    179 _id = id;
    180 _pid = pid;
    181 _text = text;
    182 }
    183 ///<summary>
    184 /// 构造一个node点
    185 ///</summary>
    186 ///<param name="id">当前ID号</param>
    187 ///<param name="pid">父ID号</param>
    188 ///<param name="text">显示文本</param>
    189 ///<param name="url">连接地址</param>
    190 public Node(int id, int pid, string text, string url)
    191 {
    192 _id = id;
    193 _pid = pid;
    194 _text = text;
    195 _url = url;
    196 }
    197 ///<summary>
    198 /// 构造一个node点
    199 ///</summary>
    200 ///<param name="id">当前ID号</param>
    201 ///<param name="pid">父ID号</param>
    202 ///<param name="text">显示文本</param>
    203 ///<param name="url">连接地址</param>
    204 ///<param name="mouseovertip">鼠标悬浮时显示</param>
    205 ///<param name="target">框架名</param>
    206 public Node(int id, int pid, string text, string url, string mouseovertip, string target)
    207 {
    208 _id = id;
    209 _pid = pid;
    210 _text = text;
    211 _url = url;
    212 _mouseovertip = mouseovertip;
    213 _target = target;
    214 }
    215 ///<summary>
    216 /// 构造一个node点
    217 ///</summary>
    218 ///<param name="id">当前ID号</param>
    219 ///<param name="pid">父ID号</param>
    220 ///<param name="text">显示文本</param>
    221 ///<param name="url">连接地址</param>
    222 ///<param name="target">框架名</param>
    223 public Node(int id, int pid, string text, string url, string target)
    224 {
    225 _id = id;
    226 _pid = pid;
    227 _text = text;
    228 _url = url;
    229 _target = target;
    230 }
    231
    232 #endregion
    233
    234 #region 外部方法
    235 ///<summary>
    236 /// 外部渲染方法
    237 ///</summary>
    238 ///<returns></returns>
    239 internalstring Render()
    240 {
    241 returnthis.Render(this.Count -1);
    242 }
    243
    244 ///<summary>
    245 /// 添加一个Node
    246 ///</summary>
    247 ///<param name="node"></param>
    248 ///<returns></returns>
    249 public Node Add(Node node)
    250 {
    251 _nodeList.Add(node);
    252 return node;
    253 }
    254 #endregion
    255
    256 privatestring Render(int count)
    257 {
    258 if (_nodeList[count].Pid ==-1)
    259 returnstring.Format(_nodestruct, _id, _pid, _text, _url, _mouseovertip, _target);
    260 else
    261 {
    262
    263 _writestream =string.Format(_nodestruct, _nodeList[count]._id,
    264 _nodeList[count]._pid, _nodeList[count]._text,
    265 _nodeList[count]._url, _nodeList[count]._mouseovertip,
    266 _nodeList[count]._target);
    267 return _writestream = (_nodeList[count -1].Render(count -1) + _writestream);
    268 }
    269 }
    270
    271 }
    272 }

    使用方法:

    代码
    1 publicpartialclass DemoTree : System.Web.UI.Page
    2 {
    3 protected Tree tree =new Tree("测试树");
    4 protectedvoid Page_Load(object sender, EventArgs e)
    5 {
    6 this.InitScript();
    7 }
    8 protectedoverridevoid OnInit(EventArgs e)
    9 {
    10 Node node = tree.Add(new Node(1, tree.Root.Id, "根1", "#"));
    11 Node node2 = tree.Add(new Node(2, tree.Root.Id, "根2", "#"));
    12 Node node3 = tree.Add(new Node(3, tree.Root.Id, "根3", "#"));
    13
    14 Node nodelist = node.Add(new Node(4, node.Id, "叶1", "#"));
    15 Node nodelist2 = node.Add(new Node(5, node.Id, "叶2", "#"));
    16
    17 Node nodelist3 = node3.Add(new Node(6, node3.Id, "子叶1", "#"));
    18 Node nodelist4 = node3.Add(new Node(7, node3.Id, "子叶2", "#"));
    19 Node nodelist5 = node3.Add(new Node(8, node3.Id, "子叶3", "#"));
    20
    21 Node nodelist6 = nodelist5.Add(new Node(9, nodelist5.Id, "子子叶1", "#"));
    22
    23 base.OnInit(e);
    24 }
    25
    26
    27
    28 protectedvoid InitScript()
    29 {
    30 Type type = GetType();
    31 if (ClientScript.IsStartupScriptRegistered(type, "TreeScript") ==false)
    32 {
    33 string strScript =@"<script language=javascript>"+ tree.Render() +"document.write(a);</script>";
    34
    35 ClientScript.RegisterStartupScript(type, "TreeScript", strScript);
    36 }
    37 }
    38
    39 }
    40
  • 相关阅读:
    对于捐赠承诺和劳务捐赠,不予以确认,但应在会计报表附注中披露
    R语言代写线性混合效应模型Linear Mixed-Effects Models的部分折叠Gibbs采样
    matlab代写MCMC贝叶斯方法用于加筋复合板的冲击载荷识别
    R语言代写dplyr-高效的数据变换与整理工具
    GIS代写遥感数据可视化评估:印度河流域上部的积雪面积变化
    R语言代写向量自回归模型(VAR)及其实现
    r语言代写实现似然的I(2)协整VAR模型弱外生性推理
    python代写缺失值处理案例分析:泰坦尼克数据
    Python代写高性能计算库——Numba
    matlab递归神经网络RNN实现:桨距控制控制风力发电机组研究
  • 原文地址:https://www.cnblogs.com/shouhongxiao/p/1682270.html
Copyright © 2020-2023  润新知