Control.cs 27 KB

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