Control.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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];
  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. throw new NotImplementedException();
  334. //TODO: Need to read up on security+web.
  335. }
  336. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  337. {
  338. return false;
  339. }
  340. protected virtual void OnDataBinding(EventArgs e) //DIT
  341. {
  342. if (_events != null)
  343. {
  344. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  345. if (eh != null) eh(this, e);
  346. }
  347. }
  348. protected virtual void OnInit(EventArgs e) //DIT
  349. {
  350. if (_events != null)
  351. {
  352. EventHandler eh = (EventHandler)(_events[InitEvent]);
  353. if (eh != null) eh(this, e);
  354. }
  355. }
  356. protected virtual void OnLoad(EventArgs e) //DIT
  357. {
  358. if (_events != null)
  359. {
  360. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  361. if (eh != null) eh(this, e);
  362. }
  363. }
  364. protected virtual void OnPreRender(EventArgs e) //DIT
  365. {
  366. if (_events != null)
  367. {
  368. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  369. if (eh != null) eh(this, e);
  370. }
  371. }
  372. protected virtual void OnUnload(EventArgs e) //DIT
  373. {
  374. if (_events != null)
  375. {
  376. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  377. if (eh != null) eh(this, e);
  378. }
  379. }
  380. protected void RaiseBubbleEvent(object source, EventArgs args)
  381. {
  382. throw new NotImplementedException();
  383. //return false;
  384. }
  385. protected virtual void Render(HtmlTextWriter writer) //DIT
  386. {
  387. RenderChildren(writer);
  388. }
  389. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  390. {
  391. if (_renderMethodDelegate != null)
  392. _renderMethodDelegate(writer, this);
  393. else if (_controls != null)
  394. foreach (Control c in _controls)
  395. c.RenderControl(writer);
  396. }
  397. protected virtual object SaveViewState()
  398. {
  399. return ViewState;
  400. }
  401. protected virtual void TrackViewState()
  402. {
  403. _trackViewState = true;
  404. }
  405. public virtual void Dispose()
  406. {
  407. //TODO: nuke stuff.
  408. throw new NotImplementedException();
  409. /*
  410. if (_events != null)
  411. {
  412. EventHandler eh = (EventHandler)(_events[DisposedEvent]);
  413. if (eh != null) eh(this, e);
  414. }
  415. */
  416. }
  417. public event EventHandler DataBinding //DIT
  418. {
  419. add
  420. {
  421. Events.AddHandler(DataBindingEvent, value);
  422. }
  423. remove
  424. {
  425. Events.RemoveHandler(DataBindingEvent, value);
  426. }
  427. }
  428. public event EventHandler Disposed //DIT
  429. {
  430. add
  431. {
  432. Events.AddHandler(DisposedEvent, value);
  433. }
  434. remove
  435. {
  436. Events.RemoveHandler(DisposedEvent, value);
  437. }
  438. }
  439. public event EventHandler Init //DIT
  440. {
  441. add
  442. {
  443. Events.AddHandler(InitEvent, value);
  444. }
  445. remove
  446. {
  447. Events.RemoveHandler(InitEvent, value);
  448. }
  449. }
  450. public event EventHandler Load //DIT
  451. {
  452. add
  453. {
  454. Events.AddHandler(LoadEvent, value);
  455. }
  456. remove
  457. {
  458. Events.RemoveHandler(LoadEvent, value);
  459. }
  460. }
  461. public event EventHandler PreRender //DIT
  462. {
  463. add
  464. {
  465. Events.AddHandler(PreRenderEvent, value);
  466. }
  467. remove
  468. {
  469. Events.RemoveHandler(PreRenderEvent, value);
  470. }
  471. }
  472. public event EventHandler Unload //DIT
  473. {
  474. add
  475. {
  476. Events.AddHandler(UnloadEvent, value);
  477. }
  478. remove
  479. {
  480. Events.RemoveHandler(UnloadEvent, value);
  481. }
  482. }
  483. public virtual void DataBind() //DIT
  484. {
  485. OnDataBinding(EventArgs.Empty);
  486. if (_controls != null)
  487. foreach (Control c in _controls)
  488. c.DataBind();
  489. }
  490. public virtual Control FindControl(string id) //DIT
  491. {
  492. return FindControl(id, 0);
  493. }
  494. public virtual bool HasControls() //DIT
  495. {
  496. if (_controls != null && _controls.Count >0) return true;
  497. return false;
  498. }
  499. public void RenderControl(HtmlTextWriter writer)
  500. {
  501. if (_visible)
  502. {
  503. //TODO: Something about tracing here.
  504. Render(writer);
  505. }
  506. }
  507. public string ResolveUrl(string relativeUrl)
  508. {
  509. throw new NotImplementedException();
  510. }
  511. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  512. {
  513. _renderMethodDelegate = renderMethod;
  514. }
  515. protected void LoadRecursive()
  516. {
  517. OnLoad(EventArgs.Empty);
  518. if (_controls != null) foreach (Control c in _controls) c.LoadRecursive();
  519. }
  520. protected void UnloadRecursive(Boolean dispose)
  521. {
  522. OnUnload(EventArgs.Empty);
  523. if (_controls != null) foreach (Control c in _controls) c.UnloadRecursive(dispose);
  524. if (dispose) Dispose();
  525. }
  526. protected void PreRenderRecursiveInternal()
  527. {
  528. OnPreRender(EventArgs.Empty);
  529. if (_controls != null) foreach (Control c in _controls) c.PreRenderRecursiveInternal();
  530. }
  531. protected void InitRecursive(Control namingContainer)
  532. {
  533. if (_controls != null) foreach (Control c in _controls) c.InitRecursive(namingContainer);
  534. OnInit(EventArgs.Empty);
  535. }
  536. protected object SaveViewStateRecursive()
  537. {
  538. throw new NotImplementedException();
  539. }
  540. protected void LoadViewStateRecursive(Object savedState)
  541. {
  542. throw new NotImplementedException();
  543. }
  544. void IParserAccessor.AddParsedSubObject(object obj)
  545. {
  546. this.AddParsedSubObject(obj);
  547. }
  548. DataBindingCollection IDataBindingsAccessor.DataBindings
  549. {
  550. get
  551. {
  552. if(dataBindings == null)
  553. dataBindings = new DataBindingCollection();
  554. return dataBindings;
  555. }
  556. }
  557. bool IDataBindingsAccessor.HasDataBindings
  558. {
  559. get
  560. {
  561. return (dataBindings!=null && dataBindings.Count>0);
  562. }
  563. }
  564. //TODO: I think there are some needed Interface implementations to do here.
  565. //TODO: Find api for INamingContainer.
  566. }
  567. }