Control.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. //
  2. // System.Web.UI.Control.cs
  3. //
  4. // Authors:
  5. // Bob Smith <[email protected]>
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) Bob Smith
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. /*
  12. * Maintainer: [email protected], [email protected]
  13. * (C) Bob Smith, Gaurav Vaish
  14. */
  15. //notes: view state only tracks changes after OnInit method is executed for the page request. You can read from it at any time, but cant write to it during rendering.
  16. //even more notes: view state info in trackviewstate method description. read later.
  17. //Ok, enough notes: what the heck is different between enable view state, and track view state.
  18. //Well, maybe not. How does the ViewState know when to track changes? Does it look at the property
  19. //on the owning control, or does it have a method/property of its own that gets called?
  20. // I think this last question is solved in the Interface for it. Look into this.
  21. //cycle:
  22. //init is called when control is first created.
  23. //load view state ic called right after init to populate the view state.
  24. //loadpostdata is called if ipostbackdatahandler is implemented.
  25. //load is called when control is loaded into a page
  26. //raisepostdatachangedevent if ipostbackdatahandler is implemented.
  27. //raisepostbackevent if ipostbackeventhandler is implemented.
  28. //prerender is called when the server is about to render its page object
  29. //SaveViewState is called.
  30. //Unload then dispose it apears. :)
  31. //Naming Container MUST have some methods. What are they? No clue. Help?
  32. //read this later. http://gotdotnet.com/quickstart/aspplus/
  33. //This to: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconattributesdesign-timesupport.asp
  34. //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpcontracefunctionality.asp
  35. // Isnt life grand? :)
  36. // See the undocumented methods? Gota love um. ;)
  37. // ASP.test4_aspx.Page_Load(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  38. // System.Web.UI.Control.OnLoad(EventArgs e) +67
  39. // System.Web.UI.Control.LoadRecursive() +73
  40. // System.Web.UI.Page.ProcessRequestMain() +394
  41. // ASP.test4_aspx.Page_Unload(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  42. // System.EventHandler.Invoke(Object sender, EventArgs e) +0
  43. // System.Web.UI.Control.OnUnload(EventArgs e) +67
  44. // System.Web.UI.Control.UnloadRecursive(Boolean dispose) +78
  45. // System.Web.UI.Page.ProcessRequest() +194
  46. // System.Web.UI.Page.ProcessRequest(HttpContext context) +18
  47. // System.Web.CallHandlerExecutionStep.Execute() +179
  48. // System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +87
  49. // ASP.test4_aspx.Page_Unload(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  50. // System.Web.UI.Control.OnUnload(EventArgs e) +67
  51. // System.Web.UI.Control.UnloadRecursive(Boolean dispose) +78
  52. // System.Web.UI.Page.ProcessRequest()
  53. // ASP.test4_aspx.Page_Kill(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  54. // System.Web.UI.Control.OnPreRender(EventArgs e) +67
  55. // System.Web.UI.Control.PreRenderRecursiveInternal() +61
  56. // System.Web.UI.Page.ProcessRequestMain() +753
  57. // ASP.test4_aspx.OnInit(EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  58. // System.Web.UI.Control.InitRecursive(Control namingContainer) +202
  59. // System.Web.UI.Page.ProcessRequestMain() +120
  60. // ASP.test4_aspx.SaveViewState() in \\genfs2\www24\bobsmith11\test4.aspx:12
  61. // System.Web.UI.Control.SaveViewStateRecursive() +51
  62. // System.Web.UI.Page.SavePageViewState() +174
  63. // System.Web.UI.Page.ProcessRequestMain() +861
  64. // ASP.test_aspx.LoadViewState(Object t) +28
  65. // System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +125
  66. // System.Web.UI.Page.LoadPageViewState() +182
  67. // System.Web.UI.Page.ProcessRequestMain() +256
  68. using System;
  69. using System.Collections;
  70. using System.ComponentModel;
  71. using System.Web;
  72. using System.Web.Util;
  73. namespace System.Web.UI
  74. {
  75. public class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
  76. {
  77. private static readonly object DataBindingEvent = new object();
  78. private static readonly object DisposedEvent = new object();
  79. private static readonly object InitEvent = new object();
  80. private static readonly object LoadEvent = new object();
  81. private static readonly object PreRenderEvent = new object();
  82. private static readonly object UnloadEvent = new object();
  83. private string _userId = null;
  84. private string _cachedUserId = null;
  85. private string _cachedClientId = null;
  86. private ControlCollection _controls = null;
  87. private bool _enableViewState = true;
  88. private IDictionary _childViewStates = null; //TODO: Not sure datatype. Placeholder guess.
  89. private bool _isNamingContainer = false;
  90. private Control _namingContainer = null;
  91. private Page _page = null;
  92. private Control _parent = null;
  93. private ISite _site = null;
  94. private bool _visible = true;
  95. private HttpContext _context = null;
  96. private bool _childControlsCreated = false;
  97. private StateBag _viewState = null;
  98. private bool _trackViewState = false;
  99. private EventHandlerList _events = new EventHandlerList();
  100. private RenderMethod _renderMethodDelegate = null;
  101. private bool autoID = true;
  102. private bool creatingControls = false;
  103. private bool bindingContainer = true;
  104. private bool autoEventWireup = true;
  105. private DataBindingCollection dataBindings = null;
  106. public Control()
  107. {
  108. if (this is INamingContainer) _isNamingContainer = true;
  109. }
  110. public Control BindingContainer
  111. {
  112. get {
  113. Control container = NamingContainer;
  114. if (!container.bindingContainer)
  115. container = container.BindingContainer;
  116. return container;
  117. }
  118. }
  119. public virtual string ClientID //DIT
  120. {
  121. get
  122. {
  123. if (_cachedUserId != null && _cachedClientId != null)
  124. return _cachedClientId;
  125. _cachedUserId = UniqueID.Replace(':', '_');
  126. return _cachedUserId;
  127. }
  128. }
  129. public virtual ControlCollection Controls //DIT
  130. {
  131. get
  132. {
  133. if (_controls == null) _controls = CreateControlCollection();
  134. return _controls;
  135. }
  136. }
  137. public virtual bool EnableViewState //DIT
  138. {
  139. get
  140. {
  141. return _enableViewState;
  142. }
  143. set
  144. {
  145. _enableViewState = value;
  146. }
  147. }
  148. public virtual string ID
  149. {
  150. get //DIT
  151. {
  152. return _userId;
  153. }
  154. set
  155. {
  156. if (value == null || value == "") return;
  157. _userId = value;
  158. _cachedUserId = null;
  159. //TODO: Some Naming Container stuff here I think.
  160. }
  161. }
  162. public virtual Control NamingContainer //DIT
  163. {
  164. get
  165. {
  166. if (_namingContainer == null && _parent != null)
  167. {
  168. if (_parent._isNamingContainer == false)
  169. _namingContainer = _parent.NamingContainer;
  170. else
  171. _namingContainer = _parent;
  172. }
  173. return _namingContainer;
  174. }
  175. }
  176. public virtual Page Page //DIT
  177. {
  178. get
  179. {
  180. if (_page == null && _parent != null) _page = _parent.Page;
  181. return _page;
  182. }
  183. set
  184. {
  185. _page = value;
  186. }
  187. }
  188. public virtual Control Parent //DIT
  189. {
  190. get
  191. {
  192. return _parent;
  193. }
  194. }
  195. public ISite Site //DIT
  196. {
  197. get
  198. {
  199. return _site;
  200. }
  201. set
  202. {
  203. _site = value;
  204. }
  205. }
  206. public virtual string TemplateSourceDirectory {
  207. get { return (_parent == null) ? String.Empty : _parent.TemplateSourceDirectory; }
  208. }
  209. [MonoTODO]
  210. public virtual string UniqueID
  211. {
  212. get
  213. {
  214. //TODO: Some Naming container methods here. What are they? Why arnt they declared?
  215. //Note: Nuked the old stuff here. Was total crap. :)
  216. return ID;
  217. }
  218. }
  219. public virtual bool Visible
  220. {
  221. get
  222. {
  223. if (_visible == false)
  224. return false;
  225. if (_parent != null)
  226. return _parent.Visible;
  227. return true;
  228. }
  229. set
  230. {
  231. _visible = value;
  232. }
  233. }
  234. protected bool ChildControlsCreated //DIT
  235. {
  236. get
  237. {
  238. return _childControlsCreated;
  239. }
  240. set
  241. {
  242. if (value == false && _childControlsCreated == true)
  243. _controls.Clear();
  244. _childControlsCreated = value;
  245. }
  246. }
  247. protected virtual HttpContext Context //DIT
  248. {
  249. get
  250. {
  251. HttpContext context;
  252. if (_context != null)
  253. return _context;
  254. if (_parent == null)
  255. return HttpContext.Current;
  256. context = _parent.Context;
  257. if (context != null)
  258. return context;
  259. return HttpContext.Current;
  260. }
  261. }
  262. protected EventHandlerList Events //DIT
  263. {
  264. get
  265. {
  266. if (_events == null)
  267. {
  268. _events = new EventHandlerList();
  269. }
  270. return _events;
  271. }
  272. }
  273. protected bool HasChildViewState //DIT
  274. {
  275. get
  276. {
  277. if (_childViewStates == null) return false;
  278. return true;
  279. }
  280. }
  281. protected bool IsTrackingViewState //DIT
  282. {
  283. get
  284. {
  285. return _trackViewState;
  286. }
  287. }
  288. protected virtual StateBag ViewState
  289. {
  290. get
  291. {
  292. if(_viewState == null)
  293. _viewState = new StateBag (ViewStateIgnoresCase);
  294. if (IsTrackingViewState)
  295. _viewState.TrackViewState ();
  296. return _viewState;
  297. }
  298. }
  299. protected virtual bool ViewStateIgnoresCase
  300. {
  301. get {
  302. return false;
  303. }
  304. }
  305. internal bool AutoEventWireup {
  306. get { return autoEventWireup; }
  307. set { autoEventWireup = value; }
  308. }
  309. internal void SetBindingContainer (bool isBC)
  310. {
  311. bindingContainer = isBC;
  312. }
  313. private int defaultNumberID;
  314. protected internal virtual void AddedControl (Control control, int index)
  315. {
  316. /* Ensure the control don't have more than 1 parent */
  317. if (control._parent != null)
  318. control._parent.Controls.Remove (control);
  319. control._parent = this;
  320. control._page = Page;
  321. // Without this, DataBoundLiteralControl crashes in OnDataBound event.
  322. Control namingContainer = NamingContainer;
  323. if (namingContainer != null)
  324. control._namingContainer = namingContainer;
  325. if (control.AutoID == true && control.ID == null)
  326. control.ID = ID + "_ctrl_" + defaultNumberID++;
  327. }
  328. protected virtual void AddParsedSubObject(object obj) //DIT
  329. {
  330. WebTrace.PushContext ("Control.AddParsedSubobject ()");
  331. Control c = obj as Control;
  332. WebTrace.WriteLine ("Start: {0} -> {1}", obj, (c != null) ? c.ID : String.Empty);
  333. if (c != null) Controls.Add(c);
  334. WebTrace.WriteLine ("End");
  335. WebTrace.PopContext ();
  336. }
  337. protected void BuildProfileTree(string parentId, bool calcViewState)
  338. {
  339. //TODO
  340. }
  341. protected void ClearChildViewState()
  342. {
  343. //TODO
  344. //Not quite sure about this. an example clears children then calls this, so I think
  345. //view state is local to the current object, not children.
  346. }
  347. protected virtual void CreateChildControls() {} //DIT
  348. protected virtual ControlCollection CreateControlCollection() //DIT
  349. {
  350. return new ControlCollection(this);
  351. }
  352. protected virtual void EnsureChildControls () //DIT
  353. {
  354. if (ChildControlsCreated == false && !creatingControls) {
  355. creatingControls = true;
  356. CreateChildControls();
  357. ChildControlsCreated = true;
  358. creatingControls = false;
  359. }
  360. }
  361. protected virtual Control FindControl(string id, int pathOffset)
  362. {
  363. //TODO: I think there is Naming Container stuff here. Redo.
  364. int i;
  365. Control ctrl;
  366. for (i = pathOffset; i < _controls.Count; i++){
  367. ctrl = _controls [i];
  368. if (ctrl.ID == id)
  369. return ctrl;
  370. if (ctrl.Controls.Count > 0){
  371. Control other = ctrl.FindControl (id);
  372. if (other != null)
  373. return other;
  374. }
  375. }
  376. return null;
  377. }
  378. protected virtual void LoadViewState(object savedState)
  379. {
  380. if (savedState != null)
  381. ViewState.LoadViewState (savedState);
  382. }
  383. [MonoTODO("Secure?")]
  384. protected string MapPathSecure(string virtualPath)
  385. {
  386. return Context.Request.MapPath (virtualPath);
  387. }
  388. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  389. {
  390. return false;
  391. }
  392. protected virtual void OnDataBinding(EventArgs e) //DIT
  393. {
  394. if (_events != null)
  395. {
  396. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  397. if (eh != null) eh(this, e);
  398. }
  399. }
  400. protected virtual void OnInit(EventArgs e) //DIT
  401. {
  402. if (_events != null)
  403. {
  404. EventHandler eh = (EventHandler)(_events[InitEvent]);
  405. if (eh != null) eh(this, e);
  406. }
  407. }
  408. protected virtual void OnLoad(EventArgs e) //DIT
  409. {
  410. if (_events != null)
  411. {
  412. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  413. if (eh != null) eh(this, e);
  414. }
  415. }
  416. protected virtual void OnPreRender(EventArgs e) //DIT
  417. {
  418. if (_events != null)
  419. {
  420. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  421. if (eh != null) eh(this, e);
  422. }
  423. }
  424. protected virtual void OnUnload(EventArgs e) //DIT
  425. {
  426. if (_events != null)
  427. {
  428. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  429. if (eh != null) eh(this, e);
  430. }
  431. }
  432. protected void RaiseBubbleEvent(object source, EventArgs args)
  433. {
  434. Control c = Parent;
  435. while (c != null) {
  436. if (c.OnBubbleEvent (source, args))
  437. break;
  438. c = c.Parent;
  439. }
  440. }
  441. protected virtual void Render(HtmlTextWriter writer) //DIT
  442. {
  443. RenderChildren(writer);
  444. }
  445. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  446. {
  447. if (_renderMethodDelegate != null)
  448. _renderMethodDelegate(writer, this);
  449. else if (_controls != null)
  450. foreach (Control c in _controls)
  451. c.RenderControl(writer);
  452. }
  453. protected virtual object SaveViewState ()
  454. {
  455. if (_viewState == null)
  456. return null;
  457. return _viewState.SaveViewState ();
  458. }
  459. protected virtual void TrackViewState()
  460. {
  461. if (_viewState != null)
  462. _viewState.TrackViewState ();
  463. _trackViewState = true;
  464. }
  465. public virtual void Dispose()
  466. {
  467. if (_events != null)
  468. {
  469. EventHandler eh = (EventHandler) _events [DisposedEvent];
  470. if (eh != null)
  471. eh(this, EventArgs.Empty);
  472. }
  473. }
  474. public bool HasChildren
  475. {
  476. get { return (_controls != null && _controls.Count > 0); }
  477. }
  478. public event EventHandler DataBinding //DIT
  479. {
  480. add
  481. {
  482. Events.AddHandler(DataBindingEvent, value);
  483. }
  484. remove
  485. {
  486. Events.RemoveHandler(DataBindingEvent, value);
  487. }
  488. }
  489. public event EventHandler Disposed //DIT
  490. {
  491. add
  492. {
  493. Events.AddHandler(DisposedEvent, value);
  494. }
  495. remove
  496. {
  497. Events.RemoveHandler(DisposedEvent, value);
  498. }
  499. }
  500. public event EventHandler Init //DIT
  501. {
  502. add
  503. {
  504. Events.AddHandler(InitEvent, value);
  505. }
  506. remove
  507. {
  508. Events.RemoveHandler(InitEvent, value);
  509. }
  510. }
  511. public event EventHandler Load //DIT
  512. {
  513. add
  514. {
  515. Events.AddHandler(LoadEvent, value);
  516. }
  517. remove
  518. {
  519. Events.RemoveHandler(LoadEvent, value);
  520. }
  521. }
  522. public event EventHandler PreRender //DIT
  523. {
  524. add
  525. {
  526. Events.AddHandler(PreRenderEvent, value);
  527. }
  528. remove
  529. {
  530. Events.RemoveHandler(PreRenderEvent, value);
  531. }
  532. }
  533. public event EventHandler Unload //DIT
  534. {
  535. add
  536. {
  537. Events.AddHandler(UnloadEvent, value);
  538. }
  539. remove
  540. {
  541. Events.RemoveHandler(UnloadEvent, value);
  542. }
  543. }
  544. public virtual void DataBind() //DIT
  545. {
  546. OnDataBinding(EventArgs.Empty);
  547. if (_controls != null)
  548. foreach (Control c in _controls)
  549. c.DataBind();
  550. }
  551. public virtual Control FindControl(string id) //DIT
  552. {
  553. return FindControl(id, 0);
  554. }
  555. public virtual bool HasControls() //DIT
  556. {
  557. if (_controls != null && _controls.Count >0) return true;
  558. return false;
  559. }
  560. public void RenderControl(HtmlTextWriter writer)
  561. {
  562. if (_visible)
  563. {
  564. //TODO: Something about tracing here.
  565. Render(writer);
  566. }
  567. }
  568. [MonoTODO]
  569. public string ResolveUrl(string relativeUrl)
  570. {
  571. return relativeUrl;
  572. }
  573. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  574. {
  575. WebTrace.PushContext ("Control.AddParsedSubobject ()");
  576. WebTrace.WriteLine ("Start");
  577. _renderMethodDelegate = renderMethod;
  578. WebTrace.WriteLine ("End");
  579. WebTrace.PopContext ();
  580. }
  581. protected void LoadRecursive()
  582. {
  583. OnLoad(EventArgs.Empty);
  584. if (_controls != null) foreach (Control c in _controls) c.LoadRecursive();
  585. }
  586. protected void UnloadRecursive(Boolean dispose)
  587. {
  588. OnUnload(EventArgs.Empty);
  589. if (_controls != null) foreach (Control c in _controls) c.UnloadRecursive(dispose);
  590. if (dispose) Dispose();
  591. }
  592. protected void PreRenderRecursiveInternal()
  593. {
  594. OnPreRender(EventArgs.Empty);
  595. if (_controls != null) foreach (Control c in _controls) c.PreRenderRecursiveInternal();
  596. }
  597. protected void InitRecursive(Control namingContainer)
  598. {
  599. if (_controls != null) foreach (Control c in _controls) c.InitRecursive(namingContainer);
  600. OnInit(EventArgs.Empty);
  601. TrackViewState ();
  602. }
  603. internal object SaveViewStateRecursive()
  604. {
  605. if (!EnableViewState)
  606. return null;
  607. ArrayList controlList = null;
  608. ArrayList controlStates = null;
  609. foreach (Control ctrl in Controls){
  610. if (controlList == null) {
  611. controlList = new ArrayList ();
  612. controlStates = new ArrayList ();
  613. }
  614. controlList.Add (ctrl.ID);
  615. controlStates.Add (ctrl.SaveViewStateRecursive ());
  616. }
  617. return new Triplet (SaveViewState (), controlList, controlStates);
  618. }
  619. internal void LoadViewStateRecursive (object savedState)
  620. {
  621. if (!EnableViewState || !Visible || savedState == null)
  622. return;
  623. Triplet savedInfo = (Triplet) savedState;
  624. LoadViewState (savedInfo.First);
  625. ArrayList controlList = savedInfo.Second as ArrayList;
  626. if (controlList == null)
  627. return;
  628. ArrayList controlStates = savedInfo.Third as ArrayList;
  629. int nControls = controlList.Count;
  630. for (int i = 0; i < nControls; i++) {
  631. Control c = FindControl ((string) controlList [i]);
  632. if (c != null && controlStates != null)
  633. c.LoadViewStateRecursive (controlStates [i]);
  634. }
  635. }
  636. void IParserAccessor.AddParsedSubObject(object obj)
  637. {
  638. AddParsedSubObject(obj);
  639. }
  640. DataBindingCollection IDataBindingsAccessor.DataBindings
  641. {
  642. get
  643. {
  644. if(dataBindings == null)
  645. dataBindings = new DataBindingCollection();
  646. return dataBindings;
  647. }
  648. }
  649. bool IDataBindingsAccessor.HasDataBindings
  650. {
  651. get
  652. {
  653. return (dataBindings!=null && dataBindings.Count>0);
  654. }
  655. }
  656. internal bool AutoID
  657. {
  658. get { return autoID; }
  659. set { autoID = value; }
  660. }
  661. internal void PreventAutoID()
  662. {
  663. AutoID = false;
  664. }
  665. protected internal virtual void RemovedControl (Control control)
  666. {
  667. control.UnloadRecursive (false);
  668. control._parent = null;
  669. control._page = null;
  670. control._namingContainer = null;
  671. }
  672. //TODO: I think there are some needed Interface implementations to do here.
  673. //TODO: Find api for INamingContainer.
  674. }
  675. }