Control.cs 31 KB

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