Control.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110
  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. using System;
  35. using System.Collections;
  36. using System.ComponentModel;
  37. using System.ComponentModel.Design;
  38. using System.ComponentModel.Design.Serialization;
  39. using System.Web;
  40. using System.Web.Util;
  41. namespace System.Web.UI
  42. {
  43. [DefaultProperty ("ID"), DesignerCategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require)]
  44. [ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, " + Consts.AssemblySystem_Design)]
  45. [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  46. [DesignerSerializer ("Microsoft.VSDesigner.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner,
  47. "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  48. public class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
  49. #if NET_2_0
  50. , IUrlResolutionService, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
  51. #endif
  52. {
  53. static readonly object DataBindingEvent = new object();
  54. static readonly object DisposedEvent = new object();
  55. static readonly object InitEvent = new object();
  56. static readonly object LoadEvent = new object();
  57. static readonly object PreRenderEvent = new object();
  58. static readonly object UnloadEvent = new object();
  59. static string[] defaultNameArray;
  60. string uniqueID;
  61. string _userId;
  62. ControlCollection _controls;
  63. IDictionary _childViewStates;
  64. Control _namingContainer;
  65. Page _page;
  66. Control _parent;
  67. ISite _site;
  68. HttpContext _context;
  69. StateBag _viewState;
  70. EventHandlerList _events;
  71. RenderMethod _renderMethodDelegate;
  72. int defaultNumberID;
  73. DataBindingCollection dataBindings;
  74. Hashtable pendingVS; // may hold unused viewstate data from child controls
  75. /*************/
  76. int stateMask;
  77. const int ENABLE_VIEWSTATE = 1;
  78. const int VISIBLE = 1 << 1;
  79. const int AUTOID = 1 << 2;
  80. const int CREATING_CONTROLS = 1 << 3;
  81. const int BINDING_CONTAINER = 1 << 4;
  82. const int AUTO_EVENT_WIREUP = 1 << 5;
  83. const int IS_NAMING_CONTAINER = 1 << 6;
  84. const int VISIBLE_CHANGED = 1 << 7;
  85. const int TRACK_VIEWSTATE = 1 << 8;
  86. const int CHILD_CONTROLS_CREATED = 1 << 9;
  87. const int ID_SET = 1 << 10;
  88. const int INITED = 1 << 11;
  89. const int INITING = 1 << 12;
  90. const int VIEWSTATE_LOADED = 1 << 13;
  91. const int LOADED = 1 << 14;
  92. const int PRERENDERED = 1 << 15;
  93. /*************/
  94. static Control ()
  95. {
  96. defaultNameArray = new string [100];
  97. for (int i = 0 ; i < 100 ; i++)
  98. defaultNameArray [i] = "_ctrl" + i;
  99. }
  100. public Control()
  101. {
  102. stateMask = ENABLE_VIEWSTATE | VISIBLE | AUTOID | BINDING_CONTAINER | AUTO_EVENT_WIREUP;
  103. if (this is INamingContainer)
  104. stateMask |= IS_NAMING_CONTAINER;
  105. }
  106. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  107. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  108. public Control BindingContainer {
  109. get {
  110. Control container = NamingContainer;
  111. if ((container.stateMask & BINDING_CONTAINER) == 0)
  112. container = container.BindingContainer;
  113. return container;
  114. }
  115. }
  116. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  117. [Browsable (false)]
  118. [WebSysDescription ("An Identification of the control that is rendered.")]
  119. public virtual string ClientID {
  120. get {
  121. string client = UniqueID;
  122. if (client != null)
  123. client = client.Replace (':', '_');
  124. return client;
  125. }
  126. }
  127. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  128. [Browsable (false)]
  129. [WebSysDescription ("The child controls of this control.")]
  130. public virtual ControlCollection Controls //DIT
  131. {
  132. get
  133. {
  134. if (_controls == null) _controls = CreateControlCollection();
  135. return _controls;
  136. }
  137. }
  138. [DefaultValue (true), WebCategory ("Behavior")]
  139. [WebSysDescription ("An Identification of the control that is rendered.")]
  140. #if NET_2_0
  141. [Themeable (true)]
  142. #endif
  143. public virtual bool EnableViewState {
  144. get { return ((stateMask & ENABLE_VIEWSTATE) != 0); }
  145. set { SetMask (ENABLE_VIEWSTATE, value); }
  146. }
  147. [MergableProperty (false), ParenthesizePropertyName (true)]
  148. [WebSysDescription ("The name of the control that is rendered.")]
  149. #if NET_2_0
  150. [Filterable (true), Themeable (true)]
  151. #endif
  152. public virtual string ID {
  153. get {
  154. return (((stateMask & ID_SET) != 0) ? _userId : null);
  155. }
  156. set {
  157. if (value == "")
  158. value = null;
  159. stateMask |= ID_SET;
  160. _userId = value;
  161. NullifyUniqueID ();
  162. }
  163. }
  164. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  165. [Browsable (false)]
  166. [WebSysDescription ("The container that this control is part of. The control's name has to be unique within the container.")]
  167. public virtual Control NamingContainer {
  168. get {
  169. if (_namingContainer == null && _parent != null) {
  170. if ((_parent.stateMask & IS_NAMING_CONTAINER) == 0)
  171. _namingContainer = _parent.NamingContainer;
  172. else
  173. _namingContainer = _parent;
  174. }
  175. return _namingContainer;
  176. }
  177. }
  178. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  179. [Browsable (false)]
  180. [WebSysDescription ("The webpage that this control resides on.")]
  181. #if NET_2_0
  182. [Bindable (true)]
  183. #endif
  184. public virtual Page Page //DIT
  185. {
  186. get
  187. {
  188. if (_page == null && _parent != null) _page = _parent.Page;
  189. return _page;
  190. }
  191. set
  192. {
  193. _page = value;
  194. }
  195. }
  196. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  197. [Browsable (false)]
  198. [WebSysDescription ("The parent control of this control.")]
  199. public virtual Control Parent //DIT
  200. {
  201. get
  202. {
  203. return _parent;
  204. }
  205. }
  206. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  207. [EditorBrowsable (EditorBrowsableState.Advanced), Browsable (false)]
  208. [WebSysDescription ("The site this control is part of.")]
  209. public ISite Site //DIT
  210. {
  211. get
  212. {
  213. return _site;
  214. }
  215. set
  216. {
  217. _site = value;
  218. }
  219. }
  220. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  221. [Browsable (false)]
  222. [WebSysDescription ("A virtual directory containing the parent of the control.")]
  223. public virtual string TemplateSourceDirectory {
  224. get { return (_parent == null) ? String.Empty : _parent.TemplateSourceDirectory; }
  225. }
  226. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  227. [Browsable (false)]
  228. [WebSysDescription ("The unique ID of the control.")]
  229. public virtual string UniqueID {
  230. get {
  231. if (uniqueID != null)
  232. return uniqueID;
  233. if (_namingContainer == null) {
  234. return _userId;
  235. }
  236. if (_userId == null)
  237. _userId = _namingContainer.GetDefaultName ();
  238. string prefix = _namingContainer.UniqueID;
  239. if (_namingContainer == _page || prefix == null) {
  240. uniqueID = _userId;
  241. return uniqueID;
  242. }
  243. uniqueID = prefix + ":" + _userId;
  244. return uniqueID;
  245. }
  246. }
  247. void SetMask (int m, bool val)
  248. {
  249. if (val)
  250. stateMask |= m;
  251. else
  252. stateMask &= ~m;
  253. }
  254. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  255. [WebSysDescription ("Visiblity state of the control.")]
  256. #if NET_2_0
  257. [Localizable (true)]
  258. #endif
  259. public virtual bool Visible {
  260. get {
  261. if ((stateMask & VISIBLE) == 0)
  262. return false;
  263. if (_parent != null)
  264. return _parent.Visible;
  265. return true;
  266. }
  267. set {
  268. if ((value && (stateMask & VISIBLE) == 0) ||
  269. (!value && (stateMask & VISIBLE) != 0)) {
  270. if (IsTrackingViewState)
  271. stateMask |= VISIBLE_CHANGED;
  272. }
  273. SetMask (VISIBLE, value);
  274. }
  275. }
  276. protected bool ChildControlsCreated {
  277. get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
  278. set {
  279. if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0)
  280. Controls.Clear();
  281. SetMask (CHILD_CONTROLS_CREATED, value);
  282. }
  283. }
  284. [Browsable (false)]
  285. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  286. protected virtual HttpContext Context //DIT
  287. {
  288. get
  289. {
  290. HttpContext context;
  291. if (_context != null)
  292. return _context;
  293. if (_parent == null)
  294. return HttpContext.Current;
  295. context = _parent.Context;
  296. if (context != null)
  297. return context;
  298. return HttpContext.Current;
  299. }
  300. }
  301. protected EventHandlerList Events //DIT
  302. {
  303. get
  304. {
  305. if (_events == null)
  306. {
  307. _events = new EventHandlerList();
  308. }
  309. return _events;
  310. }
  311. }
  312. protected bool HasChildViewState //DIT
  313. {
  314. get
  315. {
  316. if (_childViewStates == null) return false;
  317. return true;
  318. }
  319. }
  320. protected bool IsTrackingViewState {
  321. get { return ((stateMask & TRACK_VIEWSTATE) != 0); }
  322. }
  323. [Browsable (false)]
  324. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  325. [WebSysDescription ("ViewState")]
  326. protected virtual StateBag ViewState
  327. {
  328. get
  329. {
  330. if(_viewState == null)
  331. _viewState = new StateBag (ViewStateIgnoresCase);
  332. if (IsTrackingViewState)
  333. _viewState.TrackViewState ();
  334. return _viewState;
  335. }
  336. }
  337. [Browsable (false)]
  338. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  339. protected virtual bool ViewStateIgnoresCase
  340. {
  341. get {
  342. return false;
  343. }
  344. }
  345. internal bool AutoEventWireup {
  346. get { return (stateMask & AUTO_EVENT_WIREUP) != 0; }
  347. set { SetMask (AUTO_EVENT_WIREUP, value); }
  348. }
  349. internal void SetBindingContainer (bool isBC)
  350. {
  351. SetMask (BINDING_CONTAINER, isBC);
  352. }
  353. internal void ResetChildNames ()
  354. {
  355. defaultNumberID = 0;
  356. }
  357. string GetDefaultName ()
  358. {
  359. string defaultName;
  360. if (defaultNumberID > 99) {
  361. defaultName = "_ctrl" + defaultNumberID++;
  362. } else {
  363. defaultName = defaultNameArray [defaultNumberID++];
  364. }
  365. return defaultName;
  366. }
  367. void NullifyUniqueID ()
  368. {
  369. uniqueID = null;
  370. if (!HasControls ())
  371. return;
  372. foreach (Control c in Controls)
  373. c.NullifyUniqueID ();
  374. }
  375. protected internal virtual void AddedControl (Control control, int index)
  376. {
  377. /* Ensure the control don't have more than 1 parent */
  378. if (control._parent != null)
  379. control._parent.Controls.Remove (control);
  380. control._parent = this;
  381. control._page = _page;
  382. Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
  383. if (nc != null) {
  384. control._namingContainer = nc;
  385. if (control.AutoID == true && control._userId == null)
  386. control._userId = nc.GetDefaultName () + "a";
  387. }
  388. if ((stateMask & (INITING | INITED)) != 0)
  389. control.InitRecursive (nc);
  390. if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
  391. if (pendingVS != null) {
  392. object vs = pendingVS [index];
  393. if (vs != null) {
  394. pendingVS.Remove (index);
  395. if (pendingVS.Count == 0)
  396. pendingVS = null;
  397. control.LoadViewStateRecursive (vs);
  398. }
  399. }
  400. }
  401. if ((stateMask & LOADED) != 0)
  402. control.LoadRecursive ();
  403. if ((stateMask & PRERENDERED) != 0)
  404. control.PreRenderRecursiveInternal ();
  405. }
  406. protected virtual void AddParsedSubObject(object obj) //DIT
  407. {
  408. WebTrace.PushContext ("Control.AddParsedSubobject ()");
  409. Control c = obj as Control;
  410. WebTrace.WriteLine ("Start: {0} -> {1}", obj, (c != null) ? c.ID : String.Empty);
  411. if (c != null) Controls.Add(c);
  412. WebTrace.WriteLine ("End");
  413. WebTrace.PopContext ();
  414. }
  415. protected void BuildProfileTree(string parentId, bool calcViewState)
  416. {
  417. //TODO
  418. }
  419. protected void ClearChildViewState ()
  420. {
  421. pendingVS = null;
  422. }
  423. protected virtual void CreateChildControls() {} //DIT
  424. protected virtual ControlCollection CreateControlCollection() //DIT
  425. {
  426. return new ControlCollection(this);
  427. }
  428. protected virtual void EnsureChildControls ()
  429. {
  430. if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
  431. stateMask |= CREATING_CONTROLS;
  432. CreateChildControls();
  433. ChildControlsCreated = true;
  434. stateMask &= ~CREATING_CONTROLS;
  435. }
  436. }
  437. protected bool IsLiteralContent()
  438. {
  439. if (HasControls () && Controls.Count == 1 && (Controls [0] is LiteralControl))
  440. return true;
  441. return false;
  442. }
  443. public virtual Control FindControl (string id)
  444. {
  445. return FindControl (id, 0);
  446. }
  447. Control LookForControlByName (string id)
  448. {
  449. if (!HasControls ())
  450. return null;
  451. Control result = null;
  452. foreach (Control c in Controls) {
  453. if (String.Compare (id, c._userId, true) == 0) {
  454. if (result != null && result != c) {
  455. Console.WriteLine (c.GetHashCode ());
  456. Console.WriteLine (result.GetHashCode ());
  457. throw new HttpException ("1 Found more than one control with ID '" + id + "'");
  458. }
  459. result = c;
  460. continue;
  461. }
  462. if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ()) {
  463. Control child = c.LookForControlByName (id);
  464. if (child != null) {
  465. if (result != null && result != child)
  466. throw new HttpException ("2 Found more than one control with ID '" + id + "'");
  467. result = child;
  468. }
  469. }
  470. }
  471. return result;
  472. }
  473. protected virtual Control FindControl (string id, int pathOffset)
  474. {
  475. EnsureChildControls ();
  476. Control namingContainer = null;
  477. if ((stateMask & IS_NAMING_CONTAINER) == 0) {
  478. namingContainer = NamingContainer;
  479. if (namingContainer == null)
  480. return null;
  481. return namingContainer.FindControl (id, pathOffset);
  482. }
  483. if (!HasControls ())
  484. return null;
  485. int colon = id.IndexOf (':', pathOffset);
  486. if (colon == -1)
  487. return LookForControlByName (id.Substring (pathOffset));
  488. string idfound = id.Substring (pathOffset, colon - pathOffset);
  489. namingContainer = LookForControlByName (idfound);
  490. if (namingContainer == null)
  491. return null;
  492. return namingContainer.FindControl (id, colon + 1);
  493. }
  494. protected virtual void LoadViewState(object savedState)
  495. {
  496. if (savedState != null) {
  497. ViewState.LoadViewState (savedState);
  498. object o = ViewState ["Visible"];
  499. if (o != null) {
  500. SetMask (VISIBLE, (bool) o);
  501. stateMask |= VISIBLE_CHANGED;
  502. }
  503. }
  504. }
  505. [MonoTODO("Secure?")]
  506. protected string MapPathSecure(string virtualPath)
  507. {
  508. string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  509. return Context.Request.MapPath (combined);
  510. }
  511. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  512. {
  513. return false;
  514. }
  515. protected virtual void OnDataBinding(EventArgs e) //DIT
  516. {
  517. if (_events != null)
  518. {
  519. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  520. if (eh != null) eh(this, e);
  521. }
  522. }
  523. protected virtual void OnInit(EventArgs e) //DIT
  524. {
  525. if (_events != null)
  526. {
  527. EventHandler eh = (EventHandler)(_events[InitEvent]);
  528. if (eh != null) eh(this, e);
  529. }
  530. }
  531. protected virtual void OnLoad(EventArgs e) //DIT
  532. {
  533. if (_events != null)
  534. {
  535. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  536. if (eh != null) eh(this, e);
  537. }
  538. }
  539. protected virtual void OnPreRender(EventArgs e) //DIT
  540. {
  541. if (_events != null)
  542. {
  543. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  544. if (eh != null) eh(this, e);
  545. }
  546. }
  547. protected virtual void OnUnload(EventArgs e) //DIT
  548. {
  549. if (_events != null)
  550. {
  551. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  552. if (eh != null) eh(this, e);
  553. }
  554. }
  555. protected void RaiseBubbleEvent(object source, EventArgs args)
  556. {
  557. Control c = Parent;
  558. while (c != null) {
  559. if (c.OnBubbleEvent (source, args))
  560. break;
  561. c = c.Parent;
  562. }
  563. }
  564. protected virtual void Render(HtmlTextWriter writer) //DIT
  565. {
  566. RenderChildren(writer);
  567. }
  568. protected virtual void RenderChildren (HtmlTextWriter writer) //DIT
  569. {
  570. if (_renderMethodDelegate != null) {
  571. _renderMethodDelegate (writer, this);
  572. } else if (HasControls ()) {
  573. int len = Controls.Count;
  574. for (int i = 0; i < len; i++) {
  575. Control c = Controls [i];
  576. c.RenderControl (writer);
  577. }
  578. }
  579. }
  580. protected virtual object SaveViewState ()
  581. {
  582. if ((stateMask & VISIBLE_CHANGED) != 0) {
  583. ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
  584. } else if (_viewState == null) {
  585. return null;
  586. }
  587. return _viewState.SaveViewState ();
  588. }
  589. protected virtual void TrackViewState()
  590. {
  591. if (_viewState != null)
  592. _viewState.TrackViewState ();
  593. stateMask |= TRACK_VIEWSTATE;
  594. }
  595. public virtual void Dispose()
  596. {
  597. if (_events != null)
  598. {
  599. EventHandler eh = (EventHandler) _events [DisposedEvent];
  600. if (eh != null)
  601. eh(this, EventArgs.Empty);
  602. }
  603. }
  604. [WebCategory ("FIXME")]
  605. [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
  606. public event EventHandler DataBinding //DIT
  607. {
  608. add
  609. {
  610. Events.AddHandler(DataBindingEvent, value);
  611. }
  612. remove
  613. {
  614. Events.RemoveHandler(DataBindingEvent, value);
  615. }
  616. }
  617. [WebSysDescription ("Raised when the contol is disposed.")]
  618. public event EventHandler Disposed //DIT
  619. {
  620. add
  621. {
  622. Events.AddHandler(DisposedEvent, value);
  623. }
  624. remove
  625. {
  626. Events.RemoveHandler(DisposedEvent, value);
  627. }
  628. }
  629. [WebSysDescription ("Raised when the page containing the control is initialized.")]
  630. public event EventHandler Init //DIT
  631. {
  632. add
  633. {
  634. Events.AddHandler(InitEvent, value);
  635. }
  636. remove
  637. {
  638. Events.RemoveHandler(InitEvent, value);
  639. }
  640. }
  641. [WebSysDescription ("Raised after the page containing the control has been loaded.")]
  642. public event EventHandler Load //DIT
  643. {
  644. add
  645. {
  646. Events.AddHandler(LoadEvent, value);
  647. }
  648. remove
  649. {
  650. Events.RemoveHandler(LoadEvent, value);
  651. }
  652. }
  653. [WebSysDescription ("Raised before the page containing the control is rendered.")]
  654. public event EventHandler PreRender //DIT
  655. {
  656. add
  657. {
  658. Events.AddHandler(PreRenderEvent, value);
  659. }
  660. remove
  661. {
  662. Events.RemoveHandler(PreRenderEvent, value);
  663. }
  664. }
  665. [WebSysDescription ("Raised when the page containing the control is unloaded.")]
  666. public event EventHandler Unload //DIT
  667. {
  668. add
  669. {
  670. Events.AddHandler(UnloadEvent, value);
  671. }
  672. remove
  673. {
  674. Events.RemoveHandler(UnloadEvent, value);
  675. }
  676. }
  677. public virtual void DataBind() //DIT
  678. {
  679. #if NET_2_0
  680. bool foundDataItem = false;
  681. if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
  682. object o = DataBinder.GetDataItem (this, out foundDataItem);
  683. if (foundDataItem)
  684. Page.PushDataItemContext (o);
  685. }
  686. try {
  687. #endif
  688. OnDataBinding (EventArgs.Empty);
  689. DataBindChildren();
  690. #if NET_2_0
  691. } finally {
  692. if (foundDataItem)
  693. Page.PopDataItemContext ();
  694. }
  695. #endif
  696. }
  697. #if NET_2_0
  698. protected virtual
  699. #endif
  700. void DataBindChildren ()
  701. {
  702. if (!HasControls ())
  703. return;
  704. int len = Controls.Count;
  705. for (int i = 0; i < len; i++) {
  706. Control c = Controls [i];
  707. c.DataBind ();
  708. }
  709. }
  710. public virtual bool HasControls ()
  711. {
  712. return (_controls != null && _controls.Count > 0);
  713. }
  714. public void RenderControl(HtmlTextWriter writer)
  715. {
  716. if ((stateMask & VISIBLE) != 0)
  717. Render(writer);
  718. }
  719. public string ResolveUrl(string relativeUrl)
  720. {
  721. if (relativeUrl == null)
  722. throw new ArgumentNullException ("relativeUrl");
  723. if (relativeUrl == "")
  724. return "";
  725. if (relativeUrl [0] == '#')
  726. return relativeUrl;
  727. string ts = TemplateSourceDirectory;
  728. if (ts == "" || !UrlUtils.IsRelativeUrl (relativeUrl))
  729. return relativeUrl;
  730. HttpResponse resp = Context.Response;
  731. return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
  732. }
  733. [EditorBrowsable (EditorBrowsableState.Advanced)]
  734. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  735. {
  736. _renderMethodDelegate = renderMethod;
  737. }
  738. internal void LoadRecursive()
  739. {
  740. OnLoad (EventArgs.Empty);
  741. if (HasControls ()) {
  742. int len = Controls.Count;
  743. for (int i=0;i<len;i++)
  744. {
  745. Control c = Controls[i];
  746. c.LoadRecursive ();
  747. }
  748. }
  749. stateMask |= LOADED;
  750. }
  751. internal void UnloadRecursive(Boolean dispose)
  752. {
  753. if (HasControls ()) {
  754. int len = Controls.Count;
  755. for (int i=0;i<len;i++)
  756. {
  757. Control c = Controls[i];
  758. c.UnloadRecursive (dispose);
  759. }
  760. }
  761. OnUnload (EventArgs.Empty);
  762. if (dispose)
  763. Dispose();
  764. }
  765. internal void PreRenderRecursiveInternal()
  766. {
  767. if ((stateMask & VISIBLE) != 0) {
  768. EnsureChildControls ();
  769. OnPreRender (EventArgs.Empty);
  770. if (!HasControls ())
  771. return;
  772. int len = Controls.Count;
  773. for (int i=0;i<len;i++)
  774. {
  775. Control c = Controls[i];
  776. c.PreRenderRecursiveInternal ();
  777. }
  778. }
  779. stateMask |= PRERENDERED;
  780. }
  781. internal void InitRecursive(Control namingContainer)
  782. {
  783. if (HasControls ()) {
  784. if ((stateMask & IS_NAMING_CONTAINER) != 0)
  785. namingContainer = this;
  786. if (namingContainer != null &&
  787. namingContainer._userId == null &&
  788. namingContainer.AutoID)
  789. namingContainer._userId = namingContainer.GetDefaultName () + "b";
  790. int len = Controls.Count;
  791. for (int i=0;i<len;i++)
  792. {
  793. Control c = Controls[i];
  794. c._page = Page;
  795. c._namingContainer = namingContainer;
  796. if (namingContainer != null && c._userId == null && c.AutoID)
  797. c._userId = namingContainer.GetDefaultName () + "c";
  798. c.InitRecursive (namingContainer);
  799. }
  800. }
  801. stateMask |= INITING;
  802. OnInit (EventArgs.Empty);
  803. TrackViewState ();
  804. stateMask |= INITED;
  805. stateMask &= ~INITING;
  806. }
  807. internal object SaveViewStateRecursive ()
  808. {
  809. if (!EnableViewState)
  810. return null;
  811. ArrayList controlList = null;
  812. ArrayList controlStates = null;
  813. int idx = -1;
  814. if (HasControls ())
  815. {
  816. int len = Controls.Count;
  817. for (int i=0;i<len;i++)
  818. {
  819. Control ctrl = Controls[i];
  820. object ctrlState = ctrl.SaveViewStateRecursive ();
  821. idx++;
  822. if (ctrlState == null)
  823. continue;
  824. if (controlList == null)
  825. {
  826. controlList = new ArrayList ();
  827. controlStates = new ArrayList ();
  828. }
  829. controlList.Add (idx);
  830. controlStates.Add (ctrlState);
  831. }
  832. }
  833. object thisState = SaveViewState ();
  834. if (thisState == null && controlList == null && controlStates == null)
  835. return null;
  836. return new Triplet (thisState, controlList, controlStates);
  837. }
  838. internal void LoadViewStateRecursive (object savedState)
  839. {
  840. if (!EnableViewState || savedState == null)
  841. return;
  842. Triplet savedInfo = (Triplet) savedState;
  843. LoadViewState (savedInfo.First);
  844. ArrayList controlList = savedInfo.Second as ArrayList;
  845. if (controlList == null)
  846. return;
  847. ArrayList controlStates = savedInfo.Third as ArrayList;
  848. int nControls = controlList.Count;
  849. for (int i = 0; i < nControls; i++) {
  850. int k = (int) controlList [i];
  851. if (k < Controls.Count && controlStates != null) {
  852. Control c = Controls [k];
  853. c.LoadViewStateRecursive (controlStates [i]);
  854. } else {
  855. if (pendingVS == null)
  856. pendingVS = new Hashtable ();
  857. pendingVS [k] = controlStates [i];
  858. }
  859. }
  860. stateMask |= VIEWSTATE_LOADED;
  861. }
  862. void IParserAccessor.AddParsedSubObject(object obj)
  863. {
  864. AddParsedSubObject(obj);
  865. }
  866. DataBindingCollection IDataBindingsAccessor.DataBindings
  867. {
  868. get
  869. {
  870. if(dataBindings == null)
  871. dataBindings = new DataBindingCollection();
  872. return dataBindings;
  873. }
  874. }
  875. bool IDataBindingsAccessor.HasDataBindings
  876. {
  877. get
  878. {
  879. return (dataBindings!=null && dataBindings.Count>0);
  880. }
  881. }
  882. internal bool AutoID
  883. {
  884. get { return (stateMask & AUTOID) != 0; }
  885. set {
  886. if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
  887. return;
  888. SetMask (AUTOID, value);
  889. }
  890. }
  891. internal void PreventAutoID()
  892. {
  893. AutoID = false;
  894. }
  895. protected internal virtual void RemovedControl (Control control)
  896. {
  897. control.UnloadRecursive (false);
  898. control._parent = null;
  899. control._page = null;
  900. control._namingContainer = null;
  901. }
  902. #if NET_2_0
  903. protected string GetWebResourceUrl (string resourceName)
  904. {
  905. return Page.GetWebResourceUrl (GetType(), resourceName);
  906. }
  907. string IUrlResolutionService.ResolveClientUrl (string url)
  908. {
  909. throw new NotImplementedException ();
  910. }
  911. ControlBuilder IControlBuilderAccessor.ControlBuilder {
  912. get {throw new NotImplementedException (); }
  913. }
  914. IDictionary IControlDesignerAccessor.GetDesignModeState ()
  915. {
  916. throw new NotImplementedException ();
  917. }
  918. void IControlDesignerAccessor.SetDesignModeState (IDictionary designData)
  919. {
  920. throw new NotImplementedException ();
  921. }
  922. void IControlDesignerAccessor.SetOwnerControl (Control control)
  923. {
  924. throw new NotImplementedException ();
  925. }
  926. IDictionary IControlDesignerAccessor.UserData {
  927. get { throw new NotImplementedException (); }
  928. }
  929. ExpressionBindingCollection expressionBindings;
  930. ExpressionBindingCollection IExpressionsAccessor.Expressions {
  931. get {
  932. if (expressionBindings == null)
  933. expressionBindings = new ExpressionBindingCollection ();
  934. return expressionBindings;
  935. }
  936. }
  937. bool IExpressionsAccessor.HasExpressions {
  938. get {
  939. return (expressionBindings != null && expressionBindings.Count > 0);
  940. }
  941. }
  942. [MonoTODO]
  943. public virtual void Focus()
  944. {
  945. throw new NotImplementedException();
  946. }
  947. protected internal virtual void LoadControlState (object state)
  948. {
  949. }
  950. protected internal virtual object SaveControlState ()
  951. {
  952. return null;
  953. }
  954. #endif
  955. }
  956. }