Control.cs 47 KB

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