Control.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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. protected void RaiseBubbleEvent(object source, EventArgs args)
  424. {
  425. Control c = Parent;
  426. while (c != null) {
  427. if (c.OnBubbleEvent (source, args))
  428. break;
  429. c = c.Parent;
  430. }
  431. }
  432. protected virtual void Render(HtmlTextWriter writer) //DIT
  433. {
  434. RenderChildren(writer);
  435. }
  436. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  437. {
  438. if (_renderMethodDelegate != null)
  439. _renderMethodDelegate(writer, this);
  440. else if (_controls != null)
  441. foreach (Control c in _controls)
  442. c.RenderControl(writer);
  443. }
  444. protected virtual object SaveViewState ()
  445. {
  446. if (_viewState == null)
  447. return null;
  448. return _viewState.SaveViewState ();
  449. }
  450. protected virtual void TrackViewState()
  451. {
  452. if (_viewState != null)
  453. _viewState.TrackViewState ();
  454. _trackViewState = true;
  455. }
  456. public virtual void Dispose()
  457. {
  458. if (_events != null)
  459. {
  460. EventHandler eh = (EventHandler) _events [DisposedEvent];
  461. if (eh != null)
  462. eh(this, EventArgs.Empty);
  463. }
  464. }
  465. public bool HasChildren
  466. {
  467. get { return (_controls != null && _controls.Count > 0); }
  468. }
  469. public event EventHandler DataBinding //DIT
  470. {
  471. add
  472. {
  473. Events.AddHandler(DataBindingEvent, value);
  474. }
  475. remove
  476. {
  477. Events.RemoveHandler(DataBindingEvent, value);
  478. }
  479. }
  480. public event EventHandler Disposed //DIT
  481. {
  482. add
  483. {
  484. Events.AddHandler(DisposedEvent, value);
  485. }
  486. remove
  487. {
  488. Events.RemoveHandler(DisposedEvent, value);
  489. }
  490. }
  491. public event EventHandler Init //DIT
  492. {
  493. add
  494. {
  495. Events.AddHandler(InitEvent, value);
  496. }
  497. remove
  498. {
  499. Events.RemoveHandler(InitEvent, value);
  500. }
  501. }
  502. public event EventHandler Load //DIT
  503. {
  504. add
  505. {
  506. Events.AddHandler(LoadEvent, value);
  507. }
  508. remove
  509. {
  510. Events.RemoveHandler(LoadEvent, value);
  511. }
  512. }
  513. public event EventHandler PreRender //DIT
  514. {
  515. add
  516. {
  517. Events.AddHandler(PreRenderEvent, value);
  518. }
  519. remove
  520. {
  521. Events.RemoveHandler(PreRenderEvent, value);
  522. }
  523. }
  524. public event EventHandler Unload //DIT
  525. {
  526. add
  527. {
  528. Events.AddHandler(UnloadEvent, value);
  529. }
  530. remove
  531. {
  532. Events.RemoveHandler(UnloadEvent, value);
  533. }
  534. }
  535. public virtual void DataBind() //DIT
  536. {
  537. OnDataBinding(EventArgs.Empty);
  538. if (_controls != null)
  539. foreach (Control c in _controls)
  540. c.DataBind();
  541. }
  542. public virtual Control FindControl(string id) //DIT
  543. {
  544. return FindControl(id, 0);
  545. }
  546. public virtual bool HasControls() //DIT
  547. {
  548. if (_controls != null && _controls.Count >0) return true;
  549. return false;
  550. }
  551. public void RenderControl(HtmlTextWriter writer)
  552. {
  553. if (_visible)
  554. {
  555. //TODO: Something about tracing here.
  556. Render(writer);
  557. }
  558. }
  559. [MonoTODO]
  560. public string ResolveUrl(string relativeUrl)
  561. {
  562. return relativeUrl;
  563. }
  564. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  565. {
  566. _renderMethodDelegate = renderMethod;
  567. }
  568. protected void LoadRecursive()
  569. {
  570. OnLoad(EventArgs.Empty);
  571. if (_controls != null) foreach (Control c in _controls) c.LoadRecursive();
  572. }
  573. protected void UnloadRecursive(Boolean dispose)
  574. {
  575. OnUnload(EventArgs.Empty);
  576. if (_controls != null) foreach (Control c in _controls) c.UnloadRecursive(dispose);
  577. if (dispose) Dispose();
  578. }
  579. protected void PreRenderRecursiveInternal()
  580. {
  581. OnPreRender(EventArgs.Empty);
  582. if (_controls != null) foreach (Control c in _controls) c.PreRenderRecursiveInternal();
  583. }
  584. protected void InitRecursive(Control namingContainer)
  585. {
  586. if (_controls != null) foreach (Control c in _controls) c.InitRecursive(namingContainer);
  587. OnInit(EventArgs.Empty);
  588. TrackViewState ();
  589. }
  590. internal object SaveViewStateRecursive()
  591. {
  592. if (!EnableViewState)
  593. return null;
  594. ArrayList controlList = null;
  595. ArrayList controlStates = null;
  596. foreach (Control ctrl in Controls){
  597. if (controlList == null) {
  598. controlList = new ArrayList ();
  599. controlStates = new ArrayList ();
  600. }
  601. controlList.Add (ctrl);
  602. controlStates.Add (ctrl.SaveViewStateRecursive ());
  603. }
  604. return new Triplet (SaveViewState (), controlList, controlStates);
  605. }
  606. internal void LoadViewStateRecursive (object savedState)
  607. {
  608. if (!EnableViewState || !Visible || savedState == null)
  609. return;
  610. Triplet savedInfo = (Triplet) savedState;
  611. LoadViewState (savedInfo.First);
  612. ArrayList controlList = savedInfo.Second as ArrayList;
  613. if (controlList == null)
  614. return;
  615. ArrayList controlStates = savedInfo.Third as ArrayList;
  616. int nControls = controlList.Count;
  617. for (int i = 0; i < nControls; i++) {
  618. if (controlStates != null)
  619. Controls [i].LoadViewStateRecursive (controlStates [i]);
  620. }
  621. }
  622. void IParserAccessor.AddParsedSubObject(object obj)
  623. {
  624. AddParsedSubObject(obj);
  625. }
  626. DataBindingCollection IDataBindingsAccessor.DataBindings
  627. {
  628. get
  629. {
  630. if(dataBindings == null)
  631. dataBindings = new DataBindingCollection();
  632. return dataBindings;
  633. }
  634. }
  635. bool IDataBindingsAccessor.HasDataBindings
  636. {
  637. get
  638. {
  639. return (dataBindings!=null && dataBindings.Count>0);
  640. }
  641. }
  642. internal bool AutoID
  643. {
  644. get { return autoID; }
  645. set { autoID = value; }
  646. }
  647. internal void PreventAutoID()
  648. {
  649. AutoID = false;
  650. }
  651. protected internal virtual void RemovedControl (Control control)
  652. {
  653. control.UnloadRecursive (false);
  654. control._parent = null;
  655. control._page = null;
  656. control._namingContainer = null;
  657. }
  658. //TODO: I think there are some needed Interface implementations to do here.
  659. //TODO: Find api for INamingContainer.
  660. }
  661. }