Control.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 DataBindingCollection dataBindings = null;
  104. public Control()
  105. {
  106. if (this is INamingContainer) _isNamingContainer = true;
  107. }
  108. public Control BindingContainer
  109. {
  110. get {
  111. Control container = NamingContainer;
  112. if (_isNamingContainer)
  113. container = container.BindingContainer;
  114. return container;
  115. }
  116. }
  117. public virtual string ClientID //DIT
  118. {
  119. get
  120. {
  121. if (_cachedUserId != null && _cachedClientId != null)
  122. return _cachedClientId;
  123. _cachedUserId = UniqueID.Replace(':', '_');
  124. return _cachedUserId;
  125. }
  126. }
  127. public virtual ControlCollection Controls //DIT
  128. {
  129. get
  130. {
  131. if (_controls == null) _controls = CreateControlCollection();
  132. return _controls;
  133. }
  134. }
  135. public virtual bool EnableViewState //DIT
  136. {
  137. get
  138. {
  139. return _enableViewState;
  140. }
  141. set
  142. {
  143. _enableViewState = value;
  144. }
  145. }
  146. public virtual string ID
  147. {
  148. get //DIT
  149. {
  150. return _userId;
  151. }
  152. set
  153. {
  154. if (value == null || value == "") return;
  155. _userId = value;
  156. _cachedUserId = null;
  157. //TODO: Some Naming Container stuff here I think.
  158. }
  159. }
  160. public virtual Control NamingContainer //DIT
  161. {
  162. get
  163. {
  164. if (_namingContainer == null && _parent != null)
  165. {
  166. if (_parent._isNamingContainer == false)
  167. _namingContainer = _parent.NamingContainer;
  168. else
  169. _namingContainer = _parent;
  170. }
  171. return _namingContainer;
  172. }
  173. }
  174. public virtual Page Page //DIT
  175. {
  176. get
  177. {
  178. if (_page == null && _parent != null) _page = _parent.Page;
  179. return _page;
  180. }
  181. set
  182. {
  183. _page = value;
  184. }
  185. }
  186. public virtual Control Parent //DIT
  187. {
  188. get
  189. {
  190. return _parent;
  191. }
  192. }
  193. public ISite Site //DIT
  194. {
  195. get
  196. {
  197. return _site;
  198. }
  199. set
  200. {
  201. _site = value;
  202. }
  203. }
  204. public virtual string TemplateSourceDirectory
  205. {
  206. get
  207. {
  208. return Context.Request.ApplicationPath; //TODO: Dont think this is right.
  209. }
  210. }
  211. [MonoTODO]
  212. public virtual string UniqueID
  213. {
  214. get
  215. {
  216. //TODO: Some Naming container methods here. What are they? Why arnt they declared?
  217. //Note: Nuked the old stuff here. Was total crap. :)
  218. return ID;
  219. }
  220. }
  221. public virtual bool Visible
  222. {
  223. get
  224. {
  225. if (_visible == false)
  226. return false;
  227. if (_parent != null)
  228. return _parent.Visible;
  229. return true;
  230. }
  231. set
  232. {
  233. _visible = value;
  234. }
  235. }
  236. protected bool ChildControlsCreated //DIT
  237. {
  238. get
  239. {
  240. return _childControlsCreated;
  241. }
  242. set
  243. {
  244. if (value == false && _childControlsCreated == true)
  245. _controls.Clear();
  246. _childControlsCreated = value;
  247. }
  248. }
  249. protected virtual HttpContext Context //DIT
  250. {
  251. get
  252. {
  253. HttpContext context;
  254. if (_context != null)
  255. return _context;
  256. if (_parent == null)
  257. return HttpContext.Current;
  258. context = _parent.Context;
  259. if (context != null)
  260. return context;
  261. return HttpContext.Current;
  262. }
  263. }
  264. protected EventHandlerList Events //DIT
  265. {
  266. get
  267. {
  268. if (_events == null)
  269. {
  270. _events = new EventHandlerList();
  271. }
  272. return _events;
  273. }
  274. }
  275. protected bool HasChildViewState //DIT
  276. {
  277. get
  278. {
  279. if (_childViewStates == null) return false;
  280. return true;
  281. }
  282. }
  283. protected bool IsTrackingViewState //DIT
  284. {
  285. get
  286. {
  287. return _trackViewState;
  288. }
  289. }
  290. protected virtual StateBag ViewState
  291. {
  292. get
  293. {
  294. if(_viewState == null)
  295. _viewState = new StateBag (ViewStateIgnoresCase);
  296. if (IsTrackingViewState)
  297. _viewState.TrackViewState ();
  298. return _viewState;
  299. }
  300. }
  301. protected virtual bool ViewStateIgnoresCase
  302. {
  303. get {
  304. return false;
  305. }
  306. }
  307. private int defaultNumberID;
  308. protected internal virtual void AddedControl (Control control, int index)
  309. {
  310. /* Ensure the control don't have more than 1 parent */
  311. if (control._parent != null)
  312. control._parent.Controls.Remove (control);
  313. control._parent = this;
  314. control._page = Page;
  315. // Without this, DataBoundLiteralControl crashes in OnDataBound event.
  316. Control namingContainer = NamingContainer;
  317. if (namingContainer != null)
  318. control._namingContainer = namingContainer;
  319. if (control.AutoID == true && control.ID == null)
  320. control.ID = "_ctrl_" + defaultNumberID++;
  321. }
  322. protected virtual void AddParsedSubObject(object obj) //DIT
  323. {
  324. WebTrace.PushContext ("Control.AddParsedSubobject ()");
  325. Control c = obj as Control;
  326. WebTrace.WriteLine ("Start: {0} -> {1}", obj, (c != null) ? c.ID : String.Empty);
  327. if (c != null) Controls.Add(c);
  328. WebTrace.WriteLine ("End");
  329. WebTrace.PopContext ();
  330. }
  331. protected void BuildProfileTree(string parentId, bool calcViewState)
  332. {
  333. //TODO
  334. }
  335. protected void ClearChildViewState()
  336. {
  337. //TODO
  338. //Not quite sure about this. an example clears children then calls this, so I think
  339. //view state is local to the current object, not children.
  340. }
  341. protected virtual void CreateChildControls() {} //DIT
  342. protected virtual ControlCollection CreateControlCollection() //DIT
  343. {
  344. return new ControlCollection(this);
  345. }
  346. protected virtual void EnsureChildControls () //DIT
  347. {
  348. if (ChildControlsCreated == false && !creatingControls) {
  349. creatingControls = true;
  350. CreateChildControls();
  351. ChildControlsCreated = true;
  352. creatingControls = false;
  353. }
  354. }
  355. protected virtual Control FindControl(string id, int pathOffset)
  356. {
  357. //TODO: I think there is Naming Container stuff here. Redo.
  358. int i;
  359. Control ctrl;
  360. for (i = pathOffset; i < _controls.Count; i++){
  361. ctrl = _controls [i];
  362. if (ctrl.ID == id)
  363. return ctrl;
  364. if (ctrl.Controls.Count > 0){
  365. Control other = ctrl.FindControl (id);
  366. if (other != null)
  367. return other;
  368. }
  369. }
  370. return null;
  371. }
  372. protected virtual void LoadViewState(object savedState)
  373. {
  374. if (savedState != null)
  375. ViewState.LoadViewState (savedState);
  376. }
  377. [MonoTODO]
  378. protected string MapPathSecure(string virtualPath)
  379. {
  380. //TODO: Need to read up on security+web.
  381. //Return the same path. So AdRotator can read its config file.
  382. return virtualPath;
  383. }
  384. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  385. {
  386. return false;
  387. }
  388. protected virtual void OnDataBinding(EventArgs e) //DIT
  389. {
  390. if (_events != null)
  391. {
  392. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  393. if (eh != null) eh(this, e);
  394. }
  395. }
  396. protected virtual void OnInit(EventArgs e) //DIT
  397. {
  398. if (_events != null)
  399. {
  400. EventHandler eh = (EventHandler)(_events[InitEvent]);
  401. if (eh != null) eh(this, e);
  402. }
  403. }
  404. protected virtual void OnLoad(EventArgs e) //DIT
  405. {
  406. if (_events != null)
  407. {
  408. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  409. if (eh != null) eh(this, e);
  410. }
  411. }
  412. protected virtual void OnPreRender(EventArgs e) //DIT
  413. {
  414. if (_events != null)
  415. {
  416. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  417. if (eh != null) eh(this, e);
  418. }
  419. }
  420. protected virtual void OnUnload(EventArgs e) //DIT
  421. {
  422. if (_events != null)
  423. {
  424. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  425. if (eh != null) eh(this, e);
  426. }
  427. }
  428. protected void RaiseBubbleEvent(object source, EventArgs args)
  429. {
  430. Control c = Parent;
  431. while (c != null) {
  432. if (c.OnBubbleEvent (source, args))
  433. break;
  434. c = c.Parent;
  435. }
  436. }
  437. protected virtual void Render(HtmlTextWriter writer) //DIT
  438. {
  439. RenderChildren(writer);
  440. }
  441. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  442. {
  443. if (_renderMethodDelegate != null)
  444. _renderMethodDelegate(writer, this);
  445. else if (_controls != null)
  446. foreach (Control c in _controls)
  447. c.RenderControl(writer);
  448. }
  449. protected virtual object SaveViewState ()
  450. {
  451. if (_viewState == null)
  452. return null;
  453. return _viewState.SaveViewState ();
  454. }
  455. protected virtual void TrackViewState()
  456. {
  457. if (_viewState != null)
  458. _viewState.TrackViewState ();
  459. _trackViewState = true;
  460. }
  461. public virtual void Dispose()
  462. {
  463. if (_events != null)
  464. {
  465. EventHandler eh = (EventHandler) _events [DisposedEvent];
  466. if (eh != null)
  467. eh(this, EventArgs.Empty);
  468. }
  469. }
  470. public bool HasChildren
  471. {
  472. get { return (_controls != null && _controls.Count > 0); }
  473. }
  474. public event EventHandler DataBinding //DIT
  475. {
  476. add
  477. {
  478. Events.AddHandler(DataBindingEvent, value);
  479. }
  480. remove
  481. {
  482. Events.RemoveHandler(DataBindingEvent, value);
  483. }
  484. }
  485. public event EventHandler Disposed //DIT
  486. {
  487. add
  488. {
  489. Events.AddHandler(DisposedEvent, value);
  490. }
  491. remove
  492. {
  493. Events.RemoveHandler(DisposedEvent, value);
  494. }
  495. }
  496. public event EventHandler Init //DIT
  497. {
  498. add
  499. {
  500. Events.AddHandler(InitEvent, value);
  501. }
  502. remove
  503. {
  504. Events.RemoveHandler(InitEvent, value);
  505. }
  506. }
  507. public event EventHandler Load //DIT
  508. {
  509. add
  510. {
  511. Events.AddHandler(LoadEvent, value);
  512. }
  513. remove
  514. {
  515. Events.RemoveHandler(LoadEvent, value);
  516. }
  517. }
  518. public event EventHandler PreRender //DIT
  519. {
  520. add
  521. {
  522. Events.AddHandler(PreRenderEvent, value);
  523. }
  524. remove
  525. {
  526. Events.RemoveHandler(PreRenderEvent, value);
  527. }
  528. }
  529. public event EventHandler Unload //DIT
  530. {
  531. add
  532. {
  533. Events.AddHandler(UnloadEvent, value);
  534. }
  535. remove
  536. {
  537. Events.RemoveHandler(UnloadEvent, value);
  538. }
  539. }
  540. public virtual void DataBind() //DIT
  541. {
  542. OnDataBinding(EventArgs.Empty);
  543. if (_controls != null)
  544. foreach (Control c in _controls)
  545. c.DataBind();
  546. }
  547. public virtual Control FindControl(string id) //DIT
  548. {
  549. return FindControl(id, 0);
  550. }
  551. public virtual bool HasControls() //DIT
  552. {
  553. if (_controls != null && _controls.Count >0) return true;
  554. return false;
  555. }
  556. public void RenderControl(HtmlTextWriter writer)
  557. {
  558. if (_visible)
  559. {
  560. //TODO: Something about tracing here.
  561. Render(writer);
  562. }
  563. }
  564. [MonoTODO]
  565. public string ResolveUrl(string relativeUrl)
  566. {
  567. return relativeUrl;
  568. }
  569. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  570. {
  571. WebTrace.PushContext ("Control.AddParsedSubobject ()");
  572. WebTrace.WriteLine ("Start");
  573. _renderMethodDelegate = renderMethod;
  574. WebTrace.WriteLine ("End");
  575. WebTrace.PopContext ();
  576. }
  577. protected void LoadRecursive()
  578. {
  579. OnLoad(EventArgs.Empty);
  580. if (_controls != null) foreach (Control c in _controls) c.LoadRecursive();
  581. }
  582. protected void UnloadRecursive(Boolean dispose)
  583. {
  584. OnUnload(EventArgs.Empty);
  585. if (_controls != null) foreach (Control c in _controls) c.UnloadRecursive(dispose);
  586. if (dispose) Dispose();
  587. }
  588. protected void PreRenderRecursiveInternal()
  589. {
  590. OnPreRender(EventArgs.Empty);
  591. if (_controls != null) foreach (Control c in _controls) c.PreRenderRecursiveInternal();
  592. }
  593. protected void InitRecursive(Control namingContainer)
  594. {
  595. if (_controls != null) foreach (Control c in _controls) c.InitRecursive(namingContainer);
  596. OnInit(EventArgs.Empty);
  597. TrackViewState ();
  598. }
  599. internal object SaveViewStateRecursive()
  600. {
  601. if (!EnableViewState)
  602. return null;
  603. ArrayList controlList = null;
  604. ArrayList controlStates = null;
  605. foreach (Control ctrl in Controls){
  606. if (controlList == null) {
  607. controlList = new ArrayList ();
  608. controlStates = new ArrayList ();
  609. }
  610. controlList.Add (ctrl);
  611. controlStates.Add (ctrl.SaveViewStateRecursive ());
  612. }
  613. return new Triplet (SaveViewState (), controlList, controlStates);
  614. }
  615. internal void LoadViewStateRecursive (object savedState)
  616. {
  617. if (!EnableViewState || !Visible || savedState == null)
  618. return;
  619. Triplet savedInfo = (Triplet) savedState;
  620. LoadViewState (savedInfo.First);
  621. ArrayList controlList = savedInfo.Second as ArrayList;
  622. if (controlList == null)
  623. return;
  624. ArrayList controlStates = savedInfo.Third as ArrayList;
  625. int nControls = controlList.Count;
  626. for (int i = 0; i < nControls; i++) {
  627. if (controlStates != null)
  628. Controls [i].LoadViewStateRecursive (controlStates [i]);
  629. }
  630. }
  631. void IParserAccessor.AddParsedSubObject(object obj)
  632. {
  633. AddParsedSubObject(obj);
  634. }
  635. DataBindingCollection IDataBindingsAccessor.DataBindings
  636. {
  637. get
  638. {
  639. if(dataBindings == null)
  640. dataBindings = new DataBindingCollection();
  641. return dataBindings;
  642. }
  643. }
  644. bool IDataBindingsAccessor.HasDataBindings
  645. {
  646. get
  647. {
  648. return (dataBindings!=null && dataBindings.Count>0);
  649. }
  650. }
  651. internal bool AutoID
  652. {
  653. get { return autoID; }
  654. set { autoID = value; }
  655. }
  656. internal void PreventAutoID()
  657. {
  658. AutoID = false;
  659. }
  660. protected internal virtual void RemovedControl (Control control)
  661. {
  662. control.UnloadRecursive (false);
  663. control._parent = null;
  664. control._page = null;
  665. control._namingContainer = null;
  666. }
  667. //TODO: I think there are some needed Interface implementations to do here.
  668. //TODO: Find api for INamingContainer.
  669. }
  670. }