Control.cs 31 KB

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