Control.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. //more notes: look at the private on* methods for initialization order. they will help.
  11. //even more notes: view state info in trackviewstate method description. read later.
  12. //Ok, enough notes: what the heck is different between enable view state, and track view state.
  13. //cycle:
  14. //init is called when control is first created.
  15. //load view state ic called right after init to populate the view state.
  16. //loadpostdata is called if ipostbackdatahandler is implemented.
  17. //load is called when control is loaded into a page
  18. //raisepostdatachangedevent if ipostbackdatahandler is implemented.
  19. //raisepostbackevent if ipostbackeventhandler is implemented.
  20. //prerender is called when the server is about to render its page object
  21. //SaveViewState is called.
  22. //Dispose disposed/unload not sure but is last.
  23. //read this later. http://gotdotnet.com/quickstart/aspplus/
  24. using System;
  25. using System.Web;
  26. using System.ComponentModel;
  27. namespace System.Web.UI
  28. {
  29. public class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
  30. {
  31. private static readonly object DataBindingEvent = new object();
  32. private static readonly object DisposedEvent = new object();
  33. private static readonly object InitEvent = new object();
  34. private static readonly object LoadEvent = new object();
  35. private static readonly object PreRenderEvent = new object();
  36. private static readonly object UnloadEvent = new object();
  37. private string _clientId; //default to "ctrl#" where # is a static count of ctrls per page.
  38. private string _userId = null;
  39. private ControlCollection _controls = null;
  40. private bool _enableViewState = true;
  41. private bool _isNamingContainer = false;
  42. private Control _namingContainer = null;
  43. private Page _page = null;
  44. private Control _parent; //TODO: set default.
  45. private ISite _site; //TODO: what default?
  46. private bool _visible; //TODO: what default?
  47. private HttpContext _context = null;
  48. private bool _childControlsCreated = false;
  49. private StateBag _viewState = null;
  50. private bool _trackViewState = false; //TODO: I think this is right. Verify. Also modify other methods to use this.
  51. private EventHandlerList _events = new EventHandlerList();
  52. public Control() {}
  53. public virtual string ClientID
  54. {
  55. get
  56. {
  57. return _clientId;
  58. }
  59. }
  60. public virtual ControlCollection Controls //DIT
  61. {
  62. get
  63. {
  64. if (_controls == null) _controls = CreateControlCollection();
  65. return _controls;
  66. }
  67. }
  68. public virtual bool EnableViewState //DIT
  69. {
  70. get
  71. {
  72. return _enableViewState;
  73. }
  74. set
  75. {
  76. _enableViewState = value;
  77. }
  78. }
  79. public virtual string ID
  80. {
  81. get
  82. {
  83. if (_userId == null)
  84. return _clientId;
  85. else
  86. return _userId;
  87. }
  88. set
  89. {
  90. _userId = value;
  91. }
  92. }
  93. public virtual Control NamingContainer //DIT
  94. {
  95. get
  96. {
  97. if (_namingContainer == null && _parent != null)
  98. {
  99. if (_parent._isNamingContainer == false)
  100. _namingContainer = _parent.NamingContainer;
  101. else
  102. _namingContainer = _parent;
  103. }
  104. return _namingContainer;
  105. }
  106. }
  107. public virtual Page Page //DIT
  108. {
  109. get
  110. {
  111. if (_page == null && _parent != null) _page = _parent.Page;
  112. return _page;
  113. }
  114. set
  115. {
  116. _page = value;
  117. }
  118. }
  119. public virtual Control Parent //DIT
  120. {
  121. get
  122. {
  123. return _parent;
  124. }
  125. }
  126. public ISite Site //DIT
  127. {
  128. get
  129. {
  130. return _site;
  131. }
  132. set
  133. {
  134. _site = value;
  135. }
  136. }
  137. public virtual string TemplateSourceDirectory
  138. {
  139. get
  140. {
  141. return Context.Request.ApplicationPath;
  142. }
  143. }
  144. public virtual string UniqueID
  145. {
  146. get
  147. {
  148. if (_namingContainer == null)
  149. if (_userId == null)
  150. return _clientId;
  151. else
  152. return _clientId + ":" + _userId;
  153. else if (_userId == null)
  154. return _namingContainer.UniqueID + ":" + _clientId;
  155. return _namingContainer.UniqueID + ":" + _clientId + ":" + _userId;
  156. }
  157. }
  158. public virtual bool Visible
  159. {
  160. get
  161. {
  162. return _visible;
  163. }
  164. set
  165. {
  166. _visible = value;
  167. }
  168. }
  169. protected bool ChildControlsCreated
  170. {
  171. get
  172. {
  173. return _childControlsCreated;
  174. }
  175. set
  176. {
  177. _childControlsCreated = value;
  178. }
  179. }
  180. protected virtual HttpContext Context //DIT
  181. {
  182. get
  183. {
  184. HttpContext context;
  185. if (_context != null)
  186. return _context;
  187. if (_parent == null)
  188. return HttpContext.Current;
  189. context = _parent.Context;
  190. if (context != null)
  191. return context;
  192. return HttpContext.Current;
  193. }
  194. }
  195. protected EventHandlerList Events //DIT
  196. {
  197. get
  198. {
  199. if (_events != null) return _events;
  200. _events = new EventHandlerList();
  201. }
  202. }
  203. protected bool HasChildViewState
  204. {
  205. get
  206. { //FIXME: Relook over this. not sure!
  207. foreach (control c in _controls)
  208. if (c.IsTrackingViewState) return true;
  209. return false;
  210. }
  211. }
  212. protected bool IsTrackingViewState //DIT
  213. {
  214. get
  215. {
  216. return _trackingViewState;
  217. }
  218. }
  219. protected virtual StateBag ViewState
  220. {
  221. get
  222. {
  223. if (_viewState == null) _viewState = new StateBag(ViewStateIgnoreCase);
  224. return _viewState;
  225. }
  226. }
  227. protected virtual bool ViewStateIgnoresCase //DIT
  228. {
  229. get
  230. {
  231. return true;
  232. }
  233. }
  234. protected virtual void AddParsedSubObject(object obj) //DIT
  235. {
  236. Control c = (Control)obj;
  237. if (c != null) Controls.Add(c);
  238. }
  239. protected void BuildProfileTree(string parentId, bool calcViewState)
  240. {
  241. //TODO
  242. }
  243. protected void ClearChildViewState()
  244. {
  245. //TODO
  246. //Not quite sure about this. an example clears children then calls this, so I think
  247. //view state is local to the current object, not children.
  248. }
  249. protected virtual void CreateChildControls() {} //DIT
  250. protected virtual ControlCollection CreateControlCollection() //DIT
  251. {
  252. return new ControlCollection(this);
  253. }
  254. protected virtual void EnsureChildControls() //DIT
  255. {
  256. if (_childControlsCreated == false)
  257. {
  258. CreateChildControls();
  259. ChildControlsCreated = true;
  260. }
  261. }
  262. public virtual Control FindControl(string id)
  263. {
  264. int i;
  265. for (i = 0; i < _controls.Count; i++)
  266. if (_controls[i].ID == id) return _controls[i].ID;
  267. return null;
  268. }
  269. protected virtual Control FindControl(string id, int pathOffset)
  270. {
  271. int i;
  272. for (i = pathOffset; i < _controls.Count; i++)
  273. if (_controls[i].ID == id) return _controls[i].ID;
  274. return null;
  275. }
  276. protected virtual void LoadViewState(object savedState)
  277. {
  278. //TODO: What should I do by default?
  279. }
  280. protected string MapPathSecure(string virtualPath)
  281. {
  282. //TODO: Need to read up on security+web.
  283. }
  284. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  285. {
  286. return false;
  287. }
  288. protected virtual void OnDataBinding(EventArgs e) //DIT
  289. {
  290. if (_events != null)
  291. {
  292. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  293. if (eh != null) eh(this, e);
  294. }
  295. }
  296. protected virtual void OnInit(EventArgs e) //DIT
  297. {
  298. if (_events != null)
  299. {
  300. EventHandler eh = (EventHandler)(_events[InitEvent]);
  301. if (eh != null) eh(this, e);
  302. }
  303. }
  304. protected virtual void OnLoad(EventArgs e) //DIT
  305. {
  306. if (_events != null)
  307. {
  308. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  309. if (eh != null) eh(this, e);
  310. }
  311. }
  312. protected virtual void OnPreRender(EventArgs e) //DIT
  313. {
  314. if (_events != null)
  315. {
  316. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  317. if (eh != null) eh(this, e);
  318. }
  319. }
  320. protected virtual void OnUnload(EventArgs e) //DIT
  321. {
  322. if (_events != null)
  323. {
  324. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  325. if (eh != null) eh(this, e);
  326. }
  327. }
  328. protected void RaiseBubbleEvent(object source, EventArgs args)
  329. {
  330. return false;
  331. }
  332. protected virtual void Render(HtmlTextWriter writer) //DIT
  333. {
  334. RenderChildren(writer);
  335. }
  336. protected virtual void RenderChildren(HtmlTextWriter writer)
  337. {
  338. //if render method delegate is set, call it here. otherwise,
  339. //render any child controls. just a for loop?
  340. }
  341. protected virtual object SaveViewState()
  342. {
  343. return ViewState;
  344. }
  345. protected virtual void TrackViewState()
  346. {
  347. _trackViewState = true;
  348. }
  349. public virtual void DataBind()
  350. {
  351. //TODO: I think this recursively calls this method on its children.
  352. }
  353. public virtual void Dispose()
  354. {
  355. //TODO: nuke stuff.
  356. if (_events != null)
  357. {
  358. EventHandler eh = (EventHandler)(_events[DisposedEvent]);
  359. if (eh != null) eh(this, e);
  360. }
  361. }
  362. public event EventHandler DataBinding //DIT
  363. {
  364. add
  365. {
  366. Events.AddHandler(DataBindingEvent, value);
  367. }
  368. remove
  369. {
  370. Events.RemoveHandler(DataBindingEvent, value);
  371. }
  372. }
  373. public event EventHandler Disposed //DIT
  374. {
  375. add
  376. {
  377. Events.AddHandler(DisposedEvent, value);
  378. }
  379. remove
  380. {
  381. Events.RemoveHandler(DisposedEvent, value);
  382. }
  383. }
  384. public event EventHandler Init //DIT
  385. {
  386. add
  387. {
  388. Events.AddHandler(InitEvent, value);
  389. }
  390. remove
  391. {
  392. Events.RemoveHandler(InitEvent, value);
  393. }
  394. }
  395. public event EventHandler Load //DIT
  396. {
  397. add
  398. {
  399. Events.AddHandler(LoadEvent, value);
  400. }
  401. remove
  402. {
  403. Events.RemoveHandler(LoadEvent, value);
  404. }
  405. }
  406. public event EventHandler PreRender //DIT
  407. {
  408. add
  409. {
  410. Events.AddHandler(PreRenderEvent, value);
  411. }
  412. remove
  413. {
  414. Events.RemoveHandler(PreRenderEvent, value);
  415. }
  416. }
  417. public event EventHandler Unload //DIT
  418. {
  419. add
  420. {
  421. Events.AddHandler(UnloadEvent, value);
  422. }
  423. remove
  424. {
  425. Events.RemoveHandler(UnloadEvent, value);
  426. }
  427. }
  428. }
  429. }