TreeView.cs 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  1. //
  2. // System.Web.UI.WebControls.TreeView.cs
  3. //
  4. // Authors:
  5. // Lluis Sanchez Gual ([email protected])
  6. //
  7. // (C) 2004 Novell, Inc (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  29. //
  30. #if NET_2_0
  31. using System.Collections;
  32. using System.Text;
  33. using System.ComponentModel;
  34. using System.Web.Handlers;
  35. using System.Collections.Specialized;
  36. using System.IO;
  37. using System.Security.Permissions;
  38. using System.Collections.Generic;
  39. namespace System.Web.UI.WebControls
  40. {
  41. // CAS
  42. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  43. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  44. // attributes
  45. [SupportsEventValidation]
  46. [ControlValueProperty ("SelectedValue")]
  47. [DefaultEvent ("SelectedNodeChanged")]
  48. [Designer ("System.Web.UI.Design.WebControls.TreeViewDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  49. public class TreeView: HierarchicalDataBoundControl, IPostBackEventHandler, IPostBackDataHandler, ICallbackEventHandler
  50. {
  51. bool stylesPrepared;
  52. Style hoverNodeStyle;
  53. TreeNodeStyle leafNodeStyle;
  54. TreeNodeStyle nodeStyle;
  55. TreeNodeStyle parentNodeStyle;
  56. TreeNodeStyle rootNodeStyle;
  57. TreeNodeStyle selectedNodeStyle;
  58. TreeNodeStyleCollection levelStyles;
  59. TreeNodeCollection nodes;
  60. TreeNodeBindingCollection dataBindings;
  61. TreeNode selectedNode;
  62. Hashtable bindings;
  63. int registeredStylesCounter = -1;
  64. List<Style> levelLinkStyles;
  65. Style controlLinkStyle;
  66. Style nodeLinkStyle;
  67. Style rootNodeLinkStyle;
  68. Style parentNodeLinkStyle;
  69. Style leafNodeLinkStyle;
  70. Style selectedNodeLinkStyle;
  71. Style hoverNodeLinkStyle;
  72. private static readonly object TreeNodeCheckChangedEvent = new object();
  73. private static readonly object SelectedNodeChangedEvent = new object();
  74. private static readonly object TreeNodeCollapsedEvent = new object();
  75. private static readonly object TreeNodeDataBoundEvent = new object();
  76. private static readonly object TreeNodeExpandedEvent = new object();
  77. private static readonly object TreeNodePopulateEvent = new object();
  78. static Hashtable imageStyles = new Hashtable ();
  79. class ImageStyle
  80. {
  81. public ImageStyle (string expand, string collapse, string noExpand, string icon, string iconLeaf, string iconRoot) {
  82. Expand = expand;
  83. Collapse = collapse;
  84. NoExpand = noExpand;
  85. RootIcon = iconRoot;
  86. ParentIcon = icon;
  87. LeafIcon = iconLeaf;
  88. }
  89. public string Expand;
  90. public string Collapse;
  91. public string NoExpand;
  92. public string RootIcon;
  93. public string ParentIcon;
  94. public string LeafIcon;
  95. }
  96. static TreeView ()
  97. {
  98. imageStyles [TreeViewImageSet.Arrows] = new ImageStyle ("arrow_plus", "arrow_minus", "arrow_noexpand", null, null, null);
  99. imageStyles [TreeViewImageSet.BulletedList] = new ImageStyle (null, null, null, "dot_full", "dot_empty", "dot_full");
  100. imageStyles [TreeViewImageSet.BulletedList2] = new ImageStyle (null, null, null, "box_full", "box_empty", "box_full");
  101. imageStyles [TreeViewImageSet.BulletedList3] = new ImageStyle (null, null, null, "star_full", "star_empty", "star_full");
  102. imageStyles [TreeViewImageSet.BulletedList4] = new ImageStyle (null, null, null, "star_full", "star_empty", "dots");
  103. imageStyles [TreeViewImageSet.Contacts] = new ImageStyle ("TreeView_plus", "TreeView_minus", "contact", null, null, null);
  104. imageStyles [TreeViewImageSet.Events] = new ImageStyle (null, null, null, "warning", "warning", "warning");
  105. imageStyles [TreeViewImageSet.Inbox] = new ImageStyle (null, null, null, "inbox", "inbox", "inbox");
  106. imageStyles [TreeViewImageSet.Msdn] = new ImageStyle ("box_plus", "box_minus", "box_noexpand", null, null, null);
  107. imageStyles [TreeViewImageSet.Simple] = new ImageStyle (null, null, "box_full", null, null, null);
  108. imageStyles [TreeViewImageSet.Simple2] = new ImageStyle (null, null, "box_empty", null, null, null);
  109. // TODO
  110. imageStyles [TreeViewImageSet.News] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", null, null, null);
  111. imageStyles [TreeViewImageSet.Faq] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", null, null, null);
  112. imageStyles [TreeViewImageSet.WindowsHelp] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", null, null, null);
  113. imageStyles [TreeViewImageSet.XPFileExplorer] = new ImageStyle ("TreeView_plus", "TreeView_minus", "TreeView_noexpand", null, null, null);
  114. }
  115. public event TreeNodeEventHandler TreeNodeCheckChanged {
  116. add { Events.AddHandler (TreeNodeCheckChangedEvent, value); }
  117. remove { Events.RemoveHandler (TreeNodeCheckChangedEvent, value); }
  118. }
  119. public event EventHandler SelectedNodeChanged {
  120. add { Events.AddHandler (SelectedNodeChangedEvent, value); }
  121. remove { Events.RemoveHandler (SelectedNodeChangedEvent, value); }
  122. }
  123. public event TreeNodeEventHandler TreeNodeCollapsed {
  124. add { Events.AddHandler (TreeNodeCollapsedEvent, value); }
  125. remove { Events.RemoveHandler (TreeNodeCollapsedEvent, value); }
  126. }
  127. public event TreeNodeEventHandler TreeNodeDataBound {
  128. add { Events.AddHandler (TreeNodeDataBoundEvent, value); }
  129. remove { Events.RemoveHandler (TreeNodeDataBoundEvent, value); }
  130. }
  131. public event TreeNodeEventHandler TreeNodeExpanded {
  132. add { Events.AddHandler (TreeNodeExpandedEvent, value); }
  133. remove { Events.RemoveHandler (TreeNodeExpandedEvent, value); }
  134. }
  135. public event TreeNodeEventHandler TreeNodePopulate {
  136. add { Events.AddHandler (TreeNodePopulateEvent, value); }
  137. remove { Events.RemoveHandler (TreeNodePopulateEvent, value); }
  138. }
  139. protected virtual void OnTreeNodeCheckChanged (TreeNodeEventArgs e)
  140. {
  141. if (Events != null) {
  142. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeCheckChangedEvent];
  143. if (eh != null) eh (this, e);
  144. }
  145. }
  146. protected virtual void OnSelectedNodeChanged (EventArgs e)
  147. {
  148. if (Events != null) {
  149. EventHandler eh = (EventHandler) Events [SelectedNodeChangedEvent];
  150. if (eh != null) eh (this, e);
  151. }
  152. }
  153. protected virtual void OnTreeNodeCollapsed (TreeNodeEventArgs e)
  154. {
  155. if (Events != null) {
  156. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeCollapsedEvent];
  157. if (eh != null) eh (this, e);
  158. }
  159. }
  160. protected virtual void OnTreeNodeDataBound (TreeNodeEventArgs e)
  161. {
  162. if (Events != null) {
  163. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeDataBoundEvent];
  164. if (eh != null) eh (this, e);
  165. }
  166. }
  167. protected virtual void OnTreeNodeExpanded (TreeNodeEventArgs e)
  168. {
  169. if (Events != null) {
  170. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodeExpandedEvent];
  171. if (eh != null) eh (this, e);
  172. }
  173. }
  174. protected virtual void OnTreeNodePopulate (TreeNodeEventArgs e)
  175. {
  176. if (Events != null) {
  177. TreeNodeEventHandler eh = (TreeNodeEventHandler) Events [TreeNodePopulateEvent];
  178. if (eh != null) eh (this, e);
  179. }
  180. }
  181. [Localizable (true)]
  182. public string CollapseImageToolTip {
  183. get {
  184. return ViewState.GetString ("CollapseImageToolTip", "Collapse {0}");
  185. }
  186. set {
  187. ViewState["CollapseImageToolTip"] = value;
  188. }
  189. }
  190. [MonoTODO ("Implement support for this")]
  191. [WebCategory ("Behavior")]
  192. [WebSysDescription ("Whether the tree will automatically generate bindings.")]
  193. [DefaultValue (true)]
  194. public bool AutoGenerateDataBindings {
  195. get {
  196. return ViewState.GetBool ("AutoGenerateDataBindings", true);
  197. }
  198. set {
  199. ViewState["AutoGenerateDataBindings"] = value;
  200. }
  201. }
  202. [DefaultValue ("")]
  203. [WebSysDescription ("The url of the image to show when a node can be collapsed.")]
  204. [UrlProperty]
  205. [WebCategory ("Appearance")]
  206. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  207. public string CollapseImageUrl {
  208. get {
  209. return ViewState.GetString ("CollapseImageUrl", "");
  210. }
  211. set {
  212. ViewState["CollapseImageUrl"] = value;
  213. }
  214. }
  215. [WebCategory ("Data")]
  216. [PersistenceMode (PersistenceMode.InnerProperty)]
  217. [WebSysDescription ("Bindings for tree nodes.")]
  218. [Editor ("System.Web.UI.Design.WebControls.TreeViewBindingsEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  219. [DefaultValue (null)]
  220. [MergablePropertyAttribute (false)]
  221. public TreeNodeBindingCollection DataBindings {
  222. get {
  223. if (dataBindings == null) {
  224. dataBindings = new TreeNodeBindingCollection ();
  225. if (IsTrackingViewState)
  226. ((IStateManager)dataBindings).TrackViewState();
  227. }
  228. return dataBindings;
  229. }
  230. }
  231. [WebCategory ("Behavior")]
  232. [WebSysDescription ("Whether the tree view can use client-side script to expand and collapse nodes.")]
  233. [Themeable (false)]
  234. [DefaultValue (true)]
  235. public bool EnableClientScript {
  236. get {
  237. return ViewState.GetBool ("EnableClientScript", true);
  238. }
  239. set {
  240. ViewState["EnableClientScript"] = value;
  241. }
  242. }
  243. [DefaultValue (-1)]
  244. [WebCategory ("Behavior")]
  245. [WebSysDescription ("The initial expand depth.")]
  246. [TypeConverter ("System.Web.UI.WebControls.TreeView+TreeViewExpandDepthConverter, " + Consts.AssemblySystem_Web)]
  247. public int ExpandDepth {
  248. get {
  249. return ViewState.GetInt ("ExpandDepth", -1);
  250. }
  251. set {
  252. ViewState["ExpandDepth"] = value;
  253. }
  254. }
  255. [Localizable (true)]
  256. public string ExpandImageToolTip {
  257. get {
  258. return ViewState.GetString ("ExpandImageToolTip", "Expand {0}");
  259. }
  260. set {
  261. ViewState["ExpandImageToolTip"] = value;
  262. }
  263. }
  264. [DefaultValue ("")]
  265. [UrlProperty]
  266. [WebSysDescription ("The url of the image to show when a node can be expanded.")]
  267. [WebCategory ("Appearance")]
  268. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  269. public string ExpandImageUrl {
  270. get {
  271. return ViewState.GetString ("ExpandImageUrl", "");
  272. }
  273. set {
  274. ViewState["ExpandImageUrl"] = value;
  275. }
  276. }
  277. [PersistenceMode (PersistenceMode.InnerProperty)]
  278. [NotifyParentProperty (true)]
  279. [DefaultValue (null)]
  280. [WebCategory ("Styles")]
  281. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  282. public Style HoverNodeStyle {
  283. get {
  284. if (hoverNodeStyle == null) {
  285. hoverNodeStyle = new Style();
  286. if (IsTrackingViewState)
  287. hoverNodeStyle.TrackViewState();
  288. }
  289. return hoverNodeStyle;
  290. }
  291. }
  292. [DefaultValue (TreeViewImageSet.Custom)]
  293. public TreeViewImageSet ImageSet {
  294. get {
  295. return (TreeViewImageSet)ViewState.GetInt ("ImageSet", (int)TreeViewImageSet.Custom);
  296. }
  297. set {
  298. if (!Enum.IsDefined (typeof (TreeViewImageSet), value))
  299. throw new ArgumentOutOfRangeException ();
  300. ViewState["ImageSet"] = value;
  301. }
  302. }
  303. [PersistenceMode (PersistenceMode.InnerProperty)]
  304. [NotifyParentProperty (true)]
  305. [DefaultValue (null)]
  306. [WebCategory ("Styles")]
  307. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  308. public TreeNodeStyle LeafNodeStyle {
  309. get {
  310. if (leafNodeStyle == null) {
  311. leafNodeStyle = new TreeNodeStyle ();
  312. if (IsTrackingViewState)
  313. leafNodeStyle.TrackViewState();
  314. }
  315. return leafNodeStyle;
  316. }
  317. }
  318. [DefaultValue (null)]
  319. [WebCategory ("Styles")]
  320. [PersistenceMode (PersistenceMode.InnerProperty)]
  321. [Editor ("System.Web.UI.Design.WebControls.TreeNodeStyleCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  322. public TreeNodeStyleCollection LevelStyles {
  323. get {
  324. if (levelStyles == null) {
  325. levelStyles = new TreeNodeStyleCollection ();
  326. if (IsTrackingViewState)
  327. ((IStateManager)levelStyles).TrackViewState();
  328. }
  329. return levelStyles;
  330. }
  331. }
  332. [DefaultValue ("")]
  333. public string LineImagesFolder {
  334. get {
  335. return ViewState.GetString ("LineImagesFolder", "");
  336. }
  337. set {
  338. ViewState["LineImagesFolder"] = value;
  339. }
  340. }
  341. [DefaultValue (-1)]
  342. public int MaxDataBindDepth {
  343. get {
  344. return ViewState.GetInt ("MaxDataBindDepth", -1);
  345. }
  346. set {
  347. ViewState["MaxDataBindDepth"] = value;
  348. }
  349. }
  350. [DefaultValue (20)]
  351. public int NodeIndent {
  352. get {
  353. return ViewState.GetInt ("NodeIndent", 20);
  354. }
  355. set {
  356. ViewState["NodeIndent"] = value;
  357. }
  358. }
  359. [PersistenceMode (PersistenceMode.InnerProperty)]
  360. [Editor ("System.Web.UI.Design.WebControls.TreeNodeCollectionEditor," + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  361. [DefaultValueAttribute (null)]
  362. [MergablePropertyAttribute (false)]
  363. public TreeNodeCollection Nodes {
  364. get {
  365. if (nodes == null) {
  366. nodes = new TreeNodeCollection (this);
  367. if (IsTrackingViewState)
  368. ((IStateManager)nodes).TrackViewState();
  369. }
  370. return nodes;
  371. }
  372. }
  373. [PersistenceMode (PersistenceMode.InnerProperty)]
  374. [NotifyParentProperty (true)]
  375. [DefaultValue (null)]
  376. [WebCategory ("Styles")]
  377. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  378. public TreeNodeStyle NodeStyle {
  379. get {
  380. if (nodeStyle == null) {
  381. nodeStyle = new TreeNodeStyle ();
  382. if (IsTrackingViewState)
  383. nodeStyle.TrackViewState();
  384. }
  385. return nodeStyle;
  386. }
  387. }
  388. [DefaultValue (false)]
  389. public bool NodeWrap {
  390. get {
  391. return ViewState.GetBool ("NodeWrap", false);
  392. }
  393. set {
  394. ViewState ["NodeWrap"] = value;
  395. }
  396. }
  397. [UrlProperty]
  398. [DefaultValue ("")]
  399. [WebSysDescription ("The url of the image to show for leaf nodes.")]
  400. [WebCategory ("Appearance")]
  401. [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
  402. public string NoExpandImageUrl {
  403. get {
  404. return ViewState.GetString ("NoExpandImageUrl", "");
  405. }
  406. set {
  407. ViewState ["NoExpandImageUrl"] = value;
  408. }
  409. }
  410. [PersistenceMode (PersistenceMode.InnerProperty)]
  411. [NotifyParentProperty (true)]
  412. [DefaultValue (null)]
  413. [WebCategory ("Styles")]
  414. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  415. public TreeNodeStyle ParentNodeStyle {
  416. get {
  417. if (parentNodeStyle == null) {
  418. parentNodeStyle = new TreeNodeStyle ();
  419. if (IsTrackingViewState)
  420. parentNodeStyle.TrackViewState();
  421. }
  422. return parentNodeStyle;
  423. }
  424. }
  425. [DefaultValue ('/')]
  426. public char PathSeparator {
  427. get {
  428. return ViewState.GetChar ("PathSeparator", '/');
  429. }
  430. set {
  431. ViewState ["PathSeparator"] = value;
  432. }
  433. }
  434. [DefaultValue (true)]
  435. public bool PopulateNodesFromClient {
  436. get {
  437. return ViewState.GetBool ("PopulateNodesFromClient", true);
  438. }
  439. set {
  440. ViewState ["PopulateNodesFromClient"] = value;
  441. }
  442. }
  443. [PersistenceMode (PersistenceMode.InnerProperty)]
  444. [NotifyParentProperty (true)]
  445. [DefaultValue (null)]
  446. [WebCategory ("Styles")]
  447. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  448. public TreeNodeStyle RootNodeStyle {
  449. get {
  450. if (rootNodeStyle == null) {
  451. rootNodeStyle = new TreeNodeStyle ();
  452. if (IsTrackingViewState)
  453. rootNodeStyle.TrackViewState();
  454. }
  455. return rootNodeStyle;
  456. }
  457. }
  458. [PersistenceMode (PersistenceMode.InnerProperty)]
  459. [NotifyParentProperty (true)]
  460. [DefaultValue (null)]
  461. [WebCategory ("Styles")]
  462. [DesignerSerializationVisibility (DesignerSerializationVisibility.Content)]
  463. public TreeNodeStyle SelectedNodeStyle {
  464. get {
  465. if (selectedNodeStyle == null) {
  466. selectedNodeStyle = new TreeNodeStyle ();
  467. if (IsTrackingViewState)
  468. selectedNodeStyle.TrackViewState();
  469. }
  470. return selectedNodeStyle;
  471. }
  472. }
  473. private Style ControlLinkStyle {
  474. get {
  475. if (controlLinkStyle == null) {
  476. controlLinkStyle = new Style ();
  477. controlLinkStyle.AlwaysRenderTextDecoration = true;
  478. }
  479. return controlLinkStyle;
  480. }
  481. }
  482. private Style NodeLinkStyle {
  483. get {
  484. if (nodeLinkStyle == null) {
  485. nodeLinkStyle = new Style ();
  486. }
  487. return nodeLinkStyle;
  488. }
  489. }
  490. private Style RootNodeLinkStyle {
  491. get {
  492. if (rootNodeLinkStyle == null) {
  493. rootNodeLinkStyle = new Style ();
  494. }
  495. return rootNodeLinkStyle;
  496. }
  497. }
  498. private Style ParentNodeLinkStyle {
  499. get {
  500. if (parentNodeLinkStyle == null) {
  501. parentNodeLinkStyle = new Style ();
  502. }
  503. return parentNodeLinkStyle;
  504. }
  505. }
  506. private Style SelectedNodeLinkStyle {
  507. get {
  508. if (selectedNodeLinkStyle == null) {
  509. selectedNodeLinkStyle = new Style ();
  510. }
  511. return selectedNodeLinkStyle;
  512. }
  513. }
  514. private Style LeafNodeLinkStyle {
  515. get {
  516. if (leafNodeLinkStyle == null) {
  517. leafNodeLinkStyle = new Style ();
  518. }
  519. return leafNodeLinkStyle;
  520. }
  521. }
  522. private Style HoverNodeLinkStyle {
  523. get {
  524. if (hoverNodeLinkStyle == null) {
  525. hoverNodeLinkStyle = new Style ();
  526. }
  527. return hoverNodeLinkStyle;
  528. }
  529. }
  530. [DefaultValue (TreeNodeTypes.None)]
  531. public TreeNodeTypes ShowCheckBoxes {
  532. get {
  533. return (TreeNodeTypes)ViewState.GetInt ("ShowCheckBoxes", (int)TreeNodeTypes.None);
  534. }
  535. set {
  536. if ((int) value > 7)
  537. throw new ArgumentOutOfRangeException ();
  538. ViewState ["ShowCheckBoxes"] = value;
  539. }
  540. }
  541. [DefaultValue (true)]
  542. public bool ShowExpandCollapse {
  543. get {
  544. return ViewState.GetBool ("ShowExpandCollapse", true);
  545. }
  546. set {
  547. ViewState ["ShowExpandCollapse"] = value;
  548. }
  549. }
  550. [DefaultValue (false)]
  551. public bool ShowLines {
  552. get {
  553. return ViewState.GetBool ("ShowLines", false);
  554. }
  555. set {
  556. ViewState ["ShowLines"] = value;
  557. }
  558. }
  559. [Localizable (true)]
  560. public string SkipLinkText
  561. {
  562. get {
  563. return ViewState.GetString ("SkipLinkText", "Skip Navigation Links.");
  564. }
  565. set {
  566. ViewState ["SkipLinkText"] = value;
  567. }
  568. }
  569. [Browsable (false)]
  570. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  571. public TreeNode SelectedNode {
  572. get { return selectedNode; }
  573. }
  574. [Browsable (false)]
  575. [DefaultValue ("")]
  576. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  577. public string SelectedValue {
  578. get { return selectedNode != null ? selectedNode.Value : ""; }
  579. }
  580. [DefaultValue ("")]
  581. public string Target {
  582. get {
  583. return ViewState.GetString ("Target", "");
  584. }
  585. set {
  586. ViewState ["Target"] = value;
  587. }
  588. }
  589. [MonoTODO ("why override?")]
  590. public override bool Visible
  591. {
  592. get {
  593. return base.Visible;
  594. }
  595. set {
  596. base.Visible = value;
  597. }
  598. }
  599. [Browsable (false)]
  600. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  601. public TreeNodeCollection CheckedNodes {
  602. get {
  603. TreeNodeCollection col = new TreeNodeCollection ();
  604. FindCheckedNodes (Nodes, col);
  605. return col;
  606. }
  607. }
  608. void FindCheckedNodes (TreeNodeCollection nodeList, TreeNodeCollection result)
  609. {
  610. foreach (TreeNode node in nodeList) {
  611. if (node.Checked) result.Add (node, false);
  612. FindCheckedNodes (node.ChildNodes, result);
  613. }
  614. }
  615. public void ExpandAll ()
  616. {
  617. foreach (TreeNode node in Nodes)
  618. node.ExpandAll ();
  619. }
  620. public void CollapseAll ()
  621. {
  622. foreach (TreeNode node in Nodes)
  623. node.CollapseAll ();
  624. }
  625. public TreeNode FindNode (string valuePath)
  626. {
  627. if (valuePath == null) throw new ArgumentNullException ("valuePath");
  628. string[] path = valuePath.Split (PathSeparator);
  629. int n = 0;
  630. TreeNodeCollection col = Nodes;
  631. bool foundBranch = true;
  632. while (col.Count > 0 && foundBranch) {
  633. foundBranch = false;
  634. foreach (TreeNode node in col) {
  635. if (node.Value == path [n]) {
  636. if (++n == path.Length) return node;
  637. col = node.ChildNodes;
  638. foundBranch = true;
  639. break;
  640. }
  641. }
  642. }
  643. return null;
  644. }
  645. ImageStyle GetImageStyle ()
  646. {
  647. if (ImageSet != TreeViewImageSet.Custom)
  648. return (ImageStyle) imageStyles [ImageSet];
  649. else
  650. return null;
  651. }
  652. protected override HtmlTextWriterTag TagKey {
  653. get { return HtmlTextWriterTag.Div; }
  654. }
  655. protected internal virtual TreeNode CreateNode ()
  656. {
  657. return new TreeNode (this);
  658. }
  659. public sealed override void DataBind ()
  660. {
  661. base.DataBind ();
  662. }
  663. protected void SetNodeDataBound (TreeNode node, bool dataBound)
  664. {
  665. node.SetDataBound (dataBound);
  666. }
  667. protected void SetNodeDataPath (TreeNode node, string dataPath)
  668. {
  669. node.SetDataPath (dataPath);
  670. }
  671. protected void SetNodeDataItem (TreeNode node, object dataItem)
  672. {
  673. node.SetDataItem (dataItem);
  674. }
  675. protected internal override void OnInit (EventArgs e)
  676. {
  677. base.OnInit (e);
  678. }
  679. internal void SetSelectedNode (TreeNode node, bool loading)
  680. {
  681. if (selectedNode == node) return;
  682. if (selectedNode != null)
  683. selectedNode.SelectedFlag = false;
  684. selectedNode = node;
  685. if (!loading)
  686. OnSelectedNodeChanged (new TreeNodeEventArgs (selectedNode));
  687. }
  688. internal void NotifyCheckChanged (TreeNode node)
  689. {
  690. OnTreeNodeCheckChanged (new TreeNodeEventArgs (node));
  691. }
  692. internal void NotifyExpandedChanged (TreeNode node)
  693. {
  694. if (node.Expanded.HasValue && node.Expanded.Value)
  695. OnTreeNodeExpanded (new TreeNodeEventArgs (node));
  696. else if (node.Expanded.HasValue && node.IsParentNode)
  697. OnTreeNodeCollapsed (new TreeNodeEventArgs (node));
  698. }
  699. internal void NotifyPopulateRequired (TreeNode node)
  700. {
  701. OnTreeNodePopulate (new TreeNodeEventArgs (node));
  702. }
  703. protected override void TrackViewState()
  704. {
  705. EnsureDataBound ();
  706. base.TrackViewState();
  707. if (hoverNodeStyle != null) {
  708. hoverNodeStyle.TrackViewState();
  709. }
  710. if (leafNodeStyle != null) {
  711. leafNodeStyle.TrackViewState();
  712. }
  713. if (levelStyles != null && levelStyles.Count > 0) {
  714. ((IStateManager)levelStyles).TrackViewState();
  715. }
  716. if (nodeStyle != null) {
  717. nodeStyle.TrackViewState();
  718. }
  719. if (parentNodeStyle != null) {
  720. parentNodeStyle.TrackViewState();
  721. }
  722. if (rootNodeStyle != null) {
  723. rootNodeStyle.TrackViewState();
  724. }
  725. if (selectedNodeStyle != null) {
  726. selectedNodeStyle.TrackViewState();
  727. }
  728. if (dataBindings != null) {
  729. ((IStateManager)dataBindings).TrackViewState ();
  730. }
  731. if (nodes != null) {
  732. ((IStateManager)nodes).TrackViewState();;
  733. }
  734. }
  735. protected override object SaveViewState()
  736. {
  737. object[] states = new object [10];
  738. states[0] = base.SaveViewState();
  739. states[1] = (hoverNodeStyle == null ? null : hoverNodeStyle.SaveViewState());
  740. states[2] = (leafNodeStyle == null ? null : leafNodeStyle.SaveViewState());
  741. states[3] = (levelStyles == null ? null : ((IStateManager)levelStyles).SaveViewState());
  742. states[4] = (nodeStyle == null ? null : nodeStyle.SaveViewState());
  743. states[5] = (parentNodeStyle == null ? null : parentNodeStyle.SaveViewState());
  744. states[6] = (rootNodeStyle == null ? null : rootNodeStyle.SaveViewState());
  745. states[7] = (selectedNodeStyle == null ? null : selectedNodeStyle.SaveViewState());
  746. states[8] = (dataBindings == null ? null : ((IStateManager)dataBindings).SaveViewState());
  747. states[9] = (nodes == null ? null : ((IStateManager)nodes).SaveViewState());
  748. for (int i = states.Length - 1; i >= 0; i--) {
  749. if (states [i] != null)
  750. return states;
  751. }
  752. return null;
  753. }
  754. protected override void LoadViewState (object savedState)
  755. {
  756. if (savedState == null)
  757. return;
  758. object [] states = (object []) savedState;
  759. base.LoadViewState (states[0]);
  760. if (states[1] != null)
  761. HoverNodeStyle.LoadViewState (states[1]);
  762. if (states[2] != null)
  763. LeafNodeStyle.LoadViewState(states[2]);
  764. if (states[3] != null)
  765. ((IStateManager)LevelStyles).LoadViewState(states[3]);
  766. if (states[4] != null)
  767. NodeStyle.LoadViewState(states[4]);
  768. if (states[5] != null)
  769. ParentNodeStyle.LoadViewState(states[5]);
  770. if (states[6] != null)
  771. RootNodeStyle.LoadViewState(states[6]);
  772. if (states[7] != null)
  773. SelectedNodeStyle.LoadViewState(states[7]);
  774. if (states[8] != null)
  775. ((IStateManager)DataBindings).LoadViewState(states[8]);
  776. if (states[9] != null)
  777. ((IStateManager)Nodes).LoadViewState(states[9]);
  778. }
  779. protected virtual void RaisePostBackEvent (string eventArgument)
  780. {
  781. string[] args = eventArgument.Split ('|');
  782. TreeNode node = FindNodeByPos (args[1]);
  783. if (node == null) return;
  784. if (args [0] == "sel")
  785. HandleSelectEvent (node);
  786. else if (args [0] == "ec")
  787. HandleExpandCollapseEvent (node);
  788. }
  789. void HandleSelectEvent (TreeNode node)
  790. {
  791. switch (node.SelectAction) {
  792. case TreeNodeSelectAction.Select:
  793. node.Select ();
  794. break;
  795. case TreeNodeSelectAction.Expand:
  796. node.Expand ();
  797. break;
  798. case TreeNodeSelectAction.SelectExpand:
  799. node.Select ();
  800. node.Expand ();
  801. break;
  802. }
  803. }
  804. void HandleExpandCollapseEvent (TreeNode node)
  805. {
  806. node.ToggleExpandState ();
  807. }
  808. protected virtual void RaisePostDataChangedEvent ()
  809. {
  810. }
  811. string callbackResult;
  812. protected virtual void RaiseCallbackEvent (string eventArgs)
  813. {
  814. RequiresDataBinding = true;
  815. EnsureDataBound ();
  816. TreeNode node = FindNodeByPos (eventArgs);
  817. ArrayList levelLines = new ArrayList ();
  818. TreeNode nd = node;
  819. while (nd != null) {
  820. int childCount = nd.Parent != null ? nd.Parent.ChildNodes.Count : Nodes.Count;
  821. levelLines.Insert (0, (nd.Index < childCount - 1) ? this : null);
  822. nd = nd.Parent;
  823. }
  824. StringWriter sw = new StringWriter ();
  825. HtmlTextWriter writer = new HtmlTextWriter (sw);
  826. EnsureStylesPrepared ();
  827. node.Expanded = true;
  828. int num = node.ChildNodes.Count;
  829. for (int n=0; n<num; n++)
  830. RenderNode (writer, node.ChildNodes [n], node.Depth + 1, levelLines, true, n<num-1);
  831. string res = sw.ToString ();
  832. callbackResult = res != "" ? res : "*";
  833. }
  834. protected virtual string GetCallbackResult ()
  835. {
  836. return callbackResult;
  837. }
  838. void IPostBackEventHandler.RaisePostBackEvent (string eventArgument)
  839. {
  840. RaisePostBackEvent (eventArgument);
  841. }
  842. bool IPostBackDataHandler.LoadPostData (string postDataKey, NameValueCollection postCollection)
  843. {
  844. return LoadPostData (postDataKey, postCollection);
  845. }
  846. void IPostBackDataHandler.RaisePostDataChangedEvent ()
  847. {
  848. RaisePostDataChangedEvent ();
  849. }
  850. void ICallbackEventHandler.RaiseCallbackEvent (string eventArgs)
  851. {
  852. RaiseCallbackEvent (eventArgs);
  853. }
  854. string ICallbackEventHandler.GetCallbackResult ()
  855. {
  856. return GetCallbackResult ();
  857. }
  858. protected override ControlCollection CreateControlCollection ()
  859. {
  860. return new EmptyControlCollection (this);
  861. }
  862. protected internal override void PerformDataBinding ()
  863. {
  864. base.PerformDataBinding ();
  865. InitializeDataBindings ();
  866. HierarchicalDataSourceView data = GetData ("");
  867. if (data == null)
  868. return;
  869. Nodes.Clear ();
  870. IHierarchicalEnumerable e = data.Select ();
  871. FillBoundChildrenRecursive (e, Nodes);
  872. }
  873. private void FillBoundChildrenRecursive (IHierarchicalEnumerable hEnumerable, TreeNodeCollection nodeCollection) {
  874. foreach (object obj in hEnumerable) {
  875. IHierarchyData hdata = hEnumerable.GetHierarchyData (obj);
  876. TreeNode child = new TreeNode ();
  877. nodeCollection.Add (child);
  878. child.Bind (hdata);
  879. OnTreeNodeDataBound (new TreeNodeEventArgs (child));
  880. if (MaxDataBindDepth >= 0 && child.Depth == MaxDataBindDepth)
  881. continue;
  882. if (hdata == null || !hdata.HasChildren)
  883. continue;
  884. IHierarchicalEnumerable e = hdata.GetChildren ();
  885. FillBoundChildrenRecursive (e, child.ChildNodes);
  886. }
  887. }
  888. protected virtual bool LoadPostData (string postDataKey, NameValueCollection postCollection)
  889. {
  890. bool res = false;
  891. if (EnableClientScript && PopulateNodesFromClient) {
  892. string states = postCollection [ClientID + "_PopulatedStates"];
  893. if (states != null) {
  894. foreach (string id in states.Split ('|')) {
  895. if (String.IsNullOrEmpty(id))
  896. continue;
  897. TreeNode node = FindNodeByPos (id);
  898. if (node != null && node.PopulateOnDemand && !node.Populated)
  899. node.Populate();
  900. }
  901. }
  902. res = true;
  903. }
  904. if (ShowCheckBoxes != TreeNodeTypes.None) {
  905. UnsetCheckStates (Nodes, postCollection);
  906. SetCheckStates (postCollection);
  907. res = true;
  908. }
  909. if (EnableClientScript) {
  910. string states = postCollection [ClientID + "_ExpandStates"];
  911. if (states != null) {
  912. string[] ids = states.Split ('|');
  913. UnsetExpandStates (Nodes, ids);
  914. SetExpandStates (ids);
  915. }
  916. else
  917. UnsetExpandStates (Nodes, new string[0]);
  918. res = true;
  919. }
  920. return res;
  921. }
  922. protected internal override void OnPreRender (EventArgs e)
  923. {
  924. base.OnPreRender (e);
  925. if (Page != null) {
  926. if (Enabled)
  927. Page.RegisterRequiresPostBack (this);
  928. if (EnableClientScript && !Page.ClientScript.IsClientScriptIncludeRegistered (typeof(TreeView), "TreeView.js")) {
  929. string url = Page.ClientScript.GetWebResourceUrl (typeof(TreeView), "TreeView.js");
  930. Page.ClientScript.RegisterClientScriptInclude (typeof(TreeView), "TreeView.js", url);
  931. }
  932. }
  933. string ctree = ClientID + "_data";
  934. string script = string.Format ("var {0} = new Object ();\n", ctree);
  935. script += string.Format ("{0}.showImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (ShowExpandCollapse));
  936. if (ShowExpandCollapse) {
  937. bool defaultImages = ShowLines || ImageSet != TreeViewImageSet.Custom || (ExpandImageUrl == "" && CollapseImageUrl == "");
  938. script += string.Format ("{0}.defaultImages = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (defaultImages));
  939. ImageStyle imageStyle = GetImageStyle ();
  940. if (!defaultImages) {
  941. script += string.Format ("{0}.expandImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("plus", imageStyle)));
  942. script += string.Format ("{0}.collapseImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("minus", imageStyle)));
  943. }
  944. if (PopulateNodesFromClient)
  945. script += string.Format ("{0}.noExpandImage = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageUrl ("noexpand", imageStyle)));
  946. }
  947. if (Page != null) {
  948. script += string.Format ("{0}.populateFromClient = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (PopulateNodesFromClient));
  949. script += string.Format ("{0}.expandAlt = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageToolTip (true, null)));
  950. script += string.Format ("{0}.collapseAlt = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (GetNodeImageToolTip (false, null)));
  951. if (!Page.IsPostBack) {
  952. SetNodesExpandedToDepthRecursive (Nodes);
  953. }
  954. if (EnableClientScript) {
  955. Page.ClientScript.RegisterHiddenField (ClientID + "_ExpandStates", GetExpandStates ());
  956. // Make sure the basic script infrastructure is rendered
  957. Page.ClientScript.GetCallbackEventReference (this, "null", "", "null");
  958. Page.ClientScript.GetPostBackClientHyperlink (this, "");
  959. }
  960. if (EnableClientScript && PopulateNodesFromClient) {
  961. Page.ClientScript.RegisterHiddenField (ClientID + "_PopulatedStates", "|");
  962. }
  963. EnsureStylesPrepared ();
  964. if (hoverNodeStyle != null) {
  965. if (Page.Header == null)
  966. throw new InvalidOperationException ("Using TreeView.HoverNodeStyle requires Page.Header to be non-null (e.g. <head runat=\"server\" />).");
  967. RegisterStyle (HoverNodeStyle, HoverNodeLinkStyle);
  968. script += string.Format ("{0}.hoverClass = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (HoverNodeStyle.RegisteredCssClass));
  969. script += string.Format ("{0}.hoverLinkClass = {1};\n", ctree, ClientScriptManager.GetScriptLiteral (HoverNodeLinkStyle.RegisteredCssClass));
  970. }
  971. Page.ClientScript.RegisterStartupScript (typeof(TreeView), this.UniqueID, script, true);
  972. }
  973. }
  974. void EnsureStylesPrepared () {
  975. if (stylesPrepared)
  976. return;
  977. stylesPrepared = true;
  978. PrepareStyles ();
  979. }
  980. private void PrepareStyles () {
  981. // The order in which styles are defined matters when more than one class
  982. // is assigned to an element
  983. ControlLinkStyle.CopyTextStylesFrom (ControlStyle);
  984. RegisterStyle (ControlLinkStyle);
  985. if (nodeStyle != null)
  986. RegisterStyle (NodeStyle, NodeLinkStyle);
  987. if (rootNodeStyle != null)
  988. RegisterStyle (RootNodeStyle, RootNodeLinkStyle);
  989. if (parentNodeStyle != null)
  990. RegisterStyle (ParentNodeStyle, ParentNodeLinkStyle);
  991. if (leafNodeStyle != null)
  992. RegisterStyle (LeafNodeStyle, LeafNodeLinkStyle);
  993. if (levelStyles != null && levelStyles.Count > 0) {
  994. levelLinkStyles = new List<Style> (levelStyles.Count);
  995. foreach (Style style in levelStyles) {
  996. Style linkStyle = new Style ();
  997. levelLinkStyles.Add (linkStyle);
  998. RegisterStyle (style, linkStyle);
  999. }
  1000. }
  1001. if (selectedNodeStyle != null)
  1002. RegisterStyle (SelectedNodeStyle, SelectedNodeLinkStyle);
  1003. }
  1004. void SetNodesExpandedToDepthRecursive (TreeNodeCollection nodes) {
  1005. foreach (TreeNode node in nodes) {
  1006. if (!node.Expanded.HasValue) {
  1007. if (ExpandDepth < 0 || node.Depth < ExpandDepth)
  1008. node.Expanded = true;
  1009. }
  1010. SetNodesExpandedToDepthRecursive (node.ChildNodes);
  1011. }
  1012. }
  1013. string IncrementStyleClassName () {
  1014. registeredStylesCounter++;
  1015. return ClientID + "_" + registeredStylesCounter;
  1016. }
  1017. void RegisterStyle (Style baseStyle, Style linkStyle) {
  1018. linkStyle.CopyTextStylesFrom (baseStyle);
  1019. linkStyle.BorderStyle = BorderStyle.None;
  1020. baseStyle.Font.Reset ();
  1021. RegisterStyle (linkStyle);
  1022. RegisterStyle (baseStyle);
  1023. }
  1024. void RegisterStyle (Style baseStyle) {
  1025. if (Page.Header == null)
  1026. return;
  1027. string className = IncrementStyleClassName ();
  1028. baseStyle.SetRegisteredCssClass (className);
  1029. Page.Header.StyleSheet.CreateStyleRule (baseStyle, this, "." + className);
  1030. }
  1031. string GetBindingKey (string dataMember, int depth)
  1032. {
  1033. return dataMember + " " + depth;
  1034. }
  1035. void InitializeDataBindings () {
  1036. if (dataBindings != null && dataBindings.Count > 0) {
  1037. bindings = new Hashtable ();
  1038. foreach (TreeNodeBinding bin in dataBindings) {
  1039. string key = GetBindingKey (bin.DataMember, bin.Depth);
  1040. bindings [key] = bin;
  1041. }
  1042. }
  1043. else
  1044. bindings = null;
  1045. }
  1046. internal TreeNodeBinding FindBindingForNode (string type, int depth)
  1047. {
  1048. if (bindings == null)
  1049. return null;
  1050. TreeNodeBinding bin = (TreeNodeBinding) bindings [GetBindingKey (type, depth)];
  1051. if (bin != null) return bin;
  1052. bin = (TreeNodeBinding) bindings [GetBindingKey (type, -1)];
  1053. if (bin != null) return bin;
  1054. bin = (TreeNodeBinding) bindings [GetBindingKey ("", depth)];
  1055. if (bin != null) return bin;
  1056. return (TreeNodeBinding) bindings [GetBindingKey ("", -1)];
  1057. }
  1058. internal void DecorateNode(TreeNode node)
  1059. {
  1060. if (node == null)
  1061. return;
  1062. if (node.ImageUrl != null && node.ImageUrl.Length > 0)
  1063. return;
  1064. if (node.IsRootNode && rootNodeStyle != null) {
  1065. node.ImageUrl = rootNodeStyle.ImageUrl;
  1066. return;
  1067. }
  1068. if (node.IsParentNode && parentNodeStyle != null) {
  1069. node.ImageUrl = parentNodeStyle.ImageUrl;
  1070. return;
  1071. }
  1072. if (node.IsLeafNode && leafNodeStyle != null)
  1073. node.ImageUrl = leafNodeStyle.ImageUrl;
  1074. }
  1075. protected internal override void RenderContents (HtmlTextWriter writer)
  1076. {
  1077. ArrayList levelLines = new ArrayList ();
  1078. int num = Nodes.Count;
  1079. for (int n=0; n<num; n++)
  1080. RenderNode (writer, Nodes [n], 0, levelLines, n>0, n<num-1);
  1081. }
  1082. protected override void AddAttributesToRender(HtmlTextWriter writer)
  1083. {
  1084. base.AddAttributesToRender (writer);
  1085. }
  1086. public override void RenderBeginTag (HtmlTextWriter writer)
  1087. {
  1088. if (SkipLinkText != "") {
  1089. writer.AddAttribute (HtmlTextWriterAttribute.Href, "#" + ClientID + "_SkipLink");
  1090. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1091. Image img = new Image ();
  1092. ClientScriptManager csm = new ClientScriptManager (null);
  1093. img.ImageUrl = csm.GetWebResourceUrl (typeof (SiteMapPath), "transparent.gif");
  1094. img.Attributes.Add ("height", "0");
  1095. img.Attributes.Add ("width", "0");
  1096. img.AlternateText = SkipLinkText;
  1097. img.Render (writer);
  1098. writer.RenderEndTag ();
  1099. }
  1100. base.RenderBeginTag (writer);
  1101. }
  1102. public override void RenderEndTag (HtmlTextWriter writer)
  1103. {
  1104. base.RenderEndTag (writer);
  1105. if (SkipLinkText != "") {
  1106. writer.AddAttribute (HtmlTextWriterAttribute.Id, ClientID + "_SkipLink");
  1107. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1108. writer.RenderEndTag ();
  1109. }
  1110. }
  1111. void RenderNode (HtmlTextWriter writer, TreeNode node, int level, ArrayList levelLines, bool hasPrevious, bool hasNext)
  1112. {
  1113. DecorateNode(node);
  1114. string nodeImage;
  1115. bool clientExpand = EnableClientScript && Events [TreeNodeCollapsedEvent] == null && Events [TreeNodeExpandedEvent] == null;
  1116. ImageStyle imageStyle = GetImageStyle ();
  1117. bool renderChildNodes = node.Expanded.HasValue && node.Expanded.Value;
  1118. if (clientExpand && !renderChildNodes)
  1119. renderChildNodes = (!node.PopulateOnDemand || node.Populated);
  1120. bool hasChildNodes;
  1121. if (renderChildNodes)
  1122. hasChildNodes = node.ChildNodes.Count > 0;
  1123. else
  1124. hasChildNodes = (node.PopulateOnDemand && !node.Populated) || node.ChildNodes.Count > 0;
  1125. writer.AddAttribute ("cellpadding", "0");
  1126. writer.AddAttribute ("cellspacing", "0");
  1127. writer.AddStyleAttribute ("border-width", "0");
  1128. writer.RenderBeginTag (HtmlTextWriterTag.Table);
  1129. Unit nodeSpacing = GetNodeSpacing (node);
  1130. if (nodeSpacing != Unit.Empty && (node.Depth > 0 || node.Index > 0))
  1131. RenderMenuItemSpacing (writer, nodeSpacing);
  1132. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1133. // Vertical lines from previous levels
  1134. nodeImage = GetNodeImageUrl ("i", imageStyle);
  1135. for (int n=0; n<level; n++) {
  1136. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1137. writer.AddStyleAttribute ("width", NodeIndent + "px");
  1138. writer.AddStyleAttribute ("height", "1px");
  1139. writer.RenderBeginTag (HtmlTextWriterTag.Div);
  1140. if (ShowLines && levelLines [n] != null) {
  1141. writer.AddAttribute ("src", nodeImage);
  1142. writer.AddAttribute (HtmlTextWriterAttribute.Alt, "");
  1143. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1144. writer.RenderEndTag ();
  1145. }
  1146. writer.RenderEndTag ();
  1147. writer.RenderEndTag (); // TD
  1148. }
  1149. // Node image + line
  1150. if (ShowExpandCollapse || ShowLines) {
  1151. bool buttonImage = false;
  1152. string tooltip = "";
  1153. string shape = "";
  1154. if (ShowLines) {
  1155. if (hasPrevious && hasNext) shape = "t";
  1156. else if (hasPrevious && !hasNext) shape = "l";
  1157. else if (!hasPrevious && hasNext) shape = "r";
  1158. else shape = "dash";
  1159. }
  1160. if (ShowExpandCollapse) {
  1161. if (hasChildNodes) {
  1162. buttonImage = true;
  1163. if (node.Expanded.HasValue && node.Expanded.Value) shape += "minus";
  1164. else shape += "plus";
  1165. tooltip = GetNodeImageToolTip (!(node.Expanded.HasValue && node.Expanded.Value), node.Text);
  1166. } else if (!ShowLines)
  1167. shape = "noexpand";
  1168. }
  1169. if (shape != "") {
  1170. nodeImage = GetNodeImageUrl (shape, imageStyle);
  1171. writer.RenderBeginTag (HtmlTextWriterTag.Td); // TD
  1172. if (buttonImage) {
  1173. if (!clientExpand || (!PopulateNodesFromClient && node.PopulateOnDemand && !node.Populated))
  1174. writer.AddAttribute ("href", GetClientEvent (node, "ec"));
  1175. else
  1176. writer.AddAttribute ("href", GetClientExpandEvent(node));
  1177. writer.RenderBeginTag (HtmlTextWriterTag.A); // Anchor
  1178. }
  1179. writer.AddAttribute ("alt", tooltip);
  1180. if (buttonImage && clientExpand)
  1181. writer.AddAttribute ("id", GetNodeClientId (node, "img"));
  1182. writer.AddAttribute ("src", nodeImage);
  1183. if (buttonImage)
  1184. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0");
  1185. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1186. writer.RenderEndTag ();
  1187. if (buttonImage)
  1188. writer.RenderEndTag (); // Anchor
  1189. writer.RenderEndTag (); // TD
  1190. }
  1191. }
  1192. // Node icon
  1193. string imageUrl = node.ImageUrl.Length > 0 ? ResolveClientUrl (node.ImageUrl) : null;
  1194. if (String.IsNullOrEmpty (imageUrl) && imageStyle != null) {
  1195. if (imageStyle.RootIcon != null && node.IsRootNode)
  1196. imageUrl = GetNodeIconUrl (imageStyle.RootIcon);
  1197. else if (imageStyle.ParentIcon != null && node.IsParentNode)
  1198. imageUrl = GetNodeIconUrl (imageStyle.ParentIcon);
  1199. else if (imageStyle.LeafIcon != null && node.IsLeafNode)
  1200. imageUrl = GetNodeIconUrl (imageStyle.LeafIcon);
  1201. }
  1202. if (!String.IsNullOrEmpty (imageUrl)) {
  1203. writer.RenderBeginTag (HtmlTextWriterTag.Td); // TD
  1204. BeginNodeTag (writer, node, clientExpand);
  1205. writer.AddAttribute ("src", imageUrl);
  1206. writer.AddStyleAttribute (HtmlTextWriterStyle.BorderWidth, "0");
  1207. writer.AddAttribute ("alt", node.ImageToolTip);
  1208. writer.RenderBeginTag (HtmlTextWriterTag.Img);
  1209. writer.RenderEndTag (); // IMG
  1210. writer.RenderEndTag (); // style tag
  1211. writer.RenderEndTag (); // TD
  1212. }
  1213. if (!NodeWrap)
  1214. writer.AddStyleAttribute ("white-space", "nowrap");
  1215. AddNodeStyle (writer, node, level);
  1216. if (EnableClientScript) {
  1217. writer.AddAttribute ("onmouseout", "TreeView_UnhoverNode(this)");
  1218. writer.AddAttribute ("onmouseover", "TreeView_HoverNode('" + ClientID + "', this)");
  1219. }
  1220. writer.RenderBeginTag (HtmlTextWriterTag.Td); // TD
  1221. // Checkbox
  1222. bool showChecks;
  1223. if (node.ShowCheckBox.HasValue)
  1224. showChecks = node.ShowCheckBox.Value;
  1225. else
  1226. showChecks = (ShowCheckBoxes == TreeNodeTypes.All) ||
  1227. ((ShowCheckBoxes & TreeNodeTypes.Leaf) > 0 && node.IsLeafNode) ||
  1228. ((ShowCheckBoxes & TreeNodeTypes.Parent) > 0 && node.IsParentNode && node.Parent != null) ||
  1229. ((ShowCheckBoxes & TreeNodeTypes.Root) > 0 && node.Parent == null && node.ChildNodes.Count > 0);
  1230. if (showChecks) {
  1231. writer.AddAttribute ("name", ClientID + "_cs_" + node.Path);
  1232. writer.AddAttribute ("type", "checkbox");
  1233. writer.AddAttribute ("title", node.Text);
  1234. if (node.Checked) writer.AddAttribute ("checked", "checked");
  1235. writer.RenderBeginTag (HtmlTextWriterTag.Input); // INPUT
  1236. writer.RenderEndTag (); // INPUT
  1237. }
  1238. // Text
  1239. node.BeginRenderText (writer);
  1240. if (clientExpand)
  1241. writer.AddAttribute ("id", GetNodeClientId (node, "txt"));
  1242. AddNodeLinkStyle (writer, node, level);
  1243. BeginNodeTag (writer, node, clientExpand);
  1244. writer.Write (node.Text);
  1245. writer.RenderEndTag (); // style tag
  1246. node.EndRenderText (writer);
  1247. writer.RenderEndTag (); // TD
  1248. writer.RenderEndTag (); // TR
  1249. if (nodeSpacing != Unit.Empty)
  1250. RenderMenuItemSpacing (writer, nodeSpacing);
  1251. writer.RenderEndTag (); // TABLE
  1252. // Children
  1253. if (hasChildNodes)
  1254. {
  1255. if (level >= levelLines.Count) {
  1256. if (hasNext) levelLines.Add (this);
  1257. else levelLines.Add (null);
  1258. } else {
  1259. if (hasNext) levelLines [level] = this;
  1260. else levelLines [level] = null;
  1261. }
  1262. if (clientExpand) {
  1263. if (!(node.Expanded.HasValue && node.Expanded.Value)) writer.AddStyleAttribute ("display", "none");
  1264. else writer.AddStyleAttribute ("display", "block");
  1265. writer.AddAttribute ("id", GetNodeClientId (node, null));
  1266. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  1267. if (renderChildNodes) {
  1268. int num = node.ChildNodes.Count;
  1269. for (int n=0; n<num; n++)
  1270. RenderNode (writer, node.ChildNodes [n], level + 1, levelLines, true, n<num-1);
  1271. }
  1272. writer.RenderEndTag (); // SPAN
  1273. }
  1274. else if (renderChildNodes) {
  1275. int num = node.ChildNodes.Count;
  1276. for (int n=0; n<num; n++)
  1277. RenderNode (writer, node.ChildNodes [n], level + 1, levelLines, true, n<num-1);
  1278. }
  1279. }
  1280. }
  1281. private void RenderMenuItemSpacing (HtmlTextWriter writer, Unit itemSpacing) {
  1282. writer.AddStyleAttribute ("height", itemSpacing.ToString ());
  1283. writer.RenderBeginTag (HtmlTextWriterTag.Tr);
  1284. writer.RenderBeginTag (HtmlTextWriterTag.Td);
  1285. writer.RenderEndTag ();
  1286. writer.RenderEndTag ();
  1287. }
  1288. private Unit GetNodeSpacing (TreeNode node) {
  1289. if (node.Selected && selectedNodeStyle != null && selectedNodeStyle.NodeSpacing != Unit.Empty) {
  1290. return selectedNodeStyle.NodeSpacing;
  1291. }
  1292. if (levelStyles != null && node.Depth < levelStyles.Count && levelStyles [node.Depth].NodeSpacing != Unit.Empty) {
  1293. return levelStyles [node.Depth].NodeSpacing;
  1294. }
  1295. if (node.IsLeafNode) {
  1296. if (leafNodeStyle != null && leafNodeStyle.NodeSpacing != Unit.Empty)
  1297. return leafNodeStyle.NodeSpacing;
  1298. }
  1299. else if (node.IsRootNode) {
  1300. if (rootNodeStyle != null && rootNodeStyle.NodeSpacing != Unit.Empty)
  1301. return rootNodeStyle.NodeSpacing;
  1302. }
  1303. else if (node.IsParentNode) {
  1304. if (parentNodeStyle != null && parentNodeStyle.NodeSpacing != Unit.Empty)
  1305. return parentNodeStyle.NodeSpacing;
  1306. }
  1307. if (nodeStyle != null)
  1308. return nodeStyle.NodeSpacing;
  1309. else
  1310. return Unit.Empty;
  1311. }
  1312. void AddNodeStyle (HtmlTextWriter writer, TreeNode node, int level)
  1313. {
  1314. TreeNodeStyle style = new TreeNodeStyle ();
  1315. if (Page.Header != null) {
  1316. // styles are registered
  1317. if (nodeStyle != null) {
  1318. style.AddCssClass (nodeStyle.CssClass);
  1319. style.AddCssClass (nodeStyle.RegisteredCssClass);
  1320. }
  1321. if (node.IsLeafNode) {
  1322. if (leafNodeStyle != null) {
  1323. style.AddCssClass (leafNodeStyle.CssClass);
  1324. style.AddCssClass (leafNodeStyle.RegisteredCssClass);
  1325. }
  1326. }
  1327. else if (node.IsRootNode) {
  1328. if (rootNodeStyle != null) {
  1329. style.AddCssClass (rootNodeStyle.CssClass);
  1330. style.AddCssClass (rootNodeStyle.RegisteredCssClass);
  1331. }
  1332. }
  1333. else if (node.IsParentNode) {
  1334. if (parentNodeStyle != null) {
  1335. style.AddCssClass (parentNodeStyle.CssClass);
  1336. style.AddCssClass (parentNodeStyle.RegisteredCssClass);
  1337. }
  1338. }
  1339. if (levelStyles != null && levelStyles.Count > level) {
  1340. style.AddCssClass (levelStyles [level].CssClass);
  1341. style.AddCssClass (levelStyles [level].RegisteredCssClass);
  1342. }
  1343. if (node == SelectedNode && selectedNodeStyle != null) {
  1344. style.AddCssClass (selectedNodeStyle.CssClass);
  1345. style.AddCssClass (selectedNodeStyle.RegisteredCssClass);
  1346. }
  1347. }
  1348. else {
  1349. // styles are not registered
  1350. if (nodeStyle != null) {
  1351. style.CopyFrom (nodeStyle);
  1352. }
  1353. if (node.IsLeafNode) {
  1354. if (leafNodeStyle != null) {
  1355. style.CopyFrom (leafNodeStyle);
  1356. }
  1357. }
  1358. else if (node.IsRootNode) {
  1359. if (rootNodeStyle != null) {
  1360. style.CopyFrom (rootNodeStyle);
  1361. }
  1362. }
  1363. else if (node.IsParentNode) {
  1364. if (parentNodeStyle != null) {
  1365. style.CopyFrom (parentNodeStyle);
  1366. }
  1367. }
  1368. if (levelStyles != null && levelStyles.Count > level) {
  1369. style.CopyFrom (levelStyles [level]);
  1370. }
  1371. if (node == SelectedNode && selectedNodeStyle != null) {
  1372. style.CopyFrom (selectedNodeStyle);
  1373. }
  1374. }
  1375. style.AddAttributesToRender (writer);
  1376. }
  1377. void AddNodeLinkStyle (HtmlTextWriter writer, TreeNode node, int level) {
  1378. Style style = new Style ();
  1379. if (Page.Header != null) {
  1380. // styles are registered
  1381. style.AddCssClass (ControlLinkStyle.RegisteredCssClass);
  1382. if (nodeStyle != null) {
  1383. style.AddCssClass (nodeLinkStyle.CssClass);
  1384. style.AddCssClass (nodeLinkStyle.RegisteredCssClass);
  1385. }
  1386. if (node.IsLeafNode) {
  1387. if (leafNodeStyle != null) {
  1388. style.AddCssClass (leafNodeLinkStyle.CssClass);
  1389. style.AddCssClass (leafNodeLinkStyle.RegisteredCssClass);
  1390. }
  1391. }
  1392. else if (node.IsRootNode) {
  1393. if (rootNodeStyle != null) {
  1394. style.AddCssClass (rootNodeLinkStyle.CssClass);
  1395. style.AddCssClass (rootNodeLinkStyle.RegisteredCssClass);
  1396. }
  1397. }
  1398. else if (node.IsParentNode) {
  1399. if (parentNodeStyle != null) {
  1400. style.AddCssClass (parentNodeLinkStyle.CssClass);
  1401. style.AddCssClass (parentNodeLinkStyle.RegisteredCssClass);
  1402. }
  1403. }
  1404. if (levelStyles != null && levelStyles.Count > level) {
  1405. style.AddCssClass (levelLinkStyles [level].CssClass);
  1406. style.AddCssClass (levelLinkStyles [level].RegisteredCssClass);
  1407. }
  1408. if (node == SelectedNode && selectedNodeStyle != null) {
  1409. style.AddCssClass (selectedNodeLinkStyle.CssClass);
  1410. style.AddCssClass (selectedNodeLinkStyle.RegisteredCssClass);
  1411. }
  1412. }
  1413. else {
  1414. // styles are not registered
  1415. style.CopyFrom (ControlLinkStyle);
  1416. if (nodeStyle != null) {
  1417. style.CopyFrom (nodeLinkStyle);
  1418. }
  1419. if (node.IsLeafNode) {
  1420. if (node.IsLeafNode && leafNodeStyle != null) {
  1421. style.CopyFrom (leafNodeLinkStyle);
  1422. }
  1423. }
  1424. else if (node.IsRootNode) {
  1425. if (node.IsRootNode && rootNodeStyle != null) {
  1426. style.CopyFrom (rootNodeLinkStyle);
  1427. }
  1428. }
  1429. else if (node.IsParentNode) {
  1430. if (node.IsParentNode && parentNodeStyle != null) {
  1431. style.CopyFrom (parentNodeLinkStyle);
  1432. }
  1433. }
  1434. if (levelStyles != null && levelStyles.Count > level) {
  1435. style.CopyFrom (levelLinkStyles [level]);
  1436. }
  1437. if (node == SelectedNode && selectedNodeStyle != null) {
  1438. style.CopyFrom (selectedNodeLinkStyle);
  1439. }
  1440. style.AlwaysRenderTextDecoration = true;
  1441. }
  1442. style.AddAttributesToRender (writer);
  1443. }
  1444. void BeginNodeTag (HtmlTextWriter writer, TreeNode node, bool clientExpand)
  1445. {
  1446. if(node.ToolTip.Length>0)
  1447. writer.AddAttribute ("title", node.ToolTip);
  1448. if (node.NavigateUrl != "") {
  1449. writer.AddAttribute ("href", ResolveClientUrl (node.NavigateUrl));
  1450. if (node.Target.Length > 0)
  1451. writer.AddAttribute ("target", node.Target);
  1452. else if (Target.Length > 0)
  1453. writer.AddAttribute ("target", Target);
  1454. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1455. }
  1456. else if (node.SelectAction != TreeNodeSelectAction.None) {
  1457. if (node.SelectAction == TreeNodeSelectAction.Expand && clientExpand)
  1458. writer.AddAttribute ("href", GetClientExpandEvent (node));
  1459. else
  1460. writer.AddAttribute ("href", GetClientEvent (node, "sel"));
  1461. writer.RenderBeginTag (HtmlTextWriterTag.A);
  1462. }
  1463. else
  1464. writer.RenderBeginTag (HtmlTextWriterTag.Span);
  1465. }
  1466. string GetNodeImageToolTip (bool expand, string txt) {
  1467. if (expand) {
  1468. if (ExpandImageToolTip != "")
  1469. return String.Format (ExpandImageToolTip, txt);
  1470. else if (txt != null)
  1471. return "Expand " + txt;
  1472. else
  1473. return "Expand {0}";
  1474. } else {
  1475. if (CollapseImageToolTip != "")
  1476. return String.Format (CollapseImageToolTip, txt);
  1477. else if (txt != null)
  1478. return "Collapse " + txt;
  1479. else
  1480. return "Collapse {0}";
  1481. }
  1482. }
  1483. string GetNodeClientId (TreeNode node, string sufix)
  1484. {
  1485. return ClientID + "_" + node.Path + (sufix != null ? "_" + sufix : "");
  1486. }
  1487. string GetNodeImageUrl (string shape, ImageStyle imageStyle)
  1488. {
  1489. if (ShowLines) {
  1490. if (!String.IsNullOrEmpty (LineImagesFolder))
  1491. return ResolveClientUrl (LineImagesFolder + "/" + shape + ".gif");
  1492. } else {
  1493. if (imageStyle != null) {
  1494. if (shape == "plus") {
  1495. if (!String.IsNullOrEmpty (imageStyle.Expand))
  1496. return GetNodeIconUrl (imageStyle.Expand);
  1497. }
  1498. else if (shape == "minus") {
  1499. if (!String.IsNullOrEmpty (imageStyle.Collapse))
  1500. return GetNodeIconUrl (imageStyle.Collapse);
  1501. }
  1502. else if (shape == "noexpand") {
  1503. if (!String.IsNullOrEmpty (imageStyle.NoExpand))
  1504. return GetNodeIconUrl (imageStyle.NoExpand);
  1505. }
  1506. }
  1507. else {
  1508. if (shape == "plus") {
  1509. if (!String.IsNullOrEmpty (ExpandImageUrl))
  1510. return ResolveClientUrl (ExpandImageUrl);
  1511. }
  1512. else if (shape == "minus") {
  1513. if (!String.IsNullOrEmpty (CollapseImageUrl))
  1514. return ResolveClientUrl (CollapseImageUrl);
  1515. }
  1516. else if (shape == "noexpand") {
  1517. if (!String.IsNullOrEmpty (NoExpandImageUrl))
  1518. return ResolveClientUrl (NoExpandImageUrl);
  1519. }
  1520. }
  1521. if (!String.IsNullOrEmpty (LineImagesFolder))
  1522. return ResolveClientUrl (LineImagesFolder + "/" + shape + ".gif");
  1523. }
  1524. return AssemblyResourceLoader.GetResourceUrl (typeof(TreeView), "TreeView_" + shape + ".gif");
  1525. }
  1526. string GetNodeIconUrl (string icon)
  1527. {
  1528. return AssemblyResourceLoader.GetResourceUrl (typeof(TreeView), icon + ".gif");
  1529. }
  1530. string GetClientEvent (TreeNode node, string ev)
  1531. {
  1532. return Page.ClientScript.GetPostBackClientHyperlink (this, ev + "|" + node.Path, true);
  1533. }
  1534. string GetClientExpandEvent (TreeNode node)
  1535. {
  1536. return "javascript:TreeView_ToggleExpand ('" + ClientID + "', '" + node.Path + "')";
  1537. }
  1538. TreeNode FindNodeByPos (string path)
  1539. {
  1540. string[] indexes = path.Split ('_');
  1541. TreeNode node = null;
  1542. foreach (string index in indexes) {
  1543. int i = int.Parse (index);
  1544. if (node == null) {
  1545. if (i >= Nodes.Count) return null;
  1546. node = Nodes [i];
  1547. } else {
  1548. if (i >= node.ChildNodes.Count) return null;
  1549. node = node.ChildNodes [i];
  1550. }
  1551. }
  1552. return node;
  1553. }
  1554. void UnsetCheckStates (TreeNodeCollection col, NameValueCollection states)
  1555. {
  1556. foreach (TreeNode node in col) {
  1557. if (node.Checked) {
  1558. string val = states [ClientID + "_cs_" + node.Path];
  1559. if (val != "on") node.Checked = false;
  1560. }
  1561. if (node.HasChildData)
  1562. UnsetCheckStates (node.ChildNodes, states);
  1563. }
  1564. }
  1565. void SetCheckStates (NameValueCollection states)
  1566. {
  1567. string keyPrefix = ClientID + "_cs_";
  1568. foreach (string key in states) {
  1569. if (key.StartsWith (keyPrefix)) {
  1570. string id = key.Substring (keyPrefix.Length);
  1571. TreeNode node = FindNodeByPos (id);
  1572. if (node != null)
  1573. node.Checked = (Context.Request.Form [key] == "on");
  1574. }
  1575. }
  1576. }
  1577. void UnsetExpandStates (TreeNodeCollection col, string[] states)
  1578. {
  1579. foreach (TreeNode node in col) {
  1580. if (node.Expanded.HasValue && node.Expanded.Value) {
  1581. bool expand = (Array.IndexOf (states, node.Path) != -1);
  1582. if (!expand) node.Expanded = false;
  1583. }
  1584. if (node.HasChildData)
  1585. UnsetExpandStates (node.ChildNodes, states);
  1586. }
  1587. }
  1588. void SetExpandStates (string[] states)
  1589. {
  1590. foreach (string id in states) {
  1591. if (id == null || id == "") continue;
  1592. TreeNode node = FindNodeByPos (id);
  1593. if (node != null)
  1594. node.Expanded = true;
  1595. }
  1596. }
  1597. string GetExpandStates ()
  1598. {
  1599. StringBuilder sb = new StringBuilder ("|");
  1600. foreach (TreeNode node in Nodes)
  1601. GetExpandStates (sb, node);
  1602. return sb.ToString ();
  1603. }
  1604. void GetExpandStates (StringBuilder sb, TreeNode node)
  1605. {
  1606. if (node.Expanded.HasValue && node.Expanded.Value) {
  1607. sb.Append (node.Path);
  1608. sb.Append ('|');
  1609. }
  1610. if (node.HasChildData) {
  1611. foreach (TreeNode child in node.ChildNodes)
  1612. GetExpandStates (sb, child);
  1613. }
  1614. }
  1615. }
  1616. }
  1617. #endif