2
0

Control.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 bool autoID = true;
  99. private bool creatingControls = false;
  100. private DataBindingCollection dataBindings = null;
  101. public Control()
  102. {
  103. if (this is INamingContainer) _isNamingContainer = true;
  104. }
  105. public Control BindingContainer
  106. {
  107. get {
  108. Control container = NamingContainer;
  109. if (_isNamingContainer)
  110. container = container.BindingContainer;
  111. return container;
  112. }
  113. }
  114. public virtual string ClientID //DIT
  115. {
  116. get
  117. {
  118. if (_cachedUserId != null && _cachedClientId != null)
  119. return _cachedClientId;
  120. _cachedUserId = UniqueID.Replace(':', '_');
  121. return _cachedUserId;
  122. }
  123. }
  124. public virtual ControlCollection Controls //DIT
  125. {
  126. get
  127. {
  128. if (_controls == null) _controls = CreateControlCollection();
  129. return _controls;
  130. }
  131. }
  132. public virtual bool EnableViewState //DIT
  133. {
  134. get
  135. {
  136. return _enableViewState;
  137. }
  138. set
  139. {
  140. _enableViewState = value;
  141. }
  142. }
  143. public virtual string ID
  144. {
  145. get //DIT
  146. {
  147. return _userId;
  148. }
  149. set
  150. {
  151. if (value == null || value == "") return;
  152. _userId = value;
  153. _cachedUserId = null;
  154. //TODO: Some Naming Container stuff here I think.
  155. }
  156. }
  157. public virtual Control NamingContainer //DIT
  158. {
  159. get
  160. {
  161. if (_namingContainer == null && _parent != null)
  162. {
  163. if (_parent._isNamingContainer == false)
  164. _namingContainer = _parent.NamingContainer;
  165. else
  166. _namingContainer = _parent;
  167. }
  168. return _namingContainer;
  169. }
  170. }
  171. public virtual Page Page //DIT
  172. {
  173. get
  174. {
  175. if (_page == null && _parent != null) _page = _parent.Page;
  176. return _page;
  177. }
  178. set
  179. {
  180. _page = value;
  181. }
  182. }
  183. public virtual Control Parent //DIT
  184. {
  185. get
  186. {
  187. return _parent;
  188. }
  189. }
  190. public ISite Site //DIT
  191. {
  192. get
  193. {
  194. return _site;
  195. }
  196. set
  197. {
  198. _site = value;
  199. }
  200. }
  201. public virtual string TemplateSourceDirectory
  202. {
  203. get
  204. {
  205. return Context.Request.ApplicationPath; //TODO: Dont think this is right.
  206. }
  207. }
  208. [MonoTODO]
  209. public virtual string UniqueID
  210. {
  211. get
  212. {
  213. //TODO: Some Naming container methods here. What are they? Why arnt they declared?
  214. //Note: Nuked the old stuff here. Was total crap. :)
  215. return ID;
  216. }
  217. }
  218. public virtual bool Visible
  219. { //TODO: Are children visible when parents are not?
  220. get
  221. {
  222. return _visible;
  223. }
  224. set
  225. {
  226. _visible = value;
  227. }
  228. }
  229. protected bool ChildControlsCreated //DIT
  230. {
  231. get
  232. {
  233. return _childControlsCreated;
  234. }
  235. set
  236. {
  237. if (value == false && _childControlsCreated == true)
  238. _controls.Clear();
  239. _childControlsCreated = value;
  240. }
  241. }
  242. protected virtual HttpContext Context //DIT
  243. {
  244. get
  245. {
  246. HttpContext context;
  247. if (_context != null)
  248. return _context;
  249. if (_parent == null)
  250. return HttpContext.Current;
  251. context = _parent.Context;
  252. if (context != null)
  253. return context;
  254. return HttpContext.Current;
  255. }
  256. }
  257. protected EventHandlerList Events //DIT
  258. {
  259. get
  260. {
  261. if (_events == null)
  262. {
  263. _events = new EventHandlerList();
  264. }
  265. return _events;
  266. }
  267. }
  268. protected bool HasChildViewState //DIT
  269. {
  270. get
  271. {
  272. if (_childViewStates == null) return false;
  273. return true;
  274. }
  275. }
  276. protected bool IsTrackingViewState //DIT
  277. {
  278. get
  279. {
  280. return _trackViewState;
  281. }
  282. }
  283. protected virtual StateBag ViewState
  284. {
  285. get
  286. {
  287. if(_viewState == null)
  288. {
  289. _viewState = new StateBag(ViewStateIgnoresCase);
  290. if(IsTrackingViewState)
  291. _viewState.TrackViewState();
  292. }
  293. return _viewState;
  294. }
  295. }
  296. protected virtual bool ViewStateIgnoresCase //DIT
  297. {
  298. get
  299. {
  300. return true;
  301. }
  302. }
  303. private int defaultNumberID;
  304. protected internal virtual void AddedControl (Control control, int index)
  305. {
  306. /* Ensure the control don't have more than 1 parent */
  307. if (control._parent != null)
  308. control._parent.Controls.Remove (control);
  309. control._parent = this;
  310. control._page = Page;
  311. // Without this, DataBoundLiteralControl crashes in OnDataBound event.
  312. Control namingContainer = NamingContainer;
  313. if (namingContainer != null)
  314. control._namingContainer = namingContainer;
  315. if (control.AutoID == true && control.ID == null)
  316. control.ID = "_ctrl_" + defaultNumberID++;
  317. }
  318. protected virtual void AddParsedSubObject(object obj) //DIT
  319. {
  320. Control c = (Control)obj;
  321. if (c != null) Controls.Add(c);
  322. }
  323. protected void BuildProfileTree(string parentId, bool calcViewState)
  324. {
  325. //TODO
  326. }
  327. protected void ClearChildViewState()
  328. {
  329. //TODO
  330. //Not quite sure about this. an example clears children then calls this, so I think
  331. //view state is local to the current object, not children.
  332. }
  333. protected virtual void CreateChildControls() {} //DIT
  334. protected virtual ControlCollection CreateControlCollection() //DIT
  335. {
  336. return new ControlCollection(this);
  337. }
  338. protected virtual void EnsureChildControls () //DIT
  339. {
  340. if (ChildControlsCreated == false && !creatingControls) {
  341. creatingControls = true;
  342. CreateChildControls();
  343. ChildControlsCreated = true;
  344. creatingControls = false;
  345. }
  346. }
  347. protected virtual Control FindControl(string id, int pathOffset)
  348. {
  349. //TODO: I think there is Naming Container stuff here. Redo.
  350. int i;
  351. Control ctrl;
  352. for (i = pathOffset; i < _controls.Count; i++){
  353. ctrl = _controls [i];
  354. if (ctrl.ID == id)
  355. return ctrl;
  356. if (ctrl.Controls.Count > 0){
  357. Control other = ctrl.FindControl (id);
  358. if (other != null)
  359. return other;
  360. }
  361. }
  362. return null;
  363. }
  364. protected virtual void LoadViewState(object savedState)
  365. {
  366. ViewState.LoadViewState (savedState);
  367. }
  368. [MonoTODO]
  369. protected string MapPathSecure(string virtualPath)
  370. {
  371. //TODO: Need to read up on security+web.
  372. //Return the same path. So AdRotator can read its config file.
  373. return virtualPath;
  374. }
  375. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  376. {
  377. return false;
  378. }
  379. protected virtual void OnDataBinding(EventArgs e) //DIT
  380. {
  381. if (_events != null)
  382. {
  383. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  384. if (eh != null) eh(this, e);
  385. }
  386. }
  387. protected virtual void OnInit(EventArgs e) //DIT
  388. {
  389. if (_events != null)
  390. {
  391. EventHandler eh = (EventHandler)(_events[InitEvent]);
  392. if (eh != null) eh(this, e);
  393. }
  394. }
  395. protected virtual void OnLoad(EventArgs e) //DIT
  396. {
  397. if (_events != null)
  398. {
  399. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  400. if (eh != null) eh(this, e);
  401. }
  402. }
  403. protected virtual void OnPreRender(EventArgs e) //DIT
  404. {
  405. if (_events != null)
  406. {
  407. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  408. if (eh != null) eh(this, e);
  409. }
  410. }
  411. protected virtual void OnUnload(EventArgs e) //DIT
  412. {
  413. if (_events != null)
  414. {
  415. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  416. if (eh != null) eh(this, e);
  417. }
  418. }
  419. [MonoTODO]
  420. protected void RaiseBubbleEvent(object source, EventArgs args)
  421. {
  422. throw new NotImplementedException();
  423. //return false;
  424. }
  425. protected virtual void Render(HtmlTextWriter writer) //DIT
  426. {
  427. RenderChildren(writer);
  428. }
  429. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  430. {
  431. if (_renderMethodDelegate != null)
  432. _renderMethodDelegate(writer, this);
  433. else if (_controls != null)
  434. foreach (Control c in _controls)
  435. c.RenderControl(writer);
  436. }
  437. protected virtual object SaveViewState()
  438. {
  439. return ViewState;
  440. }
  441. protected virtual void TrackViewState()
  442. {
  443. _trackViewState = true;
  444. }
  445. [MonoTODO]
  446. public virtual void Dispose()
  447. {
  448. //TODO: nuke stuff.
  449. throw new NotImplementedException();
  450. /*
  451. if (_events != null)
  452. {
  453. EventHandler eh = (EventHandler)(_events[DisposedEvent]);
  454. if (eh != null) eh(this, e);
  455. }
  456. */
  457. }
  458. public bool HasChildren
  459. {
  460. get { return (_controls != null && _controls.Count > 0); }
  461. }
  462. public event EventHandler DataBinding //DIT
  463. {
  464. add
  465. {
  466. Events.AddHandler(DataBindingEvent, value);
  467. }
  468. remove
  469. {
  470. Events.RemoveHandler(DataBindingEvent, value);
  471. }
  472. }
  473. public event EventHandler Disposed //DIT
  474. {
  475. add
  476. {
  477. Events.AddHandler(DisposedEvent, value);
  478. }
  479. remove
  480. {
  481. Events.RemoveHandler(DisposedEvent, value);
  482. }
  483. }
  484. public event EventHandler Init //DIT
  485. {
  486. add
  487. {
  488. Events.AddHandler(InitEvent, value);
  489. }
  490. remove
  491. {
  492. Events.RemoveHandler(InitEvent, value);
  493. }
  494. }
  495. public event EventHandler Load //DIT
  496. {
  497. add
  498. {
  499. Events.AddHandler(LoadEvent, value);
  500. }
  501. remove
  502. {
  503. Events.RemoveHandler(LoadEvent, value);
  504. }
  505. }
  506. public event EventHandler PreRender //DIT
  507. {
  508. add
  509. {
  510. Events.AddHandler(PreRenderEvent, value);
  511. }
  512. remove
  513. {
  514. Events.RemoveHandler(PreRenderEvent, value);
  515. }
  516. }
  517. public event EventHandler Unload //DIT
  518. {
  519. add
  520. {
  521. Events.AddHandler(UnloadEvent, value);
  522. }
  523. remove
  524. {
  525. Events.RemoveHandler(UnloadEvent, value);
  526. }
  527. }
  528. public virtual void DataBind() //DIT
  529. {
  530. OnDataBinding(EventArgs.Empty);
  531. if (_controls != null)
  532. foreach (Control c in _controls)
  533. c.DataBind();
  534. }
  535. public virtual Control FindControl(string id) //DIT
  536. {
  537. return FindControl(id, 0);
  538. }
  539. public virtual bool HasControls() //DIT
  540. {
  541. if (_controls != null && _controls.Count >0) return true;
  542. return false;
  543. }
  544. public void RenderControl(HtmlTextWriter writer)
  545. {
  546. if (_visible)
  547. {
  548. // By now, PreRender is fired here.
  549. OnPreRender (EventArgs.Empty); //FIXME
  550. //TODO: Something about tracing here.
  551. Render(writer);
  552. }
  553. }
  554. [MonoTODO]
  555. public string ResolveUrl(string relativeUrl)
  556. {
  557. return relativeUrl;
  558. }
  559. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  560. {
  561. _renderMethodDelegate = renderMethod;
  562. }
  563. protected void LoadRecursive()
  564. {
  565. OnLoad(EventArgs.Empty);
  566. if (_controls != null) foreach (Control c in _controls) c.LoadRecursive();
  567. }
  568. protected void UnloadRecursive(Boolean dispose)
  569. {
  570. OnUnload(EventArgs.Empty);
  571. if (_controls != null) foreach (Control c in _controls) c.UnloadRecursive(dispose);
  572. if (dispose) Dispose();
  573. }
  574. protected void PreRenderRecursiveInternal()
  575. {
  576. OnPreRender(EventArgs.Empty);
  577. if (_controls != null) foreach (Control c in _controls) c.PreRenderRecursiveInternal();
  578. }
  579. protected void InitRecursive(Control namingContainer)
  580. {
  581. if (_controls != null) foreach (Control c in _controls) c.InitRecursive(namingContainer);
  582. OnInit(EventArgs.Empty);
  583. }
  584. [MonoTODO]
  585. protected object SaveViewStateRecursive()
  586. {
  587. throw new NotImplementedException();
  588. }
  589. [MonoTODO]
  590. protected void LoadViewStateRecursive(Object savedState)
  591. {
  592. throw new NotImplementedException();
  593. }
  594. void IParserAccessor.AddParsedSubObject(object obj)
  595. {
  596. this.AddParsedSubObject(obj);
  597. }
  598. DataBindingCollection IDataBindingsAccessor.DataBindings
  599. {
  600. get
  601. {
  602. if(dataBindings == null)
  603. dataBindings = new DataBindingCollection();
  604. return dataBindings;
  605. }
  606. }
  607. bool IDataBindingsAccessor.HasDataBindings
  608. {
  609. get
  610. {
  611. return (dataBindings!=null && dataBindings.Count>0);
  612. }
  613. }
  614. internal bool AutoID
  615. {
  616. get { return autoID; }
  617. set { autoID = value; }
  618. }
  619. internal void PreventAutoID()
  620. {
  621. AutoID = false;
  622. }
  623. //TODO: I think there are some needed Interface implementations to do here.
  624. //TODO: Find api for INamingContainer.
  625. }
  626. }