Control.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 is called when control is loaded into a page
  16. //prerender is called when the server is about to render its page object
  17. //disposed/unload not sure but is last.
  18. //read this later. http://gotdotnet.com/quickstart/aspplus/
  19. using System;
  20. using System.Web;
  21. using System.ComponentModel;
  22. namespace System.Web.UI
  23. {
  24. public class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
  25. {
  26. public event EventHandler DataBinding;
  27. public event EventHandler Disposed;
  28. public event EventHandler Init;
  29. public event EventHandler Load;
  30. public event EventHandler PreRender;
  31. public event EventHandler Unload;
  32. private string _clientId; //default to "ctrl#" where # is a static count of ctrls per page.
  33. private string _userId = null;
  34. private ControlCollection _controls;
  35. private bool _enableViewState = true;
  36. private Control _namingContainer;
  37. private Page _page;
  38. private Control _parent; //TODO: set default.
  39. private ISite _site; //TODO: what default?
  40. private bool _visible; //TODO: what default?
  41. private HttpContext _context = null;
  42. private bool _childControlsCreated = false;
  43. private StateBag _viewState; //TODO: help me.
  44. private bool _trackViewState = false; //TODO: I think this is right. Verify. Also modify other methods to use this.
  45. private bool _viewStateIgnoreCase = true;
  46. public Control()
  47. {
  48. _namingContainer = _parent;
  49. _viewState = new StateBag(_viewStateIgnoreCase);
  50. _events = new EventHandlerList();
  51. _controls = this.CreateControlCollection(); //FIXME: this goes here?
  52. }
  53. public ~Control()
  54. {
  55. Dispose();
  56. }
  57. public virtual string ClientID
  58. {
  59. get
  60. {
  61. return _clientId;
  62. }
  63. }
  64. public virtual ControlCollection Controls
  65. {
  66. get
  67. {
  68. return _controls;
  69. }
  70. }
  71. public virtual bool EnableViewState
  72. {
  73. get
  74. {
  75. return _enableViewState;
  76. }
  77. set
  78. {
  79. _enableViewState = value;
  80. }
  81. }
  82. public virtual string ID
  83. {
  84. get
  85. {
  86. if (_userId == null)
  87. return _clientId;
  88. else
  89. return _userId;
  90. }
  91. set
  92. {
  93. _userId = value;
  94. }
  95. }
  96. public virtual Control NamingContainer
  97. {
  98. get
  99. {
  100. return _namingContainer;
  101. }
  102. }
  103. public virtual Page Page
  104. {
  105. get
  106. {
  107. return _page;
  108. }
  109. set
  110. {
  111. _page = value;
  112. }
  113. }
  114. public virtual Control Parent
  115. {
  116. get
  117. {
  118. return _parent;
  119. }
  120. }
  121. public ISite Site
  122. {
  123. get
  124. {
  125. return _site;
  126. }
  127. set
  128. {
  129. _site = value;
  130. }
  131. }
  132. public virtual string TemplateSourceDirectory
  133. {
  134. get
  135. {
  136. return Context.Request.ApplicationPath;
  137. }
  138. }
  139. public virtual string UniqueID
  140. {
  141. get
  142. {
  143. if (_namingContainer == null)
  144. if (_userId == null)
  145. return _clientId;
  146. else
  147. return _clientId + ":" + _userId;
  148. else if (_userId == null)
  149. return _namingContainer.UniqueID + ":" + _clientId;
  150. return _namingContainer.UniqueID + ":" + _clientId + ":" + _userId;
  151. }
  152. }
  153. public virtual bool Visible
  154. {
  155. get
  156. {
  157. return _visible;
  158. }
  159. set
  160. {
  161. _visible = value;
  162. }
  163. }
  164. protected bool ChildControlsCreated
  165. {
  166. get
  167. {
  168. return _childControlsCreated;
  169. }
  170. set
  171. {
  172. _childControlsCreated = value;
  173. }
  174. }
  175. protected virtual HttpContext Context
  176. {
  177. get
  178. {
  179. HttpContext context;
  180. if (_context != null)
  181. return _context;
  182. if (_parent == null)
  183. return HttpContext.Current;
  184. context = _parent.Context;
  185. if (context != null)
  186. return context;
  187. return HttpContext.Current;
  188. }
  189. }
  190. protected EventHandlerList Events
  191. {
  192. get
  193. {
  194. EventHandlerList e = new EventHandlerList();
  195. e.AddHandler(this, DataBinding);
  196. e.AddHandler(this, Disposed);
  197. e.AddHandler(this, Init);
  198. e.AddHandler(this, Load);
  199. e.AddHandler(this, PreRender);
  200. e.AddHandler(this, Unload);
  201. return e;
  202. }
  203. }
  204. protected bool HasChildViewState
  205. {
  206. get
  207. { //FIXME: Relook over this. not sure!
  208. foreach (control c in _controls)
  209. if (c.IsTrackingViewState) return true;
  210. return false;
  211. }
  212. }
  213. protected bool IsTrackingViewState
  214. {
  215. get
  216. { //FIXME: ME TO!
  217. return _enableViewState;
  218. }
  219. }
  220. protected virtual StateBag ViewState
  221. {
  222. get
  223. {
  224. return _viewState;
  225. }
  226. }
  227. protected virtual bool ViewStateIgnoresCase
  228. {
  229. get
  230. {
  231. return _viewStateIgnoreCase;
  232. }
  233. }
  234. protected virtual void AddParsedSubObject(object obj)
  235. {
  236. _controls.Add(obj);
  237. }
  238. protected void BuildProfileTree(string parentId, bool calcViewState)
  239. {
  240. //TODO
  241. }
  242. protected void ClearChildViewState()
  243. {
  244. //TODO
  245. //Not quite sure about this. an example clears children then calls this, so I think
  246. //view state is local to the current object, not children.
  247. }
  248. protected virtual void CreateChildControls() {}
  249. protected virtual ControlCollection CreateControlCollection()
  250. {
  251. _controls = new ControlCollection(this);
  252. }
  253. protected virtual void EnsureChildControls() {} //FIXME: I think this should be empty.
  254. public virtual Control FindControl(string id)
  255. {
  256. int i;
  257. for (i = 0; i < _controls.Count; i++)
  258. if (_controls[i].ID == id) return _controls[i].ID;
  259. return null;
  260. }
  261. protected virtual Control FindControl(string id, int pathOffset)
  262. {
  263. int i;
  264. for (i = pathOffset; i < _controls.Count; i++)
  265. if (_controls[i].ID == id) return _controls[i].ID;
  266. return null;
  267. }
  268. protected virtual void LoadViewState(object savedState)
  269. {
  270. //TODO: What should I do by default?
  271. }
  272. protected string MapPathSecure(string virtualPath)
  273. {
  274. //TODO: Need to read up on security+web.
  275. }
  276. protected virtual bool OnBubbleEvent(object source, EventArgs args)
  277. {
  278. return false; //FIXME: It might throw "ItemCommand". not sure.
  279. }
  280. protected virtual void OnDataBinding(EventArgs e)
  281. {
  282. if (DataBinding != null) DataBinding(this, e);
  283. }
  284. protected virtual void OnInit(EventArgs e)
  285. {
  286. if (Init != null) Init(this, e);
  287. }
  288. protected virtual void OnPreRender(EventArgs e)
  289. {
  290. if (PreRender != null) PreRender(this, e);
  291. }
  292. protected virtual void OnUnload(EventArgs e)
  293. {
  294. if (Unload != null) Unload(this, e);
  295. }
  296. protected void RaiseBubbleEvent(object source, EventArgs args)
  297. {
  298. _parent.OnBubbleEvent(source, args); //FIXME: I think this is right. Check though.
  299. }
  300. protected virtual void Render(HtmlTextWriter writer) {} //FIXME: Default?
  301. protected virtual void RenderChildren(HtmlTextWriter writer)
  302. {
  303. //if render method delegate is set, call it here. otherwise,
  304. //render any child controls. just a for loop?
  305. }
  306. protected virtual object SaveViewState()
  307. {
  308. return ViewState;
  309. }
  310. protected virtual void TrackViewState()
  311. {
  312. _trackViewState = true;
  313. }
  314. public virtual void DataBind()
  315. {
  316. //TODO: I think this recursively calls this method on its children.
  317. }
  318. public virtual void Dispose()
  319. {
  320. //TODO: nuke stuff.
  321. if (Disposed != null) Disposed(this, e);
  322. }
  323. }
  324. }