Control.cs 50 KB

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