Control.cs 27 KB

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