Control.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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. { //TODO: Are children visible when parents are not?
  220. get
  221. {
  222. return _visible;
  223. }
  224. set
  225. {
  226. _visible = value;
  227. }
  228. }
  229. protected bool ChildControlsCreated //DIT
  230. {
  231. get
  232. {
  233. return _childControlsCreated;
  234. }
  235. set
  236. {
  237. if (value == false && _childControlsCreated == true)
  238. _controls.Clear();
  239. _childControlsCreated = value;
  240. }
  241. }
  242. protected virtual HttpContext Context //DIT
  243. {
  244. get
  245. {
  246. HttpContext context;
  247. if (_context != null)
  248. return _context;
  249. if (_parent == null)
  250. return HttpContext.Current;
  251. context = _parent.Context;
  252. if (context != null)
  253. return context;
  254. return HttpContext.Current;
  255. }
  256. }
  257. protected EventHandlerList Events //DIT
  258. {
  259. get
  260. {
  261. if (_events == null)
  262. {
  263. _events = new EventHandlerList();
  264. }
  265. return _events;
  266. }
  267. }
  268. protected bool HasChildViewState //DIT
  269. {
  270. get
  271. {
  272. if (_childViewStates == null) return false;
  273. return true;
  274. }
  275. }
  276. protected bool IsTrackingViewState //DIT
  277. {
  278. get
  279. {
  280. return _trackViewState;
  281. }
  282. }
  283. protected virtual StateBag ViewState
  284. {
  285. get
  286. {
  287. if(_viewState == null)
  288. {
  289. _viewState = new StateBag(ViewStateIgnoresCase);
  290. if(IsTrackingViewState)
  291. _viewState.TrackViewState();
  292. }
  293. return _viewState;
  294. }
  295. }
  296. protected virtual bool ViewStateIgnoresCase //DIT
  297. {
  298. get
  299. {
  300. return true;
  301. }
  302. }
  303. private int defaultNumberID;
  304. protected internal virtual void AddedControl (Control control, int index)
  305. {
  306. /* Ensure the control don't have more than 1 parent */
  307. if (control._parent != null)
  308. control._parent.Controls.Remove (control);
  309. control._parent = this;
  310. control._page = _page;
  311. // Without this, DataBoundLiteralControl crashes in OnDataBound event.
  312. Control namingContainer = NamingContainer;
  313. if (namingContainer != null)
  314. control._namingContainer = namingContainer;
  315. if (control.AutoID == true && control.ID == null)
  316. control.ID = "_ctrl_" + defaultNumberID++;
  317. }
  318. protected virtual void AddParsedSubObject(object obj) //DIT
  319. {
  320. Control c = (Control)obj;
  321. if (c != null) Controls.Add(c);
  322. }
  323. protected void BuildProfileTree(string parentId, bool calcViewState)
  324. {
  325. //TODO
  326. }
  327. protected void ClearChildViewState()
  328. {
  329. //TODO
  330. //Not quite sure about this. an example clears children then calls this, so I think
  331. //view state is local to the current object, not children.
  332. }
  333. protected virtual void CreateChildControls() {} //DIT
  334. protected virtual ControlCollection CreateControlCollection() //DIT
  335. {
  336. return new ControlCollection(this);
  337. }
  338. protected virtual void EnsureChildControls () //DIT
  339. {
  340. if (ChildControlsCreated == false && !creatingControls) {
  341. creatingControls = true;
  342. CreateChildControls();
  343. ChildControlsCreated = true;
  344. creatingControls = false;
  345. }
  346. }
  347. protected virtual Control FindControl(string id, int pathOffset)
  348. {
  349. //TODO: I think there is Naming Container stuff here. Redo.
  350. int i;
  351. for (i = pathOffset; i < _controls.Count; i++)
  352. if (_controls[i].ID == id) return _controls[i];
  353. return null;
  354. }
  355. protected virtual void LoadViewState(object savedState)
  356. {
  357. //TODO: What should I do by default?
  358. }
  359. [MonoTODO]
  360. protected string MapPathSecure(string virtualPath)
  361. {
  362. //TODO: Need to read up on security+web.
  363. //Return the same path. So AdRotator can read its config file.
  364. return virtualPath;
  365. }
  366. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  367. {
  368. return false;
  369. }
  370. protected virtual void OnDataBinding(EventArgs e) //DIT
  371. {
  372. if (_events != null)
  373. {
  374. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  375. if (eh != null) eh(this, e);
  376. }
  377. }
  378. protected virtual void OnInit(EventArgs e) //DIT
  379. {
  380. if (_events != null)
  381. {
  382. EventHandler eh = (EventHandler)(_events[InitEvent]);
  383. if (eh != null) eh(this, e);
  384. }
  385. }
  386. protected virtual void OnLoad(EventArgs e) //DIT
  387. {
  388. if (_events != null)
  389. {
  390. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  391. if (eh != null) eh(this, e);
  392. }
  393. }
  394. protected virtual void OnPreRender(EventArgs e) //DIT
  395. {
  396. if (_events != null)
  397. {
  398. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  399. if (eh != null) eh(this, e);
  400. }
  401. }
  402. protected virtual void OnUnload(EventArgs e) //DIT
  403. {
  404. if (_events != null)
  405. {
  406. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  407. if (eh != null) eh(this, e);
  408. }
  409. }
  410. [MonoTODO]
  411. protected void RaiseBubbleEvent(object source, EventArgs args)
  412. {
  413. throw new NotImplementedException();
  414. //return false;
  415. }
  416. protected virtual void Render(HtmlTextWriter writer) //DIT
  417. {
  418. RenderChildren(writer);
  419. }
  420. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  421. {
  422. if (_renderMethodDelegate != null)
  423. _renderMethodDelegate(writer, this);
  424. else if (_controls != null)
  425. foreach (Control c in _controls)
  426. c.RenderControl(writer);
  427. }
  428. protected virtual object SaveViewState()
  429. {
  430. return ViewState;
  431. }
  432. protected virtual void TrackViewState()
  433. {
  434. _trackViewState = true;
  435. }
  436. [MonoTODO]
  437. public virtual void Dispose()
  438. {
  439. //TODO: nuke stuff.
  440. throw new NotImplementedException();
  441. /*
  442. if (_events != null)
  443. {
  444. EventHandler eh = (EventHandler)(_events[DisposedEvent]);
  445. if (eh != null) eh(this, e);
  446. }
  447. */
  448. }
  449. public bool HasChildren
  450. {
  451. get { return (_controls != null && _controls.Count > 0); }
  452. }
  453. public event EventHandler DataBinding //DIT
  454. {
  455. add
  456. {
  457. Events.AddHandler(DataBindingEvent, value);
  458. }
  459. remove
  460. {
  461. Events.RemoveHandler(DataBindingEvent, value);
  462. }
  463. }
  464. public event EventHandler Disposed //DIT
  465. {
  466. add
  467. {
  468. Events.AddHandler(DisposedEvent, value);
  469. }
  470. remove
  471. {
  472. Events.RemoveHandler(DisposedEvent, value);
  473. }
  474. }
  475. public event EventHandler Init //DIT
  476. {
  477. add
  478. {
  479. Events.AddHandler(InitEvent, value);
  480. }
  481. remove
  482. {
  483. Events.RemoveHandler(InitEvent, value);
  484. }
  485. }
  486. public event EventHandler Load //DIT
  487. {
  488. add
  489. {
  490. Events.AddHandler(LoadEvent, value);
  491. }
  492. remove
  493. {
  494. Events.RemoveHandler(LoadEvent, value);
  495. }
  496. }
  497. public event EventHandler PreRender //DIT
  498. {
  499. add
  500. {
  501. Events.AddHandler(PreRenderEvent, value);
  502. }
  503. remove
  504. {
  505. Events.RemoveHandler(PreRenderEvent, value);
  506. }
  507. }
  508. public event EventHandler Unload //DIT
  509. {
  510. add
  511. {
  512. Events.AddHandler(UnloadEvent, value);
  513. }
  514. remove
  515. {
  516. Events.RemoveHandler(UnloadEvent, value);
  517. }
  518. }
  519. public virtual void DataBind() //DIT
  520. {
  521. OnDataBinding(EventArgs.Empty);
  522. if (_controls != null)
  523. foreach (Control c in _controls)
  524. c.DataBind();
  525. }
  526. public virtual Control FindControl(string id) //DIT
  527. {
  528. return FindControl(id, 0);
  529. }
  530. public virtual bool HasControls() //DIT
  531. {
  532. if (_controls != null && _controls.Count >0) return true;
  533. return false;
  534. }
  535. public void RenderControl(HtmlTextWriter writer)
  536. {
  537. if (_visible)
  538. {
  539. // By now, PreRender is fired here.
  540. OnPreRender (EventArgs.Empty); //FIXME
  541. //TODO: Something about tracing here.
  542. Render(writer);
  543. }
  544. }
  545. [MonoTODO]
  546. public string ResolveUrl(string relativeUrl)
  547. {
  548. return relativeUrl;
  549. }
  550. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  551. {
  552. _renderMethodDelegate = renderMethod;
  553. }
  554. protected void LoadRecursive()
  555. {
  556. OnLoad(EventArgs.Empty);
  557. if (_controls != null) foreach (Control c in _controls) c.LoadRecursive();
  558. }
  559. protected void UnloadRecursive(Boolean dispose)
  560. {
  561. OnUnload(EventArgs.Empty);
  562. if (_controls != null) foreach (Control c in _controls) c.UnloadRecursive(dispose);
  563. if (dispose) Dispose();
  564. }
  565. protected void PreRenderRecursiveInternal()
  566. {
  567. OnPreRender(EventArgs.Empty);
  568. if (_controls != null) foreach (Control c in _controls) c.PreRenderRecursiveInternal();
  569. }
  570. protected void InitRecursive(Control namingContainer)
  571. {
  572. if (_controls != null) foreach (Control c in _controls) c.InitRecursive(namingContainer);
  573. OnInit(EventArgs.Empty);
  574. }
  575. [MonoTODO]
  576. protected object SaveViewStateRecursive()
  577. {
  578. throw new NotImplementedException();
  579. }
  580. [MonoTODO]
  581. protected void LoadViewStateRecursive(Object savedState)
  582. {
  583. throw new NotImplementedException();
  584. }
  585. void IParserAccessor.AddParsedSubObject(object obj)
  586. {
  587. this.AddParsedSubObject(obj);
  588. }
  589. DataBindingCollection IDataBindingsAccessor.DataBindings
  590. {
  591. get
  592. {
  593. if(dataBindings == null)
  594. dataBindings = new DataBindingCollection();
  595. return dataBindings;
  596. }
  597. }
  598. bool IDataBindingsAccessor.HasDataBindings
  599. {
  600. get
  601. {
  602. return (dataBindings!=null && dataBindings.Count>0);
  603. }
  604. }
  605. internal bool AutoID
  606. {
  607. get { return autoID; }
  608. set { autoID = value; }
  609. }
  610. internal void PreventAutoID()
  611. {
  612. AutoID = false;
  613. }
  614. //TODO: I think there are some needed Interface implementations to do here.
  615. //TODO: Find api for INamingContainer.
  616. }
  617. }