Control.cs 22 KB

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