Control.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  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. public virtual string UniqueID
  198. {
  199. get
  200. {
  201. //TODO: Some Naming container methods here. What are they? Why arnt they declared?
  202. //Note: Nuked the old stuff here. Was total crap. :)
  203. throw new NotImplementedException();
  204. }
  205. }
  206. public virtual bool Visible
  207. { //TODO: Are children visible when parents are not?
  208. get
  209. {
  210. return _visible;
  211. }
  212. set
  213. {
  214. _visible = value;
  215. }
  216. }
  217. protected bool ChildControlsCreated //DIT
  218. {
  219. get
  220. {
  221. return _childControlsCreated;
  222. }
  223. set
  224. {
  225. if (value == false && _childControlsCreated == true)
  226. _controls.Clear();
  227. _childControlsCreated = value;
  228. }
  229. }
  230. protected virtual HttpContext Context //DIT
  231. {
  232. get
  233. {
  234. HttpContext context;
  235. if (_context != null)
  236. return _context;
  237. if (_parent == null)
  238. return HttpContext.Current;
  239. context = _parent.Context;
  240. if (context != null)
  241. return context;
  242. return HttpContext.Current;
  243. }
  244. }
  245. protected EventHandlerList Events //DIT
  246. {
  247. get
  248. {
  249. if (_events == null)
  250. {
  251. _events = new EventHandlerList();
  252. }
  253. return _events;
  254. }
  255. }
  256. protected bool HasChildViewState //DIT
  257. {
  258. get
  259. {
  260. if (_childViewStates == null) return false;
  261. return true;
  262. }
  263. }
  264. protected bool IsTrackingViewState //DIT
  265. {
  266. get
  267. {
  268. return _trackViewState;
  269. }
  270. }
  271. protected virtual StateBag ViewState
  272. {
  273. get
  274. {
  275. if(_viewState == null)
  276. {
  277. _viewState = new StateBag(ViewStateIgnoresCase);
  278. if(IsTrackingViewState)
  279. _viewState.TrackViewState();
  280. }
  281. return _viewState;
  282. }
  283. }
  284. protected virtual bool ViewStateIgnoresCase //DIT
  285. {
  286. get
  287. {
  288. return true;
  289. }
  290. }
  291. protected virtual void AddParsedSubObject(object obj) //DIT
  292. {
  293. Control c = (Control)obj;
  294. if (c != null) Controls.Add(c);
  295. }
  296. protected void BuildProfileTree(string parentId, bool calcViewState)
  297. {
  298. //TODO
  299. }
  300. protected void ClearChildViewState()
  301. {
  302. //TODO
  303. //Not quite sure about this. an example clears children then calls this, so I think
  304. //view state is local to the current object, not children.
  305. }
  306. protected virtual void CreateChildControls() {} //DIT
  307. protected virtual ControlCollection CreateControlCollection() //DIT
  308. {
  309. return new ControlCollection(this);
  310. }
  311. protected virtual void EnsureChildControls() //DIT
  312. {
  313. if (_childControlsCreated == false)
  314. {
  315. CreateChildControls();
  316. ChildControlsCreated = true;
  317. }
  318. }
  319. protected virtual Control FindControl(string id, int pathOffset)
  320. {
  321. //TODO: I think there is Naming Container stuff here. Redo.
  322. int i;
  323. for (i = pathOffset; i < _controls.Count; i++)
  324. if (_controls[i].ID == id) return _controls[i].ID;
  325. return null;
  326. }
  327. protected virtual void LoadViewState(object savedState)
  328. {
  329. //TODO: What should I do by default?
  330. }
  331. protected string MapPathSecure(string virtualPath)
  332. {
  333. //TODO: Need to read up on security+web.
  334. }
  335. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  336. {
  337. return false;
  338. }
  339. protected virtual void OnDataBinding(EventArgs e) //DIT
  340. {
  341. if (_events != null)
  342. {
  343. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  344. if (eh != null) eh(this, e);
  345. }
  346. }
  347. protected virtual void OnInit(EventArgs e) //DIT
  348. {
  349. if (_events != null)
  350. {
  351. EventHandler eh = (EventHandler)(_events[InitEvent]);
  352. if (eh != null) eh(this, e);
  353. }
  354. }
  355. protected virtual void OnLoad(EventArgs e) //DIT
  356. {
  357. if (_events != null)
  358. {
  359. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  360. if (eh != null) eh(this, e);
  361. }
  362. }
  363. protected virtual void OnPreRender(EventArgs e) //DIT
  364. {
  365. if (_events != null)
  366. {
  367. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  368. if (eh != null) eh(this, e);
  369. }
  370. }
  371. protected virtual void OnUnload(EventArgs e) //DIT
  372. {
  373. if (_events != null)
  374. {
  375. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  376. if (eh != null) eh(this, e);
  377. }
  378. }
  379. protected void RaiseBubbleEvent(object source, EventArgs args)
  380. {
  381. return false;
  382. }
  383. protected virtual void Render(HtmlTextWriter writer) //DIT
  384. {
  385. RenderChildren(writer);
  386. }
  387. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  388. {
  389. if (_renderMethodDelegate != null)
  390. _renderMethodDelegate(writer, this);
  391. else if (_controls != null)
  392. foreach (Control c in _controls)
  393. c.RenderControl(writer);
  394. }
  395. protected virtual object SaveViewState()
  396. {
  397. return ViewState;
  398. }
  399. protected virtual void TrackViewState()
  400. {
  401. _trackViewState = true;
  402. }
  403. public virtual void Dispose()
  404. {
  405. //TODO: nuke stuff.
  406. if (_events != null)
  407. {
  408. EventHandler eh = (EventHandler)(_events[DisposedEvent]);
  409. if (eh != null) eh(this, e);
  410. }
  411. }
  412. public event EventHandler DataBinding //DIT
  413. {
  414. add
  415. {
  416. Events.AddHandler(DataBindingEvent, value);
  417. }
  418. remove
  419. {
  420. Events.RemoveHandler(DataBindingEvent, value);
  421. }
  422. }
  423. public event EventHandler Disposed //DIT
  424. {
  425. add
  426. {
  427. Events.AddHandler(DisposedEvent, value);
  428. }
  429. remove
  430. {
  431. Events.RemoveHandler(DisposedEvent, value);
  432. }
  433. }
  434. public event EventHandler Init //DIT
  435. {
  436. add
  437. {
  438. Events.AddHandler(InitEvent, value);
  439. }
  440. remove
  441. {
  442. Events.RemoveHandler(InitEvent, value);
  443. }
  444. }
  445. public event EventHandler Load //DIT
  446. {
  447. add
  448. {
  449. Events.AddHandler(LoadEvent, value);
  450. }
  451. remove
  452. {
  453. Events.RemoveHandler(LoadEvent, value);
  454. }
  455. }
  456. public event EventHandler PreRender //DIT
  457. {
  458. add
  459. {
  460. Events.AddHandler(PreRenderEvent, value);
  461. }
  462. remove
  463. {
  464. Events.RemoveHandler(PreRenderEvent, value);
  465. }
  466. }
  467. public event EventHandler Unload //DIT
  468. {
  469. add
  470. {
  471. Events.AddHandler(UnloadEvent, value);
  472. }
  473. remove
  474. {
  475. Events.RemoveHandler(UnloadEvent, value);
  476. }
  477. }
  478. public virtual void DataBind() //DIT
  479. {
  480. OnDataBinding(EventArgs.Empty);
  481. if (_controls != null)
  482. foreach (Control c in _controls)
  483. c.DataBind();
  484. }
  485. public virtual Control FindControl(string id) //DIT
  486. {
  487. return FindControl(id, 0);
  488. }
  489. public virtual bool HasControls() //DIT
  490. {
  491. if (_controls != null && _controls.Count >0) return true;
  492. return false;
  493. }
  494. public void RenderControl(HtmlTextWriter writer)
  495. {
  496. if (_visible)
  497. {
  498. //TODO: Something about tracing here.
  499. Render(writer);
  500. }
  501. }
  502. public string ResolveUrl(string relativeUrl) {} //TODO
  503. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  504. {
  505. _renderMethodDelegate = renderMethod;
  506. }
  507. protected void LoadRecursive()
  508. {
  509. OnLoad(EventArgs.Empty);
  510. if (_controls != null) foreach (Control c in _controls) c.LoadRecursive();
  511. }
  512. protected void UnloadRecursive(Boolean dispose)
  513. {
  514. OnUnload(EventArgs.Empty);
  515. if (_controls != null) foreach (Control c in _controls) c.UnloadRecursive();
  516. if (dispose) Dispose();
  517. }
  518. protected void PreRenderRecursiveInternal()
  519. {
  520. OnPreRender(EventArgs.Empty);
  521. if (_controls != null) foreach (Control c in _controls) c.PreRenderRecursiveInternal();
  522. }
  523. protected void InitRecursive(Control namingContainer)
  524. {
  525. if (_controls != null) foreach (Control c in _controls) c.InitRecursive(namingContainer);
  526. OnInit(EventArgs.Empty);
  527. }
  528. protected object SaveViewStateRecursive()
  529. {
  530. //TODO
  531. }
  532. protected void LoadViewStateRecursive(Object savedState)
  533. {
  534. //TODO
  535. }
  536. void IParserAccessor.AddParsedSubObject(object obj)
  537. {
  538. this.AddParsedSubObject(obj);
  539. }
  540. DataBindingCollection IDataBindingsAccessor.DataBindings
  541. {
  542. get
  543. {
  544. if(dataBindings == null)
  545. dataBindings = new DataBindingCollection();
  546. return dataBindings;
  547. }
  548. }
  549. bool IDataBindingsAccessor.HasDataBindings
  550. {
  551. get
  552. {
  553. return (dataBindings!=null && dataBindings.Count>0);
  554. }
  555. }
  556. //TODO: I think there are some needed Interface implementations to do here.
  557. //TODO: Find api for INamingContainer.
  558. }
  559. }