Control.cs 25 KB

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