Control.cs 33 KB

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