Control.cs 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660
  1. //
  2. // System.Web.UI.Control.cs
  3. //
  4. // Authors:
  5. // Bob Smith <[email protected]>
  6. // Gonzalo Paniagua Javier ([email protected]
  7. // Andreas Nahr ([email protected])
  8. // Sanjay Gupta ([email protected])
  9. //
  10. // (C) Bob Smith
  11. // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  12. // (C) 2004 Novell, Inc. (http://www.novell.com)
  13. //
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. // This will provide extra information when trace is enabled. Might be too verbose.
  35. #define MONO_TRACE
  36. using System.Collections;
  37. using System.ComponentModel;
  38. using System.ComponentModel.Design;
  39. using System.ComponentModel.Design.Serialization;
  40. using System.Security.Permissions;
  41. using System.Web;
  42. using System.Web.Util;
  43. #if NET_2_0
  44. using System.Web.UI.Adapters;
  45. using System.IO;
  46. #endif
  47. namespace System.Web.UI
  48. {
  49. // CAS
  50. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  51. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  52. // attributes
  53. [DefaultProperty ("ID"), DesignerCategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require)]
  54. [ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, " + Consts.AssemblySystem_Design)]
  55. [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  56. #if NET_2_0
  57. [DesignerSerializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VisualStudio_Web,
  58. "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  59. [Bindable (true)]
  60. [Themeable (false)]
  61. #else
  62. [DesignerSerializer ("Microsoft.VSDesigner.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner,
  63. "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  64. #endif
  65. public partial class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
  66. #if NET_2_0
  67. , IUrlResolutionService, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
  68. #endif
  69. {
  70. static readonly object DataBindingEvent = new object();
  71. static readonly object DisposedEvent = new object();
  72. static readonly object InitEvent = new object();
  73. static readonly object LoadEvent = new object();
  74. static readonly object PreRenderEvent = new object();
  75. static readonly object UnloadEvent = new object();
  76. static string[] defaultNameArray;
  77. /* */
  78. int event_mask;
  79. const int databinding_mask = 1;
  80. const int disposed_mask = 1 << 1;
  81. const int init_mask = 1 << 2;
  82. const int load_mask = 1 << 3;
  83. const int prerender_mask = 1 << 4;
  84. const int unload_mask = 1 << 5;
  85. /* */
  86. string uniqueID;
  87. string _userId;
  88. TemplateControl _templateControl;
  89. ControlCollection _controls;
  90. Control _namingContainer;
  91. Page _page;
  92. Control _parent;
  93. ISite _site;
  94. HttpContext _context;
  95. StateBag _viewState;
  96. EventHandlerList _events;
  97. RenderMethod _renderMethodDelegate;
  98. int defaultNumberID;
  99. DataBindingCollection dataBindings;
  100. Hashtable pendingVS; // may hold unused viewstate data from child controls
  101. #if NET_2_0
  102. bool _isChildControlStateCleared;
  103. #endif
  104. /*************/
  105. int stateMask;
  106. const int ENABLE_VIEWSTATE = 1;
  107. const int VISIBLE = 1 << 1;
  108. const int AUTOID = 1 << 2;
  109. const int CREATING_CONTROLS = 1 << 3;
  110. const int BINDING_CONTAINER = 1 << 4;
  111. const int AUTO_EVENT_WIREUP = 1 << 5;
  112. const int IS_NAMING_CONTAINER = 1 << 6;
  113. const int VISIBLE_CHANGED = 1 << 7;
  114. const int TRACK_VIEWSTATE = 1 << 8;
  115. const int CHILD_CONTROLS_CREATED = 1 << 9;
  116. const int ID_SET = 1 << 10;
  117. const int INITED = 1 << 11;
  118. const int INITING = 1 << 12;
  119. const int VIEWSTATE_LOADED = 1 << 13;
  120. const int LOADED = 1 << 14;
  121. const int PRERENDERED = 1 << 15;
  122. #if NET_2_0
  123. const int ENABLE_THEMING = 1 << 16;
  124. #endif
  125. /*************/
  126. static Control ()
  127. {
  128. defaultNameArray = new string [100];
  129. for (int i = 0 ; i < 100 ; i++)
  130. defaultNameArray [i] = "_ctl" + i;
  131. }
  132. public Control()
  133. {
  134. stateMask = ENABLE_VIEWSTATE | VISIBLE | AUTOID | BINDING_CONTAINER | AUTO_EVENT_WIREUP;
  135. if (this is INamingContainer)
  136. stateMask |= IS_NAMING_CONTAINER;
  137. }
  138. #if NET_2_0
  139. [MonoTODO("Not implemented, always returns null")]
  140. protected ControlAdapter Adapter
  141. {
  142. get {
  143. // for the time being, fool the
  144. // Control machinery into thinking we
  145. // don't have an Adapter. This will
  146. // allow us to write all the rest of
  147. // the Adapter handling code without
  148. // having to worry about *having*
  149. // adapters.
  150. return null;
  151. }
  152. }
  153. string _appRelativeTemplateSourceDirectory = null;
  154. [EditorBrowsable (EditorBrowsableState.Advanced)]
  155. [Browsable (false)]
  156. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  157. public string AppRelativeTemplateSourceDirectory
  158. {
  159. get {
  160. if (_appRelativeTemplateSourceDirectory != null)
  161. return _appRelativeTemplateSourceDirectory;
  162. if (Parent != null)
  163. return Parent.AppRelativeTemplateSourceDirectory;
  164. return "~/";
  165. }
  166. [EditorBrowsable (EditorBrowsableState.Never)]
  167. set { _appRelativeTemplateSourceDirectory = value; }
  168. }
  169. #endif
  170. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  171. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  172. public Control BindingContainer {
  173. get {
  174. Control container = NamingContainer;
  175. if (container != null && (container.stateMask & BINDING_CONTAINER) == 0)
  176. container = container.BindingContainer;
  177. return container;
  178. }
  179. }
  180. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  181. [Browsable (false)]
  182. [WebSysDescription ("An Identification of the control that is rendered.")]
  183. public virtual string ClientID {
  184. get {
  185. string client = UniqueID;
  186. if (client != null)
  187. client = client.Replace (':', ClientIDSeparator);
  188. stateMask |= ID_SET;
  189. return client;
  190. }
  191. }
  192. #if NET_2_0
  193. protected char ClientIDSeparator
  194. #else
  195. char ClientIDSeparator
  196. #endif
  197. {
  198. get {
  199. return '_';
  200. }
  201. }
  202. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  203. [Browsable (false)]
  204. [WebSysDescription ("The child controls of this control.")]
  205. public virtual ControlCollection Controls //DIT
  206. {
  207. get
  208. {
  209. if (_controls == null) _controls = CreateControlCollection();
  210. return _controls;
  211. }
  212. }
  213. #if NET_2_0
  214. [MonoTODO ("revisit once we have a real design strategy")]
  215. protected internal bool DesignMode
  216. {
  217. get { return false; }
  218. }
  219. #endif
  220. [DefaultValue (true), WebCategory ("Behavior")]
  221. [WebSysDescription ("An Identification of the control that is rendered.")]
  222. #if NET_2_0
  223. [Themeable (false)]
  224. #endif
  225. public virtual bool EnableViewState {
  226. get { return ((stateMask & ENABLE_VIEWSTATE) != 0); }
  227. set { SetMask (ENABLE_VIEWSTATE, value); }
  228. }
  229. [MergableProperty (false), ParenthesizePropertyName (true)]
  230. [WebSysDescription ("The name of the control that is rendered.")]
  231. #if NET_2_0
  232. [Filterable (false), Themeable (false)]
  233. #endif
  234. public virtual string ID {
  235. get {
  236. return (((stateMask & ID_SET) != 0) ? _userId : null);
  237. }
  238. set {
  239. if (value == "")
  240. value = null;
  241. stateMask |= ID_SET;
  242. _userId = value;
  243. NullifyUniqueID ();
  244. }
  245. }
  246. #if NET_2_0
  247. protected char IdSeparator
  248. {
  249. get {
  250. return '$';
  251. }
  252. }
  253. protected internal bool IsChildControlStateCleared {
  254. get { return _isChildControlStateCleared; }
  255. }
  256. protected internal bool IsViewStateEnabled
  257. {
  258. get {
  259. for (Control control = this; control != null; control = control.Parent)
  260. if (!control.EnableViewState)
  261. return false;
  262. return true;
  263. }
  264. }
  265. protected bool LoadViewStateByID
  266. {
  267. get {
  268. return false;
  269. }
  270. }
  271. #endif
  272. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  273. [Browsable (false)]
  274. [WebSysDescription ("The container that this control is part of. The control's name has to be unique within the container.")]
  275. public virtual Control NamingContainer {
  276. get {
  277. if (_namingContainer == null && _parent != null) {
  278. if ((_parent.stateMask & IS_NAMING_CONTAINER) == 0)
  279. _namingContainer = _parent.NamingContainer;
  280. else
  281. _namingContainer = _parent;
  282. }
  283. return _namingContainer;
  284. }
  285. }
  286. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  287. [Browsable (false)]
  288. [WebSysDescription ("The webpage that this control resides on.")]
  289. #if NET_2_0
  290. [Bindable (false)]
  291. #endif
  292. public virtual Page Page //DIT
  293. {
  294. get
  295. {
  296. if (_page == null && _parent != null) _page = _parent.Page;
  297. return _page;
  298. }
  299. set
  300. {
  301. _page = value;
  302. }
  303. }
  304. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  305. [Browsable (false)]
  306. [WebSysDescription ("The parent control of this control.")]
  307. public virtual Control Parent //DIT
  308. {
  309. get
  310. {
  311. return _parent;
  312. }
  313. }
  314. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  315. [EditorBrowsable (EditorBrowsableState.Advanced), Browsable (false)]
  316. [WebSysDescription ("The site this control is part of.")]
  317. public ISite Site //DIT
  318. {
  319. get
  320. {
  321. return _site;
  322. }
  323. set
  324. {
  325. _site = value;
  326. }
  327. }
  328. #if NET_2_0
  329. [Browsable (false)]
  330. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  331. public TemplateControl TemplateControl
  332. {
  333. get {
  334. return _templateControl;
  335. }
  336. [EditorBrowsable (EditorBrowsableState.Never)]
  337. set {
  338. _templateControl = value;
  339. }
  340. }
  341. #endif
  342. #if TARGET_JVM
  343. private string _templateSourceDir;
  344. public virtual string TemplateSourceDirectory
  345. {
  346. get
  347. {
  348. int location = 0;
  349. if (_templateSourceDir == null) {
  350. string tempSrcDir = _appRelativeTemplateSourceDirectory;
  351. if (tempSrcDir == null && Parent != null)
  352. tempSrcDir = Parent.TemplateSourceDirectory;
  353. if (tempSrcDir != null && tempSrcDir.Length > 1) {
  354. location = tempSrcDir.IndexOf ('/', 1);
  355. if (location != -1)
  356. tempSrcDir = tempSrcDir.Substring (location + 1);
  357. else
  358. tempSrcDir = string.Empty;
  359. }
  360. string answer = HttpRuntime.AppDomainAppVirtualPath;
  361. if (tempSrcDir == null)
  362. tempSrcDir = "";
  363. if (tempSrcDir.Length > 0 && tempSrcDir [tempSrcDir.Length - 1] == '/')
  364. tempSrcDir = tempSrcDir.Substring (0, tempSrcDir.Length - 1);
  365. if (tempSrcDir.StartsWith ("/") || tempSrcDir.Length == 0)
  366. _templateSourceDir = answer + tempSrcDir;
  367. else
  368. _templateSourceDir = answer + "/" + tempSrcDir;
  369. }
  370. return _templateSourceDir;
  371. }
  372. }
  373. #else
  374. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  375. [Browsable (false)]
  376. [WebSysDescription ("A virtual directory containing the parent of the control.")]
  377. public virtual string TemplateSourceDirectory {
  378. get { return (_parent == null) ? String.Empty : _parent.TemplateSourceDirectory; }
  379. }
  380. #endif
  381. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  382. [Browsable (false)]
  383. [WebSysDescription ("The unique ID of the control.")]
  384. public virtual string UniqueID {
  385. get {
  386. if (uniqueID != null)
  387. return uniqueID;
  388. if (_namingContainer == null) {
  389. if ((stateMask & IS_NAMING_CONTAINER) == 0)
  390. _namingContainer = NamingContainer;
  391. if (_namingContainer == null)
  392. return _userId;
  393. }
  394. if (_userId == null)
  395. _userId = _namingContainer.GetDefaultName ();
  396. string prefix = _namingContainer.UniqueID;
  397. if (_namingContainer == _page || prefix == null) {
  398. uniqueID = _userId;
  399. return uniqueID;
  400. }
  401. uniqueID = prefix + ":" + _userId;
  402. return uniqueID;
  403. }
  404. }
  405. void SetMask (int m, bool val)
  406. {
  407. if (val)
  408. stateMask |= m;
  409. else
  410. stateMask &= ~m;
  411. }
  412. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  413. [WebSysDescription ("Visiblity state of the control.")]
  414. public virtual bool Visible {
  415. get {
  416. if ((stateMask & VISIBLE) == 0)
  417. return false;
  418. if (_parent != null)
  419. return _parent.Visible;
  420. return true;
  421. }
  422. set {
  423. if ((value && (stateMask & VISIBLE) == 0) ||
  424. (!value && (stateMask & VISIBLE) != 0)) {
  425. if (IsTrackingViewState)
  426. stateMask |= VISIBLE_CHANGED;
  427. }
  428. SetMask (VISIBLE, value);
  429. }
  430. }
  431. protected bool ChildControlsCreated {
  432. get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
  433. set {
  434. if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0) {
  435. if (_controls != null)
  436. _controls.Clear();
  437. }
  438. SetMask (CHILD_CONTROLS_CREATED, value);
  439. }
  440. }
  441. [Browsable (false)]
  442. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  443. protected virtual HttpContext Context //DIT
  444. {
  445. get
  446. {
  447. HttpContext context;
  448. if (_context != null)
  449. return _context;
  450. if (_parent == null)
  451. return HttpContext.Current;
  452. context = _parent.Context;
  453. if (context != null)
  454. return context;
  455. return HttpContext.Current;
  456. }
  457. }
  458. protected EventHandlerList Events {
  459. get {
  460. if (_events == null)
  461. _events = new EventHandlerList ();
  462. return _events;
  463. }
  464. }
  465. protected bool HasChildViewState {
  466. get {
  467. return (pendingVS != null && pendingVS.Count > 0);
  468. }
  469. }
  470. protected bool IsTrackingViewState {
  471. get { return ((stateMask & TRACK_VIEWSTATE) != 0); }
  472. }
  473. [Browsable (false)]
  474. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  475. [WebSysDescription ("ViewState")]
  476. protected virtual StateBag ViewState
  477. {
  478. get
  479. {
  480. if(_viewState == null)
  481. _viewState = new StateBag (ViewStateIgnoresCase);
  482. if (IsTrackingViewState)
  483. _viewState.TrackViewState ();
  484. return _viewState;
  485. }
  486. }
  487. [Browsable (false)]
  488. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  489. protected virtual bool ViewStateIgnoresCase
  490. {
  491. get {
  492. return false;
  493. }
  494. }
  495. internal bool AutoEventWireup {
  496. get { return (stateMask & AUTO_EVENT_WIREUP) != 0; }
  497. set { SetMask (AUTO_EVENT_WIREUP, value); }
  498. }
  499. internal void SetBindingContainer (bool isBC)
  500. {
  501. SetMask (BINDING_CONTAINER, isBC);
  502. }
  503. internal void ResetChildNames ()
  504. {
  505. defaultNumberID = 0;
  506. }
  507. #if !TARGET_J2EE
  508. string GetDefaultName ()
  509. {
  510. string defaultName;
  511. if (defaultNumberID > 99) {
  512. defaultName = "_ctl" + defaultNumberID++;
  513. } else {
  514. defaultName = defaultNameArray [defaultNumberID++];
  515. }
  516. return defaultName;
  517. }
  518. #endif
  519. void NullifyUniqueID ()
  520. {
  521. uniqueID = null;
  522. if (!HasControls ())
  523. return;
  524. foreach (Control c in _controls)
  525. c.NullifyUniqueID ();
  526. }
  527. protected internal virtual void AddedControl (Control control, int index)
  528. {
  529. /* Ensure the control don't have more than 1 parent */
  530. if (control._parent != null)
  531. control._parent.Controls.Remove (control);
  532. control._parent = this;
  533. control._page = _page;
  534. Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
  535. if (nc != null) {
  536. control._namingContainer = nc;
  537. if (control.AutoID == true && control._userId == null)
  538. control._userId = nc.GetDefaultName ();
  539. }
  540. if ((stateMask & (INITING | INITED)) != 0)
  541. control.InitRecursive (nc);
  542. if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
  543. if (pendingVS != null) {
  544. object vs = pendingVS [index];
  545. if (vs != null) {
  546. pendingVS.Remove (index);
  547. if (pendingVS.Count == 0)
  548. pendingVS = null;
  549. control.LoadViewStateRecursive (vs);
  550. }
  551. }
  552. }
  553. if ((stateMask & LOADED) != 0)
  554. control.LoadRecursive ();
  555. if ((stateMask & PRERENDERED) != 0)
  556. control.PreRenderRecursiveInternal ();
  557. }
  558. protected virtual void AddParsedSubObject(object obj) //DIT
  559. {
  560. Control c = obj as Control;
  561. if (c != null) Controls.Add(c);
  562. }
  563. #if NET_2_0
  564. [EditorBrowsable (EditorBrowsableState.Advanced)]
  565. public virtual void ApplyStyleSheetSkin (Page page)
  566. {
  567. if (!EnableTheming) /* this enough? */
  568. return;
  569. /* apply the style sheet skin here */
  570. if (page.StyleSheetPageTheme != null) {
  571. ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType(), SkinID);
  572. if (cs != null)
  573. cs.ApplySkin (this);
  574. }
  575. }
  576. #endif
  577. protected void BuildProfileTree(string parentId, bool calcViewState)
  578. {
  579. //TODO
  580. }
  581. #if NET_2_0
  582. protected void ClearChildControlState ()
  583. {
  584. _isChildControlStateCleared = true;
  585. }
  586. protected void ClearChildState ()
  587. {
  588. ClearChildViewState ();
  589. ClearChildControlState ();
  590. }
  591. #endif
  592. protected void ClearChildViewState ()
  593. {
  594. pendingVS = null;
  595. }
  596. #if NET_2_0
  597. protected internal
  598. #else
  599. protected
  600. #endif
  601. virtual void CreateChildControls() {} //DIT
  602. protected virtual ControlCollection CreateControlCollection() //DIT
  603. {
  604. return new ControlCollection(this);
  605. }
  606. protected virtual void EnsureChildControls ()
  607. {
  608. if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
  609. stateMask |= CREATING_CONTROLS;
  610. #if NET_2_0
  611. if (Adapter != null)
  612. Adapter.CreateChildControls ();
  613. else
  614. #endif
  615. CreateChildControls();
  616. ChildControlsCreated = true;
  617. stateMask &= ~CREATING_CONTROLS;
  618. }
  619. }
  620. #if NET_2_0
  621. protected void EnsureID ()
  622. {
  623. if (Page == null)
  624. return;
  625. ID = NamingContainer.GetDefaultName ();
  626. }
  627. protected bool HasEvents ()
  628. {
  629. return _events != null;
  630. }
  631. #endif
  632. protected bool IsLiteralContent()
  633. {
  634. if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
  635. return true;
  636. return false;
  637. }
  638. [WebSysDescription ("")]
  639. public virtual Control FindControl (string id)
  640. {
  641. return FindControl (id, 0);
  642. }
  643. Control LookForControlByName (string id)
  644. {
  645. if (!HasControls ())
  646. return null;
  647. Control result = null;
  648. foreach (Control c in _controls) {
  649. if (String.Compare (id, c._userId, true) == 0) {
  650. if (result != null && result != c) {
  651. throw new HttpException ("1 Found more than one control with ID '" + id + "'");
  652. }
  653. result = c;
  654. continue;
  655. }
  656. if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ()) {
  657. Control child = c.LookForControlByName (id);
  658. if (child != null) {
  659. if (result != null && result != child)
  660. throw new HttpException ("2 Found more than one control with ID '" + id + "'");
  661. result = child;
  662. }
  663. }
  664. }
  665. return result;
  666. }
  667. protected virtual Control FindControl (string id, int pathOffset)
  668. {
  669. EnsureChildControls ();
  670. Control namingContainer = null;
  671. if ((stateMask & IS_NAMING_CONTAINER) == 0) {
  672. namingContainer = NamingContainer;
  673. if (namingContainer == null)
  674. return null;
  675. return namingContainer.FindControl (id, pathOffset);
  676. }
  677. if (!HasControls ())
  678. return null;
  679. int colon = id.IndexOf (':', pathOffset);
  680. if (colon == -1)
  681. return LookForControlByName (id.Substring (pathOffset));
  682. string idfound = id.Substring (pathOffset, colon - pathOffset);
  683. namingContainer = LookForControlByName (idfound);
  684. if (namingContainer == null)
  685. return null;
  686. return namingContainer.FindControl (id, colon + 1);
  687. }
  688. protected virtual void LoadViewState(object savedState)
  689. {
  690. if (savedState != null) {
  691. ViewState.LoadViewState (savedState);
  692. object o = ViewState ["Visible"];
  693. if (o != null) {
  694. SetMask (VISIBLE, (bool) o);
  695. stateMask |= VISIBLE_CHANGED;
  696. }
  697. }
  698. }
  699. // [MonoTODO("Secure?")]
  700. protected string MapPathSecure(string virtualPath)
  701. {
  702. string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  703. return Context.Request.MapPath (combined);
  704. }
  705. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  706. {
  707. #if MONO_TRACE
  708. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  709. string type_name = null;
  710. if (trace != null) {
  711. type_name = GetType ().Name;
  712. trace.Write ("control", String.Format ("OnBubbleEvent {0} {1}", _userId, type_name));
  713. }
  714. #endif
  715. return false;
  716. }
  717. protected virtual void OnDataBinding (EventArgs e)
  718. {
  719. if ((event_mask & databinding_mask) != 0) {
  720. EventHandler eh = (EventHandler)(_events [DataBindingEvent]);
  721. if (eh != null) {
  722. #if MONO_TRACE
  723. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  724. string type_name = null;
  725. if (trace != null) {
  726. type_name = GetType ().Name;
  727. trace.Write ("control", String.Format ("OnDataBinding {0} {1}", _userId, type_name));
  728. }
  729. #endif
  730. eh (this, e);
  731. }
  732. }
  733. }
  734. #if NET_2_0
  735. protected internal
  736. #else
  737. protected
  738. #endif
  739. virtual void OnInit (EventArgs e)
  740. {
  741. if ((event_mask & init_mask) != 0) {
  742. EventHandler eh = (EventHandler)(_events [InitEvent]);
  743. if (eh != null) {
  744. #if MONO_TRACE
  745. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  746. string type_name = null;
  747. if (trace != null) {
  748. type_name = GetType ().Name;
  749. trace.Write ("control", String.Format ("OnInit {0} {1}", _userId, type_name));
  750. }
  751. #endif
  752. eh (this, e);
  753. }
  754. }
  755. }
  756. #if NET_2_0
  757. protected internal
  758. #else
  759. protected
  760. #endif
  761. virtual void OnLoad (EventArgs e)
  762. {
  763. if ((event_mask & load_mask) != 0) {
  764. EventHandler eh = (EventHandler)(_events [LoadEvent]);
  765. if (eh != null) {
  766. #if MONO_TRACE
  767. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  768. string type_name = null;
  769. if (trace != null) {
  770. type_name = GetType ().Name;
  771. trace.Write ("control", String.Format ("OnLoad {0} {1}", _userId, type_name));
  772. }
  773. #endif
  774. eh (this, e);
  775. }
  776. }
  777. }
  778. #if NET_2_0
  779. protected internal
  780. #else
  781. protected
  782. #endif
  783. virtual void OnPreRender (EventArgs e)
  784. {
  785. if ((event_mask & prerender_mask) != 0) {
  786. EventHandler eh = (EventHandler)(_events [PreRenderEvent]);
  787. if (eh != null) {
  788. #if MONO_TRACE
  789. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  790. string type_name = null;
  791. if (trace != null) {
  792. type_name = GetType ().Name;
  793. trace.Write ("control", String.Format ("OnPreRender {0} {1}", _userId, type_name));
  794. }
  795. #endif
  796. eh (this, e);
  797. }
  798. }
  799. }
  800. #if NET_2_0
  801. protected internal
  802. #else
  803. protected
  804. #endif
  805. virtual void OnUnload(EventArgs e)
  806. {
  807. if ((event_mask & unload_mask) != 0) {
  808. EventHandler eh = (EventHandler)(_events [UnloadEvent]);
  809. if (eh != null) {
  810. #if MONO_TRACE
  811. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  812. string type_name = null;
  813. if (trace != null) {
  814. type_name = GetType ().Name;
  815. trace.Write ("control", String.Format ("OnUnload {0} {1}", _userId, type_name));
  816. }
  817. #endif
  818. eh (this, e);
  819. }
  820. }
  821. }
  822. #if NET_2_0
  823. protected internal Stream OpenFile (string path)
  824. {
  825. try {
  826. string filePath = Context.Server.MapPath (path);
  827. return File.OpenRead (filePath);
  828. }
  829. catch (UnauthorizedAccessException) {
  830. throw new HttpException ("Access to the specified file was denied.");
  831. }
  832. }
  833. #endif
  834. protected void RaiseBubbleEvent(object source, EventArgs args)
  835. {
  836. Control c = Parent;
  837. while (c != null) {
  838. #if MONO_TRACE
  839. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  840. string type_name = null;
  841. if (trace != null) {
  842. type_name = GetType ().Name;
  843. trace.Write ("control", String.Format ("RaiseBubbleEvent {0} {1}", _userId, type_name));
  844. }
  845. #endif
  846. if (c.OnBubbleEvent (source, args)) {
  847. #if MONO_TRACE
  848. if (trace != null)
  849. trace.Write ("control", String.Format ("End RaiseBubbleEvent (false) {0} {1}", _userId, type_name));
  850. #endif
  851. break;
  852. }
  853. #if MONO_TRACE
  854. if (trace != null)
  855. trace.Write ("control", String.Format ("End RaiseBubbleEvent (true) {0} {1}", _userId, type_name));
  856. #endif
  857. c = c.Parent;
  858. }
  859. }
  860. #if NET_2_0
  861. protected internal
  862. #else
  863. protected
  864. #endif
  865. virtual void Render(HtmlTextWriter writer) //DIT
  866. {
  867. RenderChildren(writer);
  868. }
  869. #if NET_2_0
  870. protected internal
  871. #else
  872. protected
  873. #endif
  874. virtual void RenderChildren (HtmlTextWriter writer) //DIT
  875. {
  876. if (_renderMethodDelegate != null) {
  877. _renderMethodDelegate (writer, this);
  878. } else if (_controls != null) {
  879. int len = _controls.Count;
  880. for (int i = 0; i < len; i++) {
  881. Control c = _controls [i];
  882. #if NET_2_0
  883. if (c.Adapter != null)
  884. c.RenderControl (writer, c.Adapter);
  885. else
  886. #endif
  887. c.RenderControl (writer);
  888. }
  889. }
  890. }
  891. #if NET_2_0
  892. protected virtual ControlAdapter ResolveAdapter ()
  893. {
  894. throw new NotImplementedException ();
  895. }
  896. #endif
  897. protected virtual object SaveViewState ()
  898. {
  899. if ((stateMask & VISIBLE_CHANGED) != 0) {
  900. ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
  901. } else if (_viewState == null) {
  902. return null;
  903. }
  904. return _viewState.SaveViewState ();
  905. }
  906. protected virtual void TrackViewState()
  907. {
  908. if (_viewState != null)
  909. _viewState.TrackViewState ();
  910. stateMask |= TRACK_VIEWSTATE;
  911. }
  912. public virtual void Dispose ()
  913. {
  914. if ((event_mask & disposed_mask) != 0) {
  915. EventHandler eh = (EventHandler)(_events [DisposedEvent]);
  916. if (eh != null) eh (this, EventArgs.Empty);
  917. }
  918. }
  919. [WebCategory ("FIXME")]
  920. [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
  921. public event EventHandler DataBinding {
  922. add {
  923. event_mask |= databinding_mask;
  924. Events.AddHandler (DataBindingEvent, value);
  925. }
  926. remove { Events.RemoveHandler (DataBindingEvent, value); }
  927. }
  928. [WebSysDescription ("Raised when the contol is disposed.")]
  929. public event EventHandler Disposed {
  930. add {
  931. event_mask |= disposed_mask;
  932. Events.AddHandler (DisposedEvent, value);
  933. }
  934. remove { Events.RemoveHandler (DisposedEvent, value); }
  935. }
  936. [WebSysDescription ("Raised when the page containing the control is initialized.")]
  937. public event EventHandler Init {
  938. add {
  939. event_mask |= init_mask;
  940. Events.AddHandler (InitEvent, value);
  941. }
  942. remove { Events.RemoveHandler (InitEvent, value); }
  943. }
  944. [WebSysDescription ("Raised after the page containing the control has been loaded.")]
  945. public event EventHandler Load {
  946. add {
  947. event_mask |= load_mask;
  948. Events.AddHandler (LoadEvent, value);
  949. }
  950. remove { Events.RemoveHandler (LoadEvent, value); }
  951. }
  952. [WebSysDescription ("Raised before the page containing the control is rendered.")]
  953. public event EventHandler PreRender {
  954. add {
  955. event_mask |= prerender_mask;
  956. Events.AddHandler (PreRenderEvent, value);
  957. }
  958. remove { Events.RemoveHandler (PreRenderEvent, value); }
  959. }
  960. [WebSysDescription ("Raised when the page containing the control is unloaded.")]
  961. public event EventHandler Unload {
  962. add {
  963. event_mask |= unload_mask;
  964. Events.AddHandler (UnloadEvent, value);
  965. }
  966. remove { Events.RemoveHandler (UnloadEvent, value); }
  967. }
  968. public virtual void DataBind() //DIT
  969. {
  970. #if NET_2_0
  971. DataBind (true);
  972. #else
  973. OnDataBinding (EventArgs.Empty);
  974. DataBindChildren();
  975. #endif
  976. }
  977. #if NET_2_0
  978. protected virtual
  979. #endif
  980. void DataBindChildren ()
  981. {
  982. if (!HasControls ())
  983. return;
  984. int len = _controls.Count;
  985. for (int i = 0; i < len; i++) {
  986. Control c = _controls [i];
  987. c.DataBind ();
  988. }
  989. }
  990. public virtual bool HasControls ()
  991. {
  992. return (_controls != null && _controls.Count > 0);
  993. }
  994. #if NET_2_0
  995. public virtual
  996. #else
  997. public
  998. #endif
  999. void RenderControl (HtmlTextWriter writer)
  1000. {
  1001. if ((stateMask & VISIBLE) != 0) {
  1002. HttpContext ctx = Context;
  1003. TraceContext trace = (ctx != null) ? ctx.Trace : null;
  1004. int pos = 0;
  1005. if ((trace != null) && trace.IsEnabled)
  1006. pos = ctx.Response.GetOutputByteCount ();
  1007. Render(writer);
  1008. if ((trace != null) && trace.IsEnabled) {
  1009. int size = ctx.Response.GetOutputByteCount () - pos;
  1010. trace.SaveSize (this, size >= 0 ? size : 0);
  1011. }
  1012. }
  1013. }
  1014. #if NET_2_0
  1015. protected void RenderControl (HtmlTextWriter writer,
  1016. ControlAdapter adapter)
  1017. {
  1018. if ((stateMask & VISIBLE) != 0) {
  1019. adapter.BeginRender (writer);
  1020. adapter.Render (writer);
  1021. adapter.EndRender (writer);
  1022. }
  1023. }
  1024. #endif
  1025. public string ResolveUrl (string relativeUrl)
  1026. {
  1027. if (relativeUrl == null)
  1028. throw new ArgumentNullException ("relativeUrl");
  1029. if (relativeUrl == "")
  1030. return "";
  1031. if (relativeUrl [0] == '#')
  1032. return relativeUrl;
  1033. string ts = TemplateSourceDirectory;
  1034. if (ts == null || ts.Length == 0 ||
  1035. Context == null || Context.Response == null ||
  1036. !UrlUtils.IsRelativeUrl (relativeUrl))
  1037. return relativeUrl;
  1038. HttpResponse resp = Context.Response;
  1039. return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
  1040. }
  1041. #if NET_2_0
  1042. public
  1043. #else
  1044. internal
  1045. #endif
  1046. string ResolveClientUrl (string relativeUrl)
  1047. {
  1048. string absoluteUrl = ResolveUrl (relativeUrl);
  1049. #if TARGET_J2EE
  1050. // There are no relative paths when rendering a J2EE portlet
  1051. if (IsPortletRender)
  1052. return absoluteUrl;
  1053. #endif
  1054. if (Context != null && Context.Request != null) {
  1055. string baseUrl = Context.Request.BaseVirtualDir;
  1056. if (absoluteUrl.StartsWith (baseUrl + "/"))
  1057. return absoluteUrl.Substring (baseUrl.Length + 1);
  1058. }
  1059. return absoluteUrl;
  1060. }
  1061. internal bool HasRenderMethodDelegate () {
  1062. return _renderMethodDelegate != null;
  1063. }
  1064. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1065. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  1066. {
  1067. _renderMethodDelegate = renderMethod;
  1068. }
  1069. internal void LoadRecursive()
  1070. {
  1071. #if MONO_TRACE
  1072. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1073. string type_name = null;
  1074. if (trace != null) {
  1075. type_name = GetType ().Name;
  1076. trace.Write ("control", String.Format ("LoadRecursive {0} {1}", _userId, type_name));
  1077. }
  1078. #endif
  1079. #if NET_2_0
  1080. if (Adapter != null)
  1081. Adapter.OnLoad (EventArgs.Empty);
  1082. else
  1083. #endif
  1084. OnLoad (EventArgs.Empty);
  1085. if (HasControls ()) {
  1086. int len = _controls.Count;
  1087. for (int i=0;i<len;i++)
  1088. {
  1089. Control c = _controls[i];
  1090. c.LoadRecursive ();
  1091. }
  1092. }
  1093. #if MONO_TRACE
  1094. if (trace != null)
  1095. trace.Write ("control", String.Format ("End LoadRecursive {0} {1}", _userId, type_name));
  1096. #endif
  1097. stateMask |= LOADED;
  1098. }
  1099. internal void UnloadRecursive(Boolean dispose)
  1100. {
  1101. #if MONO_TRACE
  1102. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1103. string type_name = null;
  1104. if (trace != null) {
  1105. type_name = GetType ().Name;
  1106. trace.Write ("control", String.Format ("UnloadRecursive {0} {1}", _userId, type_name));
  1107. }
  1108. #endif
  1109. if (HasControls ()) {
  1110. int len = _controls.Count;
  1111. for (int i=0;i<len;i++)
  1112. {
  1113. Control c = _controls[i];
  1114. c.UnloadRecursive (dispose);
  1115. }
  1116. }
  1117. #if MONO_TRACE
  1118. if (trace != null)
  1119. trace.Write ("control", String.Format ("End UnloadRecursive {0} {1}", _userId, type_name));
  1120. #endif
  1121. #if NET_2_0
  1122. if (Adapter != null)
  1123. Adapter.OnUnload (EventArgs.Empty);
  1124. else
  1125. #endif
  1126. OnUnload (EventArgs.Empty);
  1127. if (dispose)
  1128. Dispose();
  1129. }
  1130. internal void PreRenderRecursiveInternal()
  1131. {
  1132. if ((stateMask & VISIBLE) != 0) {
  1133. EnsureChildControls ();
  1134. #if MONO_TRACE
  1135. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1136. string type_name = null;
  1137. if (trace != null) {
  1138. type_name = GetType ().Name;
  1139. trace.Write ("control", String.Format ("PreRenderRecursive {0} {1}", _userId, type_name));
  1140. }
  1141. #endif
  1142. #if NET_2_0
  1143. if (Adapter != null)
  1144. Adapter.OnPreRender (EventArgs.Empty);
  1145. else
  1146. #endif
  1147. OnPreRender (EventArgs.Empty);
  1148. if (!HasControls ())
  1149. return;
  1150. int len = _controls.Count;
  1151. for (int i=0;i<len;i++)
  1152. {
  1153. Control c = _controls[i];
  1154. c.PreRenderRecursiveInternal ();
  1155. }
  1156. #if MONO_TRACE
  1157. if (trace != null)
  1158. trace.Write ("control", String.Format ("End PreRenderRecursive {0} {1}", _userId, type_name));
  1159. #endif
  1160. }
  1161. stateMask |= PRERENDERED;
  1162. }
  1163. internal void InitRecursive(Control namingContainer)
  1164. {
  1165. #if MONO_TRACE
  1166. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1167. string type_name = null;
  1168. if (trace != null) {
  1169. type_name = GetType ().Name;
  1170. trace.Write ("control", String.Format ("InitRecursive {0} {1}", _userId, type_name));
  1171. }
  1172. #endif
  1173. if (HasControls ()) {
  1174. if ((stateMask & IS_NAMING_CONTAINER) != 0)
  1175. namingContainer = this;
  1176. if (namingContainer != null &&
  1177. namingContainer._userId == null &&
  1178. namingContainer.AutoID)
  1179. namingContainer._userId = namingContainer.GetDefaultName () + "b";
  1180. int len = _controls.Count;
  1181. for (int i=0;i<len;i++)
  1182. {
  1183. Control c = _controls[i];
  1184. c._page = Page;
  1185. c._namingContainer = namingContainer;
  1186. if (namingContainer != null && c._userId == null && c.AutoID)
  1187. c._userId = namingContainer.GetDefaultName () + "c";
  1188. c.InitRecursive (namingContainer);
  1189. }
  1190. }
  1191. stateMask |= INITING;
  1192. #if NET_2_0
  1193. ApplyTheme ();
  1194. if (Adapter != null)
  1195. Adapter.OnInit (EventArgs.Empty);
  1196. else
  1197. #endif
  1198. OnInit (EventArgs.Empty);
  1199. #if MONO_TRACE
  1200. if (trace != null)
  1201. trace.Write ("control", String.Format ("End InitRecursive {0} {1}", _userId, type_name));
  1202. #endif
  1203. TrackViewState ();
  1204. stateMask |= INITED;
  1205. stateMask &= ~INITING;
  1206. }
  1207. internal object SaveViewStateRecursive ()
  1208. {
  1209. if (!EnableViewState)
  1210. return null;
  1211. #if MONO_TRACE
  1212. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1213. string type_name = null;
  1214. if (trace != null) {
  1215. type_name = GetType ().Name;
  1216. trace.Write ("control", String.Format ("SaveViewStateRecursive {0} {1}", _userId, type_name));
  1217. }
  1218. #endif
  1219. ArrayList controlList = null;
  1220. ArrayList controlStates = null;
  1221. int idx = -1;
  1222. if (HasControls ())
  1223. {
  1224. int len = _controls.Count;
  1225. for (int i=0;i<len;i++)
  1226. {
  1227. Control ctrl = _controls[i];
  1228. object ctrlState = ctrl.SaveViewStateRecursive ();
  1229. idx++;
  1230. if (ctrlState == null)
  1231. continue;
  1232. if (controlList == null)
  1233. {
  1234. controlList = new ArrayList ();
  1235. controlStates = new ArrayList ();
  1236. }
  1237. controlList.Add (idx);
  1238. controlStates.Add (ctrlState);
  1239. }
  1240. }
  1241. object thisState = SaveViewState ();
  1242. if (thisState == null && controlList == null && controlStates == null) {
  1243. #if MONO_TRACE
  1244. if (trace != null) {
  1245. trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved nothing", _userId, type_name));
  1246. trace.SaveViewState (this, null);
  1247. }
  1248. #endif
  1249. return null;
  1250. }
  1251. #if MONO_TRACE
  1252. if (trace != null) {
  1253. trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved a Triplet", _userId, type_name));
  1254. trace.SaveViewState (this, thisState);
  1255. }
  1256. #endif
  1257. return new Triplet (thisState, controlList, controlStates);
  1258. }
  1259. internal void LoadViewStateRecursive (object savedState)
  1260. {
  1261. if (!EnableViewState || savedState == null)
  1262. return;
  1263. #if MONO_TRACE
  1264. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1265. string type_name = null;
  1266. if (trace != null) {
  1267. type_name = GetType ().Name;
  1268. trace.Write ("control", String.Format ("LoadViewStateRecursive {0} {1}", _userId, type_name));
  1269. }
  1270. #endif
  1271. Triplet savedInfo = (Triplet) savedState;
  1272. LoadViewState (savedInfo.First);
  1273. ArrayList controlList = savedInfo.Second as ArrayList;
  1274. if (controlList == null)
  1275. return;
  1276. ArrayList controlStates = savedInfo.Third as ArrayList;
  1277. int nControls = controlList.Count;
  1278. for (int i = 0; i < nControls; i++) {
  1279. int k = (int) controlList [i];
  1280. if (k < Controls.Count && controlStates != null) {
  1281. Control c = Controls [k];
  1282. c.LoadViewStateRecursive (controlStates [i]);
  1283. } else {
  1284. if (pendingVS == null)
  1285. pendingVS = new Hashtable ();
  1286. pendingVS [k] = controlStates [i];
  1287. }
  1288. }
  1289. #if MONO_TRACE
  1290. if (trace != null)
  1291. trace.Write ("control", String.Format ("End LoadViewStateRecursive {0} {1}", _userId, type_name));
  1292. #endif
  1293. stateMask |= VIEWSTATE_LOADED;
  1294. }
  1295. #if NET_2_0
  1296. internal ControlSkin controlSkin;
  1297. internal void ApplyTheme ()
  1298. {
  1299. #if MONO_TRACE
  1300. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1301. string type_name = null;
  1302. if (trace != null) {
  1303. type_name = GetType ().Name;
  1304. trace.Write ("control", String.Format ("ApplyThemeRecursive {0} {1}", _userId, type_name));
  1305. }
  1306. #endif
  1307. if (Page.PageTheme != null && EnableTheming) {
  1308. ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
  1309. if (controlSkin != null)
  1310. controlSkin.ApplySkin (this);
  1311. }
  1312. #if MONO_TRACE
  1313. if (trace != null)
  1314. trace.Write ("control", String.Format ("End ApplyThemeRecursive {0} {1}", _userId, type_name));
  1315. #endif
  1316. }
  1317. #endif
  1318. internal bool AutoID
  1319. {
  1320. get { return (stateMask & AUTOID) != 0; }
  1321. set {
  1322. if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
  1323. return;
  1324. SetMask (AUTOID, value);
  1325. }
  1326. }
  1327. protected internal virtual void RemovedControl (Control control)
  1328. {
  1329. control.UnloadRecursive (false);
  1330. control._parent = null;
  1331. control._page = null;
  1332. control._namingContainer = null;
  1333. }
  1334. #if NET_2_0
  1335. string skinId = string.Empty;
  1336. bool _enableTheming = true;
  1337. [Browsable (false)]
  1338. [Themeable (false)]
  1339. [DefaultValue (true)]
  1340. public virtual bool EnableTheming
  1341. {
  1342. get
  1343. {
  1344. if ((stateMask & ENABLE_THEMING) != 0)
  1345. return _enableTheming;
  1346. if (_parent != null)
  1347. return _parent.EnableTheming;
  1348. return true;
  1349. }
  1350. set
  1351. {
  1352. SetMask (ENABLE_THEMING, true);
  1353. _enableTheming = value;
  1354. }
  1355. }
  1356. [Browsable (false)]
  1357. [DefaultValue ("")]
  1358. [Filterable (false)]
  1359. public virtual string SkinID
  1360. {
  1361. get { return skinId; }
  1362. set { skinId = value; }
  1363. }
  1364. ControlBuilder IControlBuilderAccessor.ControlBuilder {
  1365. get {throw new NotImplementedException (); }
  1366. }
  1367. IDictionary IControlDesignerAccessor.GetDesignModeState ()
  1368. {
  1369. throw new NotImplementedException ();
  1370. }
  1371. void IControlDesignerAccessor.SetDesignModeState (IDictionary designData)
  1372. {
  1373. SetDesignModeState (designData);
  1374. }
  1375. void IControlDesignerAccessor.SetOwnerControl (Control control)
  1376. {
  1377. throw new NotImplementedException ();
  1378. }
  1379. IDictionary IControlDesignerAccessor.UserData {
  1380. get { throw new NotImplementedException (); }
  1381. }
  1382. ExpressionBindingCollection expressionBindings;
  1383. ExpressionBindingCollection IExpressionsAccessor.Expressions {
  1384. get {
  1385. if (expressionBindings == null)
  1386. expressionBindings = new ExpressionBindingCollection ();
  1387. return expressionBindings;
  1388. }
  1389. }
  1390. bool IExpressionsAccessor.HasExpressions {
  1391. get {
  1392. return (expressionBindings != null && expressionBindings.Count > 0);
  1393. }
  1394. }
  1395. public virtual void Focus()
  1396. {
  1397. Page.SetFocus (this);
  1398. }
  1399. protected internal virtual void LoadControlState (object state)
  1400. {
  1401. }
  1402. protected internal virtual object SaveControlState ()
  1403. {
  1404. return null;
  1405. }
  1406. protected virtual void DataBind (bool raiseOnDataBinding)
  1407. {
  1408. bool foundDataItem = false;
  1409. if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
  1410. object o = DataBinder.GetDataItem (this, out foundDataItem);
  1411. if (foundDataItem)
  1412. Page.PushDataItemContext (o);
  1413. }
  1414. try {
  1415. if (raiseOnDataBinding)
  1416. OnDataBinding (EventArgs.Empty);
  1417. DataBindChildren();
  1418. } finally {
  1419. if (foundDataItem)
  1420. Page.PopDataItemContext ();
  1421. }
  1422. }
  1423. protected virtual IDictionary GetDesignModeState ()
  1424. {
  1425. throw new NotImplementedException ();
  1426. }
  1427. protected virtual void SetDesignModeState (IDictionary data)
  1428. {
  1429. throw new NotImplementedException ();
  1430. }
  1431. #endif
  1432. void IParserAccessor.AddParsedSubObject (object obj) {
  1433. this.AddParsedSubObject (obj);
  1434. }
  1435. DataBindingCollection IDataBindingsAccessor.DataBindings {
  1436. get {
  1437. if (dataBindings == null) {
  1438. dataBindings = new DataBindingCollection ();
  1439. }
  1440. return dataBindings;
  1441. }
  1442. }
  1443. bool IDataBindingsAccessor.HasDataBindings {
  1444. get {
  1445. if (dataBindings != null && dataBindings.Count > 0) {
  1446. return true;
  1447. }
  1448. return false;
  1449. }
  1450. }
  1451. }
  1452. }