Control.cs 46 KB

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