Control.cs 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732
  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. get { return _templateControl; }
  346. [EditorBrowsable (EditorBrowsableState.Never)]
  347. set { _templateControl = value; }
  348. }
  349. #endif
  350. #if !TARGET_J2EE
  351. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  352. [Browsable (false)]
  353. [WebSysDescription ("A virtual directory containing the parent of the control.")]
  354. public virtual string TemplateSourceDirectory {
  355. get {
  356. #if NET_2_0
  357. if (_templateControl != null)
  358. return _templateControl.TemplateSourceDirectory;
  359. // Arguably, a hack, but this is the easiest way of doing it. We must be sure that controls placed
  360. // in an .ascx file get their source directory right, or otherwise ResolveClientUrl will return
  361. // invalid paths in such situations.
  362. return (_parent == null || !(_parent is UserControl)) ? String.Empty : _parent.TemplateSourceDirectory;
  363. #else
  364. return (_parent == null) ? String.Empty : _parent.TemplateSourceDirectory;
  365. #endif
  366. }
  367. }
  368. #endif
  369. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  370. [Browsable (false)]
  371. [WebSysDescription ("The unique ID of the control.")]
  372. public virtual string UniqueID {
  373. get {
  374. if (uniqueID != null)
  375. return uniqueID;
  376. if (_namingContainer == null) {
  377. if ((stateMask & IS_NAMING_CONTAINER) == 0)
  378. _namingContainer = NamingContainer;
  379. if (_namingContainer == null)
  380. return _userId;
  381. }
  382. if (_userId == null)
  383. _userId = _namingContainer.GetDefaultName ();
  384. string prefix = _namingContainer.UniqueID;
  385. #if TARGET_J2EE
  386. // For J2EE portlets we need to add the namespace to the ID.
  387. if (_namingContainer == _page && _page.PortletNamespace != null)
  388. prefix = _page.PortletNamespace;
  389. else
  390. #endif
  391. if (_namingContainer == _page || prefix == null) {
  392. uniqueID = _userId;
  393. return uniqueID;
  394. }
  395. #if NET_2_0
  396. uniqueID = prefix + IdSeparator + _userId;
  397. #else
  398. uniqueID = prefix + ":" + _userId;
  399. #endif
  400. return uniqueID;
  401. }
  402. }
  403. void SetMask (int m, bool val)
  404. {
  405. if (val)
  406. stateMask |= m;
  407. else
  408. stateMask &= ~m;
  409. }
  410. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  411. [WebSysDescription ("Visiblity state of the control.")]
  412. public virtual bool Visible {
  413. get {
  414. if ((stateMask & VISIBLE) == 0)
  415. return false;
  416. if (_parent != null)
  417. return _parent.Visible;
  418. return true;
  419. }
  420. set {
  421. if ((value && (stateMask & VISIBLE) == 0) ||
  422. (!value && (stateMask & VISIBLE) != 0)) {
  423. if (IsTrackingViewState)
  424. stateMask |= VISIBLE_CHANGED;
  425. }
  426. SetMask (VISIBLE, value);
  427. }
  428. }
  429. protected bool ChildControlsCreated {
  430. get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
  431. set {
  432. if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0) {
  433. if (_controls != null)
  434. _controls.Clear();
  435. }
  436. SetMask (CHILD_CONTROLS_CREATED, value);
  437. }
  438. }
  439. [Browsable (false)]
  440. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  441. protected virtual HttpContext Context //DIT
  442. {
  443. get
  444. {
  445. HttpContext context;
  446. if (_context != null)
  447. return _context;
  448. if (_parent == null)
  449. return HttpContext.Current;
  450. context = _parent.Context;
  451. if (context != null)
  452. return context;
  453. return HttpContext.Current;
  454. }
  455. }
  456. protected EventHandlerList Events {
  457. get {
  458. if (_events == null)
  459. _events = new EventHandlerList ();
  460. return _events;
  461. }
  462. }
  463. protected bool HasChildViewState {
  464. get {
  465. return (pendingVS != null && pendingVS.Count > 0);
  466. }
  467. }
  468. protected bool IsTrackingViewState {
  469. get { return ((stateMask & TRACK_VIEWSTATE) != 0); }
  470. }
  471. [Browsable (false)]
  472. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  473. [WebSysDescription ("ViewState")]
  474. protected virtual StateBag ViewState
  475. {
  476. get
  477. {
  478. if(_viewState == null)
  479. _viewState = new StateBag (ViewStateIgnoresCase);
  480. if (IsTrackingViewState)
  481. _viewState.TrackViewState ();
  482. return _viewState;
  483. }
  484. }
  485. [Browsable (false)]
  486. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  487. protected virtual bool ViewStateIgnoresCase
  488. {
  489. get {
  490. return false;
  491. }
  492. }
  493. internal bool AutoEventWireup {
  494. get { return (stateMask & AUTO_EVENT_WIREUP) != 0; }
  495. set { SetMask (AUTO_EVENT_WIREUP, value); }
  496. }
  497. internal void SetBindingContainer (bool isBC)
  498. {
  499. SetMask (BINDING_CONTAINER, isBC);
  500. }
  501. internal void ResetChildNames ()
  502. {
  503. defaultNumberID = 0;
  504. }
  505. string GetDefaultName ()
  506. {
  507. string defaultName;
  508. if (defaultNumberID > 99) {
  509. #if NET_2_0
  510. defaultName = "ctl" + defaultNumberID++;
  511. #else
  512. defaultName = "_ctl" + defaultNumberID++;
  513. #endif
  514. } else {
  515. defaultName = defaultNameArray [defaultNumberID++];
  516. }
  517. return defaultName;
  518. }
  519. void NullifyUniqueID ()
  520. {
  521. uniqueID = null;
  522. if (!HasControls ())
  523. return;
  524. foreach (Control c in _controls)
  525. c.NullifyUniqueID ();
  526. }
  527. protected internal virtual void AddedControl (Control control, int index)
  528. {
  529. /* Ensure the control don't have more than 1 parent */
  530. if (control._parent != null)
  531. control._parent.Controls.Remove (control);
  532. control._parent = this;
  533. control._page = _page;
  534. Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
  535. if (nc != null) {
  536. control._namingContainer = nc;
  537. if (control.AutoID == true && control._userId == null)
  538. control._userId = nc.GetDefaultName ();
  539. }
  540. if ((stateMask & (INITING | INITED)) != 0)
  541. control.InitRecursive (nc);
  542. if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
  543. if (pendingVS != null) {
  544. object vs = pendingVS [index];
  545. if (vs != null) {
  546. pendingVS.Remove (index);
  547. if (pendingVS.Count == 0)
  548. pendingVS = null;
  549. control.LoadViewStateRecursive (vs);
  550. }
  551. }
  552. }
  553. if ((stateMask & LOADED) != 0)
  554. control.LoadRecursive ();
  555. if ((stateMask & PRERENDERED) != 0)
  556. control.PreRenderRecursiveInternal ();
  557. }
  558. protected virtual void AddParsedSubObject(object obj) //DIT
  559. {
  560. Control c = obj as Control;
  561. if (c != null) Controls.Add(c);
  562. }
  563. #if NET_2_0
  564. [EditorBrowsable (EditorBrowsableState.Advanced)]
  565. public virtual void ApplyStyleSheetSkin (Page page)
  566. {
  567. if (!EnableTheming) /* this enough? */
  568. return;
  569. /* apply the style sheet skin here */
  570. if (page.StyleSheetPageTheme != null) {
  571. ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType(), SkinID);
  572. if (cs != null)
  573. cs.ApplySkin (this);
  574. }
  575. }
  576. #endif
  577. protected void BuildProfileTree(string parentId, bool calcViewState)
  578. {
  579. //TODO
  580. }
  581. #if NET_2_0
  582. protected void ClearChildControlState ()
  583. {
  584. _isChildControlStateCleared = true;
  585. }
  586. protected void ClearChildState ()
  587. {
  588. ClearChildViewState ();
  589. ClearChildControlState ();
  590. }
  591. #endif
  592. protected void ClearChildViewState ()
  593. {
  594. pendingVS = null;
  595. }
  596. #if NET_2_0
  597. protected internal
  598. #else
  599. protected
  600. #endif
  601. virtual void CreateChildControls() {} //DIT
  602. protected virtual ControlCollection CreateControlCollection() //DIT
  603. {
  604. return new ControlCollection(this);
  605. }
  606. protected virtual void EnsureChildControls ()
  607. {
  608. if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
  609. stateMask |= CREATING_CONTROLS;
  610. #if NET_2_0
  611. if (Adapter != null)
  612. Adapter.CreateChildControls ();
  613. else
  614. #endif
  615. CreateChildControls();
  616. ChildControlsCreated = true;
  617. stateMask &= ~CREATING_CONTROLS;
  618. }
  619. }
  620. #if NET_2_0
  621. protected void EnsureID ()
  622. {
  623. if (Page == null)
  624. return;
  625. ID = NamingContainer.GetDefaultName ();
  626. }
  627. protected bool HasEvents ()
  628. {
  629. return _events != null;
  630. }
  631. #endif
  632. protected bool IsLiteralContent()
  633. {
  634. if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
  635. return true;
  636. return false;
  637. }
  638. [WebSysDescription ("")]
  639. public virtual Control FindControl (string id)
  640. {
  641. return FindControl (id, 0);
  642. }
  643. Control LookForControlByName (string id)
  644. {
  645. #if TARGET_J2EE
  646. if (this == Page && id != null && id == Page.PortletNamespace)
  647. return this;
  648. #endif
  649. if (!HasControls ())
  650. return null;
  651. Control result = null;
  652. foreach (Control c in _controls) {
  653. if (String.Compare (id, c._userId, true, CultureInfo.InvariantCulture) == 0) {
  654. if (result != null && result != c) {
  655. throw new HttpException ("1 Found more than one control with ID '" + id + "'");
  656. }
  657. result = c;
  658. continue;
  659. }
  660. if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ()) {
  661. Control child = c.LookForControlByName (id);
  662. if (child != null) {
  663. if (result != null && result != child)
  664. throw new HttpException ("2 Found more than one control with ID '" + id + "'");
  665. result = child;
  666. }
  667. }
  668. }
  669. return result;
  670. }
  671. protected virtual Control FindControl (string id, int pathOffset)
  672. {
  673. EnsureChildControls ();
  674. Control namingContainer = null;
  675. if ((stateMask & IS_NAMING_CONTAINER) == 0) {
  676. namingContainer = NamingContainer;
  677. if (namingContainer == null)
  678. return null;
  679. return namingContainer.FindControl (id, pathOffset);
  680. }
  681. if (!HasControls ())
  682. return null;
  683. #if NET_2_0
  684. int separatorIdx = id.IndexOf (IdSeparator, pathOffset);
  685. #else
  686. int separatorIdx = id.IndexOf (':', pathOffset);
  687. #endif
  688. if (separatorIdx == -1)
  689. return LookForControlByName (id.Substring (pathOffset));
  690. string idfound = id.Substring (pathOffset, separatorIdx - pathOffset);
  691. namingContainer = LookForControlByName (idfound);
  692. if (namingContainer == null)
  693. return null;
  694. return namingContainer.FindControl (id, separatorIdx + 1);
  695. }
  696. protected virtual void LoadViewState(object savedState)
  697. {
  698. if (savedState != null) {
  699. ViewState.LoadViewState (savedState);
  700. object o = ViewState ["Visible"];
  701. if (o != null) {
  702. SetMask (VISIBLE, (bool) o);
  703. stateMask |= VISIBLE_CHANGED;
  704. }
  705. }
  706. }
  707. // [MonoTODO("Secure?")]
  708. protected string MapPathSecure(string virtualPath)
  709. {
  710. string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  711. return Context.Request.MapPath (combined);
  712. }
  713. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  714. {
  715. #if MONO_TRACE
  716. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  717. string type_name = null;
  718. if (trace != null) {
  719. type_name = GetType ().Name;
  720. trace.Write ("control", String.Format ("OnBubbleEvent {0} {1}", _userId, type_name));
  721. }
  722. #endif
  723. return false;
  724. }
  725. protected virtual void OnDataBinding (EventArgs e)
  726. {
  727. if ((event_mask & databinding_mask) != 0) {
  728. EventHandler eh = (EventHandler)(_events [DataBindingEvent]);
  729. if (eh != null) {
  730. #if MONO_TRACE
  731. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  732. string type_name = null;
  733. if (trace != null) {
  734. type_name = GetType ().Name;
  735. trace.Write ("control", String.Format ("OnDataBinding {0} {1}", _userId, type_name));
  736. }
  737. #endif
  738. eh (this, e);
  739. }
  740. }
  741. }
  742. #if NET_2_0
  743. protected internal
  744. #else
  745. protected
  746. #endif
  747. virtual void OnInit (EventArgs e)
  748. {
  749. if ((event_mask & init_mask) != 0) {
  750. EventHandler eh = (EventHandler)(_events [InitEvent]);
  751. if (eh != null) {
  752. #if MONO_TRACE
  753. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  754. string type_name = null;
  755. if (trace != null) {
  756. type_name = GetType ().Name;
  757. trace.Write ("control", String.Format ("OnInit {0} {1}", _userId, type_name));
  758. }
  759. #endif
  760. eh (this, e);
  761. }
  762. }
  763. }
  764. #if NET_2_0
  765. protected internal
  766. #else
  767. protected
  768. #endif
  769. virtual void OnLoad (EventArgs e)
  770. {
  771. if ((event_mask & load_mask) != 0) {
  772. EventHandler eh = (EventHandler)(_events [LoadEvent]);
  773. if (eh != null) {
  774. #if MONO_TRACE
  775. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  776. string type_name = null;
  777. if (trace != null) {
  778. type_name = GetType ().Name;
  779. trace.Write ("control", String.Format ("OnLoad {0} {1}", _userId, type_name));
  780. }
  781. #endif
  782. eh (this, e);
  783. }
  784. }
  785. }
  786. #if NET_2_0
  787. protected internal
  788. #else
  789. protected
  790. #endif
  791. virtual void OnPreRender (EventArgs e)
  792. {
  793. if ((event_mask & prerender_mask) != 0) {
  794. EventHandler eh = (EventHandler)(_events [PreRenderEvent]);
  795. if (eh != null) {
  796. #if MONO_TRACE
  797. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  798. string type_name = null;
  799. if (trace != null) {
  800. type_name = GetType ().Name;
  801. trace.Write ("control", String.Format ("OnPreRender {0} {1}", _userId, type_name));
  802. }
  803. #endif
  804. eh (this, e);
  805. }
  806. }
  807. }
  808. #if NET_2_0
  809. protected internal
  810. #else
  811. protected
  812. #endif
  813. virtual void OnUnload(EventArgs e)
  814. {
  815. if ((event_mask & unload_mask) != 0) {
  816. EventHandler eh = (EventHandler)(_events [UnloadEvent]);
  817. if (eh != null) {
  818. #if MONO_TRACE
  819. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  820. string type_name = null;
  821. if (trace != null) {
  822. type_name = GetType ().Name;
  823. trace.Write ("control", String.Format ("OnUnload {0} {1}", _userId, type_name));
  824. }
  825. #endif
  826. eh (this, e);
  827. }
  828. }
  829. }
  830. #if NET_2_0
  831. protected internal Stream OpenFile (string path)
  832. {
  833. try {
  834. string filePath = Context.Server.MapPath (path);
  835. return File.OpenRead (filePath);
  836. }
  837. catch (UnauthorizedAccessException) {
  838. throw new HttpException ("Access to the specified file was denied.");
  839. }
  840. }
  841. internal string GetPhysicalFilePath (string virtualPath)
  842. {
  843. Page page = Page;
  844. if (VirtualPathUtility.IsAbsolute (virtualPath))
  845. return page != null ? page.MapPath (virtualPath) : Context.Server.MapPath (virtualPath);
  846. // We need to determine whether one of our parents is a
  847. // master page. If so, we need to map the path
  848. // relatively to the master page and not our containing
  849. // page/control. This is necessary for cases when a
  850. // relative path is used in a control placed in a master
  851. // page and the master page is referenced from a
  852. // location other than its own. In such cases MS.NET looks
  853. // for the file in the directory where the master page
  854. // is.
  855. //
  856. // An example of where it is needed is at
  857. //
  858. // http://quickstarts.asp.net/QuickStartv20/aspnet/samples/masterpages/masterpages_cs/pages/default.aspx
  859. //
  860. MasterPage master = null;
  861. Control ctrl = Parent;
  862. while (ctrl != null) {
  863. if (ctrl is MasterPage) {
  864. master = ctrl as MasterPage;
  865. break;
  866. }
  867. ctrl = ctrl.Parent;
  868. }
  869. string path;
  870. if (master != null)
  871. path = VirtualPathUtility.Combine (master.TemplateSourceDirectory, virtualPath);
  872. else
  873. path = VirtualPathUtility.Combine (TemplateSourceDirectory, virtualPath);
  874. return page != null ? page.MapPath (path) : Context.Server.MapPath (path);
  875. }
  876. #endif
  877. protected void RaiseBubbleEvent(object source, EventArgs args)
  878. {
  879. Control c = Parent;
  880. while (c != null) {
  881. #if MONO_TRACE
  882. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  883. string type_name = null;
  884. if (trace != null) {
  885. type_name = GetType ().Name;
  886. trace.Write ("control", String.Format ("RaiseBubbleEvent {0} {1}", _userId, type_name));
  887. }
  888. #endif
  889. if (c.OnBubbleEvent (source, args)) {
  890. #if MONO_TRACE
  891. if (trace != null)
  892. trace.Write ("control", String.Format ("End RaiseBubbleEvent (false) {0} {1}", _userId, type_name));
  893. #endif
  894. break;
  895. }
  896. #if MONO_TRACE
  897. if (trace != null)
  898. trace.Write ("control", String.Format ("End RaiseBubbleEvent (true) {0} {1}", _userId, type_name));
  899. #endif
  900. c = c.Parent;
  901. }
  902. }
  903. #if NET_2_0
  904. protected internal
  905. #else
  906. protected
  907. #endif
  908. virtual void Render(HtmlTextWriter writer) //DIT
  909. {
  910. RenderChildren(writer);
  911. }
  912. #if NET_2_0
  913. protected internal
  914. #else
  915. protected
  916. #endif
  917. virtual void RenderChildren (HtmlTextWriter writer) //DIT
  918. {
  919. if (_renderMethodDelegate != null) {
  920. _renderMethodDelegate (writer, this);
  921. } else if (_controls != null) {
  922. int len = _controls.Count;
  923. for (int i = 0; i < len; i++) {
  924. Control c = _controls [i];
  925. #if NET_2_0
  926. if (c.Adapter != null)
  927. c.RenderControl (writer, c.Adapter);
  928. else
  929. #endif
  930. c.RenderControl (writer);
  931. }
  932. }
  933. }
  934. #if NET_2_0
  935. protected virtual ControlAdapter ResolveAdapter ()
  936. {
  937. throw new NotImplementedException ();
  938. }
  939. #endif
  940. protected virtual object SaveViewState ()
  941. {
  942. if ((stateMask & VISIBLE_CHANGED) != 0) {
  943. ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
  944. } else if (_viewState == null) {
  945. return null;
  946. }
  947. return _viewState.SaveViewState ();
  948. }
  949. protected virtual void TrackViewState()
  950. {
  951. if (_viewState != null)
  952. _viewState.TrackViewState ();
  953. stateMask |= TRACK_VIEWSTATE;
  954. }
  955. public virtual void Dispose ()
  956. {
  957. if ((event_mask & disposed_mask) != 0) {
  958. EventHandler eh = (EventHandler)(_events [DisposedEvent]);
  959. if (eh != null) eh (this, EventArgs.Empty);
  960. }
  961. }
  962. [WebCategory ("FIXME")]
  963. [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
  964. public event EventHandler DataBinding {
  965. add {
  966. event_mask |= databinding_mask;
  967. Events.AddHandler (DataBindingEvent, value);
  968. }
  969. remove { Events.RemoveHandler (DataBindingEvent, value); }
  970. }
  971. [WebSysDescription ("Raised when the contol is disposed.")]
  972. public event EventHandler Disposed {
  973. add {
  974. event_mask |= disposed_mask;
  975. Events.AddHandler (DisposedEvent, value);
  976. }
  977. remove { Events.RemoveHandler (DisposedEvent, value); }
  978. }
  979. [WebSysDescription ("Raised when the page containing the control is initialized.")]
  980. public event EventHandler Init {
  981. add {
  982. event_mask |= init_mask;
  983. Events.AddHandler (InitEvent, value);
  984. }
  985. remove { Events.RemoveHandler (InitEvent, value); }
  986. }
  987. [WebSysDescription ("Raised after the page containing the control has been loaded.")]
  988. public event EventHandler Load {
  989. add {
  990. event_mask |= load_mask;
  991. Events.AddHandler (LoadEvent, value);
  992. }
  993. remove { Events.RemoveHandler (LoadEvent, value); }
  994. }
  995. [WebSysDescription ("Raised before the page containing the control is rendered.")]
  996. public event EventHandler PreRender {
  997. add {
  998. event_mask |= prerender_mask;
  999. Events.AddHandler (PreRenderEvent, value);
  1000. }
  1001. remove { Events.RemoveHandler (PreRenderEvent, value); }
  1002. }
  1003. [WebSysDescription ("Raised when the page containing the control is unloaded.")]
  1004. public event EventHandler Unload {
  1005. add {
  1006. event_mask |= unload_mask;
  1007. Events.AddHandler (UnloadEvent, value);
  1008. }
  1009. remove { Events.RemoveHandler (UnloadEvent, value); }
  1010. }
  1011. public virtual void DataBind() //DIT
  1012. {
  1013. #if NET_2_0
  1014. DataBind (true);
  1015. #else
  1016. OnDataBinding (EventArgs.Empty);
  1017. DataBindChildren();
  1018. #endif
  1019. }
  1020. #if NET_2_0
  1021. protected virtual
  1022. #endif
  1023. void DataBindChildren ()
  1024. {
  1025. if (!HasControls ())
  1026. return;
  1027. int len = _controls.Count;
  1028. for (int i = 0; i < len; i++) {
  1029. Control c = _controls [i];
  1030. c.DataBind ();
  1031. }
  1032. }
  1033. public virtual bool HasControls ()
  1034. {
  1035. return (_controls != null && _controls.Count > 0);
  1036. }
  1037. #if NET_2_0
  1038. public virtual
  1039. #else
  1040. public
  1041. #endif
  1042. void RenderControl (HtmlTextWriter writer)
  1043. {
  1044. if ((stateMask & VISIBLE) != 0) {
  1045. HttpContext ctx = Context;
  1046. TraceContext trace = (ctx != null) ? ctx.Trace : null;
  1047. int pos = 0;
  1048. if ((trace != null) && trace.IsEnabled)
  1049. pos = ctx.Response.GetOutputByteCount ();
  1050. Render(writer);
  1051. if ((trace != null) && trace.IsEnabled) {
  1052. int size = ctx.Response.GetOutputByteCount () - pos;
  1053. trace.SaveSize (this, size >= 0 ? size : 0);
  1054. }
  1055. }
  1056. }
  1057. #if NET_2_0
  1058. protected void RenderControl (HtmlTextWriter writer,
  1059. ControlAdapter adapter)
  1060. {
  1061. if ((stateMask & VISIBLE) != 0) {
  1062. adapter.BeginRender (writer);
  1063. adapter.Render (writer);
  1064. adapter.EndRender (writer);
  1065. }
  1066. }
  1067. #endif
  1068. public string ResolveUrl (string relativeUrl)
  1069. {
  1070. if (relativeUrl == null)
  1071. throw new ArgumentNullException ("relativeUrl");
  1072. if (relativeUrl == "")
  1073. return "";
  1074. if (relativeUrl [0] == '#')
  1075. return relativeUrl;
  1076. string ts = TemplateSourceDirectory;
  1077. if (ts == null || ts.Length == 0 ||
  1078. Context == null || Context.Response == null ||
  1079. !UrlUtils.IsRelativeUrl (relativeUrl))
  1080. return relativeUrl;
  1081. HttpResponse resp = Context.Response;
  1082. return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
  1083. }
  1084. #if NET_2_0
  1085. public
  1086. #else
  1087. internal
  1088. #endif
  1089. string ResolveClientUrl (string relativeUrl)
  1090. {
  1091. #if TARGET_J2EE
  1092. // There are no relative paths when rendering a J2EE portlet
  1093. if (Page != null && Page.PortletNamespace != null)
  1094. return ResolveUrl (relativeUrl);
  1095. #endif
  1096. if (relativeUrl == null)
  1097. throw new ArgumentNullException ("relativeUrl");
  1098. if (relativeUrl.Length == 0)
  1099. return String.Empty;
  1100. if (VirtualPathUtility.IsAbsolute (relativeUrl) || relativeUrl.IndexOf (':') >= 0)
  1101. return relativeUrl;
  1102. HttpContext context = Context;
  1103. if (context != null && context.Request != null) {
  1104. string basePath = context.Request.CurrentExecutionFilePath;
  1105. if (basePath.Length > 1 && basePath [basePath.Length - 1] != '/') {
  1106. basePath = VirtualPathUtility.GetDirectory (basePath);
  1107. }
  1108. if(VirtualPathUtility.IsAppRelative(relativeUrl))
  1109. return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
  1110. string templateSourceDirectory = TemplateSourceDirectory;
  1111. if (templateSourceDirectory == null || templateSourceDirectory.Length == 0)
  1112. return relativeUrl;
  1113. string templatePath = VirtualPathUtility.AppendTrailingSlash (templateSourceDirectory);
  1114. if (basePath.Length == templatePath.Length && String.CompareOrdinal (basePath, templatePath) == 0)
  1115. return relativeUrl;
  1116. relativeUrl = VirtualPathUtility.Combine (templatePath, relativeUrl);
  1117. return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
  1118. }
  1119. return relativeUrl;
  1120. }
  1121. internal bool HasRenderMethodDelegate () {
  1122. return _renderMethodDelegate != null;
  1123. }
  1124. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1125. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  1126. {
  1127. _renderMethodDelegate = renderMethod;
  1128. }
  1129. internal void LoadRecursive()
  1130. {
  1131. #if MONO_TRACE
  1132. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1133. string type_name = null;
  1134. if (trace != null) {
  1135. type_name = GetType ().Name;
  1136. trace.Write ("control", String.Format ("LoadRecursive {0} {1}", _userId, type_name));
  1137. }
  1138. #endif
  1139. #if NET_2_0
  1140. if (Adapter != null)
  1141. Adapter.OnLoad (EventArgs.Empty);
  1142. else
  1143. #endif
  1144. OnLoad (EventArgs.Empty);
  1145. if (HasControls ()) {
  1146. int len = _controls.Count;
  1147. for (int i=0;i<len;i++)
  1148. {
  1149. Control c = _controls[i];
  1150. c.LoadRecursive ();
  1151. }
  1152. }
  1153. #if MONO_TRACE
  1154. if (trace != null)
  1155. trace.Write ("control", String.Format ("End LoadRecursive {0} {1}", _userId, type_name));
  1156. #endif
  1157. stateMask |= LOADED;
  1158. }
  1159. internal void UnloadRecursive(Boolean dispose)
  1160. {
  1161. #if MONO_TRACE
  1162. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1163. string type_name = null;
  1164. if (trace != null) {
  1165. type_name = GetType ().Name;
  1166. trace.Write ("control", String.Format ("UnloadRecursive {0} {1}", _userId, type_name));
  1167. }
  1168. #endif
  1169. if (HasControls ()) {
  1170. int len = _controls.Count;
  1171. for (int i=0;i<len;i++)
  1172. {
  1173. Control c = _controls[i];
  1174. c.UnloadRecursive (dispose);
  1175. }
  1176. }
  1177. #if MONO_TRACE
  1178. if (trace != null)
  1179. trace.Write ("control", String.Format ("End UnloadRecursive {0} {1}", _userId, type_name));
  1180. #endif
  1181. #if NET_2_0
  1182. if (Adapter != null)
  1183. Adapter.OnUnload (EventArgs.Empty);
  1184. else
  1185. #endif
  1186. OnUnload (EventArgs.Empty);
  1187. if (dispose)
  1188. Dispose();
  1189. }
  1190. internal void PreRenderRecursiveInternal()
  1191. {
  1192. if ((stateMask & VISIBLE) != 0) {
  1193. EnsureChildControls ();
  1194. #if MONO_TRACE
  1195. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1196. string type_name = null;
  1197. if (trace != null) {
  1198. type_name = GetType ().Name;
  1199. trace.Write ("control", String.Format ("PreRenderRecursive {0} {1}", _userId, type_name));
  1200. }
  1201. #endif
  1202. #if NET_2_0
  1203. if (Adapter != null)
  1204. Adapter.OnPreRender (EventArgs.Empty);
  1205. else
  1206. #endif
  1207. OnPreRender (EventArgs.Empty);
  1208. if (!HasControls ())
  1209. return;
  1210. int len = _controls.Count;
  1211. for (int i=0;i<len;i++)
  1212. {
  1213. Control c = _controls[i];
  1214. c.PreRenderRecursiveInternal ();
  1215. }
  1216. #if MONO_TRACE
  1217. if (trace != null)
  1218. trace.Write ("control", String.Format ("End PreRenderRecursive {0} {1}", _userId, type_name));
  1219. #endif
  1220. }
  1221. stateMask |= PRERENDERED;
  1222. }
  1223. internal void InitRecursive(Control namingContainer)
  1224. {
  1225. #if MONO_TRACE
  1226. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1227. string type_name = null;
  1228. if (trace != null) {
  1229. type_name = GetType ().Name;
  1230. trace.Write ("control", String.Format ("InitRecursive {0} {1}", _userId, type_name));
  1231. }
  1232. #endif
  1233. if (HasControls ()) {
  1234. if ((stateMask & IS_NAMING_CONTAINER) != 0)
  1235. namingContainer = this;
  1236. if (namingContainer != null &&
  1237. namingContainer._userId == null &&
  1238. namingContainer.AutoID)
  1239. namingContainer._userId = namingContainer.GetDefaultName () + "b";
  1240. int len = _controls.Count;
  1241. for (int i=0;i<len;i++)
  1242. {
  1243. Control c = _controls[i];
  1244. c._page = Page;
  1245. c._namingContainer = namingContainer;
  1246. if (namingContainer != null && c._userId == null && c.AutoID)
  1247. c._userId = namingContainer.GetDefaultName () + "c";
  1248. c.InitRecursive (namingContainer);
  1249. }
  1250. }
  1251. stateMask |= INITING;
  1252. #if NET_2_0
  1253. ApplyTheme ();
  1254. if (Adapter != null)
  1255. Adapter.OnInit (EventArgs.Empty);
  1256. else
  1257. #endif
  1258. OnInit (EventArgs.Empty);
  1259. #if MONO_TRACE
  1260. if (trace != null)
  1261. trace.Write ("control", String.Format ("End InitRecursive {0} {1}", _userId, type_name));
  1262. #endif
  1263. TrackViewState ();
  1264. stateMask |= INITED;
  1265. stateMask &= ~INITING;
  1266. }
  1267. internal object SaveViewStateRecursive ()
  1268. {
  1269. if (!EnableViewState)
  1270. return null;
  1271. #if MONO_TRACE
  1272. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1273. string type_name = null;
  1274. if (trace != null) {
  1275. type_name = GetType ().Name;
  1276. trace.Write ("control", String.Format ("SaveViewStateRecursive {0} {1}", _userId, type_name));
  1277. }
  1278. #endif
  1279. ArrayList controlList = null;
  1280. ArrayList controlStates = null;
  1281. int idx = -1;
  1282. if (HasControls ())
  1283. {
  1284. int len = _controls.Count;
  1285. for (int i=0;i<len;i++)
  1286. {
  1287. Control ctrl = _controls[i];
  1288. object ctrlState = ctrl.SaveViewStateRecursive ();
  1289. idx++;
  1290. if (ctrlState == null)
  1291. continue;
  1292. if (controlList == null)
  1293. {
  1294. controlList = new ArrayList ();
  1295. controlStates = new ArrayList ();
  1296. }
  1297. controlList.Add (idx);
  1298. controlStates.Add (ctrlState);
  1299. }
  1300. }
  1301. object thisState = SaveViewState ();
  1302. if (thisState == null && controlList == null && controlStates == null) {
  1303. #if MONO_TRACE
  1304. if (trace != null) {
  1305. trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved nothing", _userId, type_name));
  1306. trace.SaveViewState (this, null);
  1307. }
  1308. #endif
  1309. return null;
  1310. }
  1311. #if MONO_TRACE
  1312. if (trace != null) {
  1313. trace.Write ("control", String.Format ("End SaveViewStateRecursive {0} {1} saved a Triplet", _userId, type_name));
  1314. trace.SaveViewState (this, thisState);
  1315. }
  1316. #endif
  1317. return new Triplet (thisState, controlList, controlStates);
  1318. }
  1319. internal void LoadViewStateRecursive (object savedState)
  1320. {
  1321. if (!EnableViewState || savedState == null)
  1322. return;
  1323. #if MONO_TRACE
  1324. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1325. string type_name = null;
  1326. if (trace != null) {
  1327. type_name = GetType ().Name;
  1328. trace.Write ("control", String.Format ("LoadViewStateRecursive {0} {1}", _userId, type_name));
  1329. }
  1330. #endif
  1331. Triplet savedInfo = (Triplet) savedState;
  1332. LoadViewState (savedInfo.First);
  1333. ArrayList controlList = savedInfo.Second as ArrayList;
  1334. if (controlList == null)
  1335. return;
  1336. ArrayList controlStates = savedInfo.Third as ArrayList;
  1337. int nControls = controlList.Count;
  1338. for (int i = 0; i < nControls; i++) {
  1339. int k = (int) controlList [i];
  1340. if (k < Controls.Count && controlStates != null) {
  1341. Control c = Controls [k];
  1342. c.LoadViewStateRecursive (controlStates [i]);
  1343. } else {
  1344. if (pendingVS == null)
  1345. pendingVS = new Hashtable ();
  1346. pendingVS [k] = controlStates [i];
  1347. }
  1348. }
  1349. #if MONO_TRACE
  1350. if (trace != null)
  1351. trace.Write ("control", String.Format ("End LoadViewStateRecursive {0} {1}", _userId, type_name));
  1352. #endif
  1353. stateMask |= VIEWSTATE_LOADED;
  1354. }
  1355. #if NET_2_0
  1356. internal ControlSkin controlSkin;
  1357. internal void ApplyTheme ()
  1358. {
  1359. #if MONO_TRACE
  1360. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1361. string type_name = null;
  1362. if (trace != null) {
  1363. type_name = GetType ().Name;
  1364. trace.Write ("control", String.Format ("ApplyThemeRecursive {0} {1}", _userId, type_name));
  1365. }
  1366. #endif
  1367. if (Page.PageTheme != null && EnableTheming) {
  1368. ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
  1369. if (controlSkin != null)
  1370. controlSkin.ApplySkin (this);
  1371. }
  1372. #if MONO_TRACE
  1373. if (trace != null)
  1374. trace.Write ("control", String.Format ("End ApplyThemeRecursive {0} {1}", _userId, type_name));
  1375. #endif
  1376. }
  1377. #endif
  1378. internal bool AutoID
  1379. {
  1380. get { return (stateMask & AUTOID) != 0; }
  1381. set {
  1382. if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
  1383. return;
  1384. SetMask (AUTOID, value);
  1385. }
  1386. }
  1387. protected internal virtual void RemovedControl (Control control)
  1388. {
  1389. control.UnloadRecursive (false);
  1390. control._parent = null;
  1391. control._page = null;
  1392. control._namingContainer = null;
  1393. }
  1394. #if NET_2_0
  1395. string skinId = string.Empty;
  1396. bool _enableTheming = true;
  1397. [Browsable (false)]
  1398. [Themeable (false)]
  1399. [DefaultValue (true)]
  1400. public virtual bool EnableTheming
  1401. {
  1402. get
  1403. {
  1404. if ((stateMask & ENABLE_THEMING) != 0)
  1405. return _enableTheming;
  1406. if (_parent != null)
  1407. return _parent.EnableTheming;
  1408. return true;
  1409. }
  1410. set
  1411. {
  1412. SetMask (ENABLE_THEMING, true);
  1413. _enableTheming = value;
  1414. }
  1415. }
  1416. [Browsable (false)]
  1417. [DefaultValue ("")]
  1418. [Filterable (false)]
  1419. public virtual string SkinID
  1420. {
  1421. get { return skinId; }
  1422. set { skinId = value; }
  1423. }
  1424. ControlBuilder IControlBuilderAccessor.ControlBuilder {
  1425. get {throw new NotImplementedException (); }
  1426. }
  1427. IDictionary IControlDesignerAccessor.GetDesignModeState ()
  1428. {
  1429. throw new NotImplementedException ();
  1430. }
  1431. void IControlDesignerAccessor.SetDesignModeState (IDictionary designData)
  1432. {
  1433. SetDesignModeState (designData);
  1434. }
  1435. void IControlDesignerAccessor.SetOwnerControl (Control control)
  1436. {
  1437. throw new NotImplementedException ();
  1438. }
  1439. IDictionary IControlDesignerAccessor.UserData {
  1440. get { throw new NotImplementedException (); }
  1441. }
  1442. ExpressionBindingCollection expressionBindings;
  1443. ExpressionBindingCollection IExpressionsAccessor.Expressions {
  1444. get {
  1445. if (expressionBindings == null)
  1446. expressionBindings = new ExpressionBindingCollection ();
  1447. return expressionBindings;
  1448. }
  1449. }
  1450. bool IExpressionsAccessor.HasExpressions {
  1451. get {
  1452. return (expressionBindings != null && expressionBindings.Count > 0);
  1453. }
  1454. }
  1455. public virtual void Focus()
  1456. {
  1457. Page.SetFocus (this);
  1458. }
  1459. protected internal virtual void LoadControlState (object state)
  1460. {
  1461. }
  1462. protected internal virtual object SaveControlState ()
  1463. {
  1464. return null;
  1465. }
  1466. protected virtual void DataBind (bool raiseOnDataBinding)
  1467. {
  1468. bool foundDataItem = false;
  1469. if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
  1470. object o = DataBinder.GetDataItem (this, out foundDataItem);
  1471. if (foundDataItem)
  1472. Page.PushDataItemContext (o);
  1473. }
  1474. try {
  1475. if (raiseOnDataBinding)
  1476. OnDataBinding (EventArgs.Empty);
  1477. DataBindChildren();
  1478. } finally {
  1479. if (foundDataItem)
  1480. Page.PopDataItemContext ();
  1481. }
  1482. }
  1483. protected virtual IDictionary GetDesignModeState ()
  1484. {
  1485. throw new NotImplementedException ();
  1486. }
  1487. protected virtual void SetDesignModeState (IDictionary data)
  1488. {
  1489. throw new NotImplementedException ();
  1490. }
  1491. #endif
  1492. void IParserAccessor.AddParsedSubObject (object obj) {
  1493. this.AddParsedSubObject (obj);
  1494. }
  1495. DataBindingCollection IDataBindingsAccessor.DataBindings {
  1496. get {
  1497. if (dataBindings == null) {
  1498. dataBindings = new DataBindingCollection ();
  1499. }
  1500. return dataBindings;
  1501. }
  1502. }
  1503. bool IDataBindingsAccessor.HasDataBindings {
  1504. get {
  1505. if (dataBindings != null && dataBindings.Count > 0) {
  1506. return true;
  1507. }
  1508. return false;
  1509. }
  1510. }
  1511. }
  1512. }