Control.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  1. //
  2. // System.Web.UI.Control.cs
  3. //
  4. // Authors:
  5. // Bob Smith <[email protected]>
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) Bob Smith
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. //
  12. /*
  13. * Maintainer: [email protected], [email protected]
  14. * (C) Bob Smith, Gaurav Vaish
  15. */
  16. //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.
  17. //even more notes: view state info in trackviewstate method description. read later.
  18. //Ok, enough notes: what the heck is different between enable view state, and track view state.
  19. //Well, maybe not. How does the ViewState know when to track changes? Does it look at the property
  20. //on the owning control, or does it have a method/property of its own that gets called?
  21. // I think this last question is solved in the Interface for it. Look into this.
  22. //cycle:
  23. //init is called when control is first created.
  24. //load view state ic called right after init to populate the view state.
  25. //loadpostdata is called if ipostbackdatahandler is implemented.
  26. //load is called when control is loaded into a page
  27. //raisepostdatachangedevent if ipostbackdatahandler is implemented.
  28. //raisepostbackevent if ipostbackeventhandler is implemented.
  29. //prerender is called when the server is about to render its page object
  30. //SaveViewState is called.
  31. //Unload then dispose it apears. :)
  32. //Naming Container MUST have some methods. What are they? No clue. Help? (updated: the doc says that it's just a marker interface)
  33. //read this later. http://gotdotnet.com/quickstart/aspplus/
  34. //This to: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpconattributesdesign-timesupport.asp
  35. //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguidnf/html/cpcontracefunctionality.asp
  36. // Isnt life grand? :)
  37. // See the undocumented methods? Gota love um. ;)
  38. // ASP.test4_aspx.Page_Load(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  39. // System.Web.UI.Control.OnLoad(EventArgs e) +67
  40. // System.Web.UI.Control.LoadRecursive() +73
  41. // System.Web.UI.Page.ProcessRequestMain() +394
  42. // ASP.test4_aspx.Page_Unload(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  43. // System.EventHandler.Invoke(Object sender, EventArgs e) +0
  44. // System.Web.UI.Control.OnUnload(EventArgs e) +67
  45. // System.Web.UI.Control.UnloadRecursive(Boolean dispose) +78
  46. // System.Web.UI.Page.ProcessRequest() +194
  47. // System.Web.UI.Page.ProcessRequest(HttpContext context) +18
  48. // System.Web.CallHandlerExecutionStep.Execute() +179
  49. // System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +87
  50. // ASP.test4_aspx.Page_Unload(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  51. // System.Web.UI.Control.OnUnload(EventArgs e) +67
  52. // System.Web.UI.Control.UnloadRecursive(Boolean dispose) +78
  53. // System.Web.UI.Page.ProcessRequest()
  54. // ASP.test4_aspx.Page_Kill(Object Sender, EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  55. // System.Web.UI.Control.OnPreRender(EventArgs e) +67
  56. // System.Web.UI.Control.PreRenderRecursiveInternal() +61
  57. // System.Web.UI.Page.ProcessRequestMain() +753
  58. // ASP.test4_aspx.OnInit(EventArgs e) in \\genfs2\www24\bobsmith11\test4.aspx:6
  59. // System.Web.UI.Control.InitRecursive(Control namingContainer) +202
  60. // System.Web.UI.Page.ProcessRequestMain() +120
  61. // ASP.test4_aspx.SaveViewState() in \\genfs2\www24\bobsmith11\test4.aspx:12
  62. // System.Web.UI.Control.SaveViewStateRecursive() +51
  63. // System.Web.UI.Page.SavePageViewState() +174
  64. // System.Web.UI.Page.ProcessRequestMain() +861
  65. // ASP.test_aspx.LoadViewState(Object t) +28
  66. // System.Web.UI.Control.LoadViewStateRecursive(Object savedState) +125
  67. // System.Web.UI.Page.LoadPageViewState() +182
  68. // System.Web.UI.Page.ProcessRequestMain() +256
  69. using System;
  70. using System.Collections;
  71. using System.ComponentModel;
  72. using System.ComponentModel.Design;
  73. using System.ComponentModel.Design.Serialization;
  74. using System.Web;
  75. using System.Web.Util;
  76. namespace System.Web.UI
  77. {
  78. [DefaultProperty ("ID"), DesignerCategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require)]
  79. [ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, " + Consts.AssemblySystem_Design)]
  80. [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  81. [DesignerSerializer ("Microsoft.VSDesigner.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  82. public class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
  83. {
  84. private static readonly object DataBindingEvent = new object();
  85. private static readonly object DisposedEvent = new object();
  86. private static readonly object InitEvent = new object();
  87. private static readonly object LoadEvent = new object();
  88. private static readonly object PreRenderEvent = new object();
  89. private static readonly object UnloadEvent = new object();
  90. private string uniqueID;
  91. private string _userId = null;
  92. private string _cachedClientId = null;
  93. private ControlCollection _controls = null;
  94. private bool _enableViewState = true;
  95. private IDictionary _childViewStates = null; //TODO: Not sure datatype. Placeholder guess.
  96. private bool _isNamingContainer = false;
  97. private Control _namingContainer = null;
  98. private Page _page = null;
  99. private Control _parent = null;
  100. private ISite _site = null;
  101. private bool _visible = true;
  102. private HttpContext _context = null;
  103. private bool _childControlsCreated = false;
  104. private StateBag _viewState = null;
  105. private bool _trackViewState = false;
  106. private EventHandlerList _events = new EventHandlerList();
  107. private RenderMethod _renderMethodDelegate = null;
  108. private bool autoID = true;
  109. private bool creatingControls = false;
  110. private bool bindingContainer = true;
  111. private bool autoEventWireup = true;
  112. bool inited = false;
  113. bool loaded = false;
  114. bool prerendered = false;
  115. int defaultNumberID = 0;
  116. private DataBindingCollection dataBindings = null;
  117. public Control()
  118. {
  119. if (this is INamingContainer) _isNamingContainer = true;
  120. }
  121. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  122. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  123. public Control BindingContainer
  124. {
  125. get {
  126. Control container = NamingContainer;
  127. if (!container.bindingContainer)
  128. container = container.BindingContainer;
  129. return container;
  130. }
  131. }
  132. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  133. [Browsable (false)]
  134. [WebSysDescription ("An Identification of the control that is rendered.")]
  135. public virtual string ClientID {
  136. get {
  137. string client = UniqueID;
  138. if (client != null)
  139. client = client.Replace (':', '_');
  140. return client;
  141. }
  142. }
  143. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  144. [Browsable (false)]
  145. [WebSysDescription ("The child controls of this control.")]
  146. public virtual ControlCollection Controls //DIT
  147. {
  148. get
  149. {
  150. if (_controls == null) _controls = CreateControlCollection();
  151. return _controls;
  152. }
  153. }
  154. [DefaultValue (true), WebCategory ("FIXME")]
  155. [WebSysDescription ("An Identification of the control that is rendered.")]
  156. public virtual bool EnableViewState //DIT
  157. {
  158. get
  159. {
  160. return _enableViewState;
  161. }
  162. set
  163. {
  164. _enableViewState = value;
  165. }
  166. }
  167. [MergableProperty (false), ParenthesizePropertyName (true)]
  168. [WebSysDescription ("The name of the control that is rendered.")]
  169. public virtual string ID {
  170. get {
  171. return _userId;
  172. }
  173. set {
  174. if (value == "")
  175. value = null;
  176. _userId = value;
  177. NullifyUniqueID ();
  178. }
  179. }
  180. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  181. [Browsable (false)]
  182. [WebSysDescription ("The container that this control is part of. The control's name has to be unique within the container.")]
  183. public virtual Control NamingContainer //DIT
  184. {
  185. get
  186. {
  187. if (_namingContainer == null && _parent != null)
  188. {
  189. if (_parent._isNamingContainer == false)
  190. _namingContainer = _parent.NamingContainer;
  191. else
  192. _namingContainer = _parent;
  193. }
  194. return _namingContainer;
  195. }
  196. }
  197. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  198. [Browsable (false)]
  199. [WebSysDescription ("The webpage that this control resides on.")]
  200. public virtual Page Page //DIT
  201. {
  202. get
  203. {
  204. if (_page == null && _parent != null) _page = _parent.Page;
  205. return _page;
  206. }
  207. set
  208. {
  209. _page = value;
  210. }
  211. }
  212. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  213. [Browsable (false)]
  214. [WebSysDescription ("The parent control of this control.")]
  215. public virtual Control Parent //DIT
  216. {
  217. get
  218. {
  219. return _parent;
  220. }
  221. }
  222. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  223. [EditorBrowsable (EditorBrowsableState.Advanced), Browsable (false)]
  224. [WebSysDescription ("The site this control is part of.")]
  225. public ISite Site //DIT
  226. {
  227. get
  228. {
  229. return _site;
  230. }
  231. set
  232. {
  233. _site = value;
  234. }
  235. }
  236. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  237. [Browsable (false)]
  238. [WebSysDescription ("A virtual directory containing the parent of the control.")]
  239. public virtual string TemplateSourceDirectory {
  240. get { return (_parent == null) ? String.Empty : _parent.TemplateSourceDirectory; }
  241. }
  242. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  243. [Browsable (false)]
  244. [WebSysDescription ("The unique ID of the control.")]
  245. public virtual string UniqueID {
  246. get {
  247. if (uniqueID != null)
  248. return uniqueID;
  249. if (_namingContainer == null) {
  250. return _userId;
  251. }
  252. if (_userId == null)
  253. _userId = _namingContainer.GetDefaultName ();
  254. string prefix = _namingContainer.UniqueID;
  255. if (_namingContainer == _page || prefix == null) {
  256. uniqueID = _userId;
  257. return uniqueID;
  258. }
  259. uniqueID = prefix + ":" + _userId;
  260. return uniqueID;
  261. }
  262. }
  263. [DefaultValue (true), Bindable (true), WebCategory ("FIXME")]
  264. [WebSysDescription ("Visiblity state of the control.")]
  265. public virtual bool Visible
  266. {
  267. get
  268. {
  269. if (_visible == false)
  270. return false;
  271. if (_parent != null)
  272. return _parent.Visible;
  273. return true;
  274. }
  275. set
  276. {
  277. _visible = value;
  278. }
  279. }
  280. protected bool ChildControlsCreated //DIT
  281. {
  282. get
  283. {
  284. return _childControlsCreated;
  285. }
  286. set
  287. {
  288. if (value == false && _childControlsCreated == true)
  289. _controls.Clear();
  290. _childControlsCreated = value;
  291. }
  292. }
  293. protected virtual HttpContext Context //DIT
  294. {
  295. get
  296. {
  297. HttpContext context;
  298. if (_context != null)
  299. return _context;
  300. if (_parent == null)
  301. return HttpContext.Current;
  302. context = _parent.Context;
  303. if (context != null)
  304. return context;
  305. return HttpContext.Current;
  306. }
  307. }
  308. protected EventHandlerList Events //DIT
  309. {
  310. get
  311. {
  312. if (_events == null)
  313. {
  314. _events = new EventHandlerList();
  315. }
  316. return _events;
  317. }
  318. }
  319. protected bool HasChildViewState //DIT
  320. {
  321. get
  322. {
  323. if (_childViewStates == null) return false;
  324. return true;
  325. }
  326. }
  327. protected bool IsTrackingViewState //DIT
  328. {
  329. get
  330. {
  331. return _trackViewState;
  332. }
  333. }
  334. protected virtual StateBag ViewState
  335. {
  336. get
  337. {
  338. if(_viewState == null)
  339. _viewState = new StateBag (ViewStateIgnoresCase);
  340. if (IsTrackingViewState)
  341. _viewState.TrackViewState ();
  342. return _viewState;
  343. }
  344. }
  345. protected virtual bool ViewStateIgnoresCase
  346. {
  347. get {
  348. return false;
  349. }
  350. }
  351. internal bool AutoEventWireup {
  352. get { return autoEventWireup; }
  353. set { autoEventWireup = value; }
  354. }
  355. internal void SetBindingContainer (bool isBC)
  356. {
  357. bindingContainer = isBC;
  358. }
  359. internal void ResetChildNames ()
  360. {
  361. defaultNumberID = 0;
  362. }
  363. string GetDefaultName ()
  364. {
  365. return "_ctrl" + defaultNumberID++;
  366. }
  367. void NullifyUniqueID ()
  368. {
  369. uniqueID = null;
  370. if (_controls == null)
  371. return;
  372. foreach (Control c in _controls)
  373. c.NullifyUniqueID ();
  374. }
  375. protected internal virtual void AddedControl (Control control, int index)
  376. {
  377. /* Ensure the control don't have more than 1 parent */
  378. if (control._parent != null)
  379. control._parent.Controls.Remove (control);
  380. control._parent = this;
  381. control._page = _page;
  382. Control nc = _isNamingContainer ? this : NamingContainer;
  383. if (nc != null) {
  384. control._namingContainer = nc;
  385. if (control.AutoID == true && control._userId == null)
  386. control._userId = nc.GetDefaultName () + "a";
  387. }
  388. if (inited)
  389. control.InitRecursive (nc);
  390. if (loaded)
  391. control.LoadRecursive ();
  392. if (prerendered)
  393. control.PreRenderRecursiveInternal ();
  394. }
  395. protected virtual void AddParsedSubObject(object obj) //DIT
  396. {
  397. WebTrace.PushContext ("Control.AddParsedSubobject ()");
  398. Control c = obj as Control;
  399. WebTrace.WriteLine ("Start: {0} -> {1}", obj, (c != null) ? c.ID : String.Empty);
  400. if (c != null) Controls.Add(c);
  401. WebTrace.WriteLine ("End");
  402. WebTrace.PopContext ();
  403. }
  404. protected void BuildProfileTree(string parentId, bool calcViewState)
  405. {
  406. //TODO
  407. }
  408. protected void ClearChildViewState()
  409. {
  410. //TODO
  411. //Not quite sure about this. an example clears children then calls this, so I think
  412. //view state is local to the current object, not children.
  413. }
  414. protected virtual void CreateChildControls() {} //DIT
  415. protected virtual ControlCollection CreateControlCollection() //DIT
  416. {
  417. return new ControlCollection(this);
  418. }
  419. protected virtual void EnsureChildControls () //DIT
  420. {
  421. if (ChildControlsCreated == false && !creatingControls) {
  422. creatingControls = true;
  423. CreateChildControls();
  424. ChildControlsCreated = true;
  425. creatingControls = false;
  426. }
  427. }
  428. protected bool IsLiteralContent()
  429. {
  430. if (_controls != null)
  431. if (_controls.Count == 1)
  432. if (_controls[0] is LiteralControl)
  433. return true;
  434. return false;
  435. }
  436. public virtual Control FindControl (string id)
  437. {
  438. return FindControl (id, 0);
  439. }
  440. Control LookForControlByName (string id)
  441. {
  442. if (!HasChildren)
  443. return null;
  444. foreach (Control c in _controls) {
  445. if (String.Compare (id, c._userId, true) == 0)
  446. return c;
  447. if (!c._isNamingContainer && c.HasChildren) {
  448. Control child = c.LookForControlByName (id);
  449. if (child != null)
  450. return child;
  451. }
  452. }
  453. return null;
  454. }
  455. protected virtual Control FindControl (string id, int pathOffset)
  456. {
  457. EnsureChildControls ();
  458. if (_controls == null)
  459. return null;
  460. Control namingContainer = null;
  461. if (!_isNamingContainer) {
  462. namingContainer = NamingContainer;
  463. if (namingContainer == null)
  464. return null;
  465. return namingContainer.FindControl (id, pathOffset);
  466. }
  467. int colon = id.IndexOf (':', pathOffset);
  468. if (colon == -1)
  469. return LookForControlByName (id.Substring (pathOffset));
  470. string idfound = id.Substring (pathOffset, colon - pathOffset);
  471. namingContainer = LookForControlByName (idfound);
  472. if (namingContainer == null)
  473. return null;
  474. return namingContainer.FindControl (id, colon + 1);
  475. }
  476. protected virtual void LoadViewState(object savedState)
  477. {
  478. if (savedState != null)
  479. ViewState.LoadViewState (savedState);
  480. }
  481. [MonoTODO("Secure?")]
  482. protected string MapPathSecure(string virtualPath)
  483. {
  484. string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  485. return Context.Request.MapPath (combined);
  486. }
  487. protected virtual bool OnBubbleEvent(object source, EventArgs args) //DIT
  488. {
  489. return false;
  490. }
  491. protected virtual void OnDataBinding(EventArgs e) //DIT
  492. {
  493. if (_events != null)
  494. {
  495. EventHandler eh = (EventHandler)(_events[DataBindingEvent]);
  496. if (eh != null) eh(this, e);
  497. }
  498. }
  499. protected virtual void OnInit(EventArgs e) //DIT
  500. {
  501. if (_events != null)
  502. {
  503. EventHandler eh = (EventHandler)(_events[InitEvent]);
  504. if (eh != null) eh(this, e);
  505. }
  506. }
  507. protected virtual void OnLoad(EventArgs e) //DIT
  508. {
  509. if (_events != null)
  510. {
  511. EventHandler eh = (EventHandler)(_events[LoadEvent]);
  512. if (eh != null) eh(this, e);
  513. }
  514. }
  515. protected virtual void OnPreRender(EventArgs e) //DIT
  516. {
  517. if (_events != null)
  518. {
  519. EventHandler eh = (EventHandler)(_events[PreRenderEvent]);
  520. if (eh != null) eh(this, e);
  521. }
  522. }
  523. protected virtual void OnUnload(EventArgs e) //DIT
  524. {
  525. if (_events != null)
  526. {
  527. EventHandler eh = (EventHandler)(_events[UnloadEvent]);
  528. if (eh != null) eh(this, e);
  529. }
  530. }
  531. protected void RaiseBubbleEvent(object source, EventArgs args)
  532. {
  533. Control c = Parent;
  534. while (c != null) {
  535. if (c.OnBubbleEvent (source, args))
  536. break;
  537. c = c.Parent;
  538. }
  539. }
  540. protected virtual void Render(HtmlTextWriter writer) //DIT
  541. {
  542. RenderChildren(writer);
  543. }
  544. protected virtual void RenderChildren(HtmlTextWriter writer) //DIT
  545. {
  546. if (_renderMethodDelegate != null)
  547. _renderMethodDelegate(writer, this);
  548. else if (_controls != null)
  549. foreach (Control c in _controls)
  550. c.RenderControl(writer);
  551. }
  552. protected virtual object SaveViewState ()
  553. {
  554. if (_viewState == null)
  555. return null;
  556. return _viewState.SaveViewState ();
  557. }
  558. protected virtual void TrackViewState()
  559. {
  560. if (_viewState != null)
  561. _viewState.TrackViewState ();
  562. _trackViewState = true;
  563. }
  564. public virtual void Dispose()
  565. {
  566. if (_events != null)
  567. {
  568. EventHandler eh = (EventHandler) _events [DisposedEvent];
  569. if (eh != null)
  570. eh(this, EventArgs.Empty);
  571. }
  572. }
  573. internal bool HasChildren
  574. {
  575. get { return (_controls != null && _controls.Count > 0); }
  576. }
  577. [WebCategory ("FIXME")]
  578. [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
  579. public event EventHandler DataBinding //DIT
  580. {
  581. add
  582. {
  583. Events.AddHandler(DataBindingEvent, value);
  584. }
  585. remove
  586. {
  587. Events.RemoveHandler(DataBindingEvent, value);
  588. }
  589. }
  590. [WebSysDescription ("Raised when the contol is disposed.")]
  591. public event EventHandler Disposed //DIT
  592. {
  593. add
  594. {
  595. Events.AddHandler(DisposedEvent, value);
  596. }
  597. remove
  598. {
  599. Events.RemoveHandler(DisposedEvent, value);
  600. }
  601. }
  602. [WebSysDescription ("Raised when the page containing the control is initialized.")]
  603. public event EventHandler Init //DIT
  604. {
  605. add
  606. {
  607. Events.AddHandler(InitEvent, value);
  608. }
  609. remove
  610. {
  611. Events.RemoveHandler(InitEvent, value);
  612. }
  613. }
  614. [WebSysDescription ("Raised after the page containing the control has been loaded.")]
  615. public event EventHandler Load //DIT
  616. {
  617. add
  618. {
  619. Events.AddHandler(LoadEvent, value);
  620. }
  621. remove
  622. {
  623. Events.RemoveHandler(LoadEvent, value);
  624. }
  625. }
  626. [WebSysDescription ("Raised before the page containing the control is rendered.")]
  627. public event EventHandler PreRender //DIT
  628. {
  629. add
  630. {
  631. Events.AddHandler(PreRenderEvent, value);
  632. }
  633. remove
  634. {
  635. Events.RemoveHandler(PreRenderEvent, value);
  636. }
  637. }
  638. [WebSysDescription ("Raised when the page containing the control is unloaded.")]
  639. public event EventHandler Unload //DIT
  640. {
  641. add
  642. {
  643. Events.AddHandler(UnloadEvent, value);
  644. }
  645. remove
  646. {
  647. Events.RemoveHandler(UnloadEvent, value);
  648. }
  649. }
  650. public virtual void DataBind() //DIT
  651. {
  652. OnDataBinding(EventArgs.Empty);
  653. if (_controls != null)
  654. foreach (Control c in _controls)
  655. c.DataBind();
  656. }
  657. public virtual bool HasControls() //DIT
  658. {
  659. if (_controls != null && _controls.Count >0) return true;
  660. return false;
  661. }
  662. public void RenderControl(HtmlTextWriter writer)
  663. {
  664. if (_visible)
  665. {
  666. //TODO: Something about tracing here.
  667. Render(writer);
  668. }
  669. }
  670. public string ResolveUrl(string relativeUrl)
  671. {
  672. if (relativeUrl == null)
  673. throw new ArgumentNullException ("relativeUrl");
  674. if (relativeUrl == "")
  675. return "";
  676. string ts = TemplateSourceDirectory;
  677. if (UrlUtils.IsRelativeUrl (relativeUrl) == false || ts == "")
  678. return relativeUrl;
  679. HttpResponse resp = Context.Response;
  680. return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
  681. }
  682. [EditorBrowsable (EditorBrowsableState.Advanced)]
  683. public void SetRenderMethodDelegate(RenderMethod renderMethod) //DIT
  684. {
  685. WebTrace.PushContext ("Control.AddParsedSubobject ()");
  686. WebTrace.WriteLine ("Start");
  687. _renderMethodDelegate = renderMethod;
  688. WebTrace.WriteLine ("End");
  689. WebTrace.PopContext ();
  690. }
  691. internal void LoadRecursive()
  692. {
  693. OnLoad (EventArgs.Empty);
  694. if (_controls != null) {
  695. foreach (Control c in _controls)
  696. c.LoadRecursive ();
  697. }
  698. loaded = true;
  699. }
  700. internal void UnloadRecursive(Boolean dispose)
  701. {
  702. if (_controls != null) {
  703. foreach (Control c in _controls)
  704. c.UnloadRecursive (dispose);
  705. }
  706. OnUnload (EventArgs.Empty);
  707. if (dispose)
  708. Dispose();
  709. }
  710. internal void PreRenderRecursiveInternal()
  711. {
  712. if (_visible) {
  713. EnsureChildControls ();
  714. OnPreRender (EventArgs.Empty);
  715. if (_controls == null)
  716. return;
  717. foreach (Control c in _controls)
  718. c.PreRenderRecursiveInternal ();
  719. }
  720. prerendered = true;
  721. }
  722. internal void InitRecursive(Control namingContainer)
  723. {
  724. if (_controls != null) {
  725. if (_isNamingContainer)
  726. namingContainer = this;
  727. if (namingContainer != null &&
  728. namingContainer._userId == null &&
  729. namingContainer.autoID)
  730. namingContainer._userId = namingContainer.GetDefaultName () + "b";
  731. foreach (Control c in _controls) {
  732. c._page = Page;
  733. c._namingContainer = namingContainer;
  734. if (namingContainer != null && c._userId == null && c.autoID)
  735. c._userId = namingContainer.GetDefaultName () + "c";
  736. c.InitRecursive (namingContainer);
  737. }
  738. }
  739. OnInit (EventArgs.Empty);
  740. TrackViewState ();
  741. inited = true;
  742. }
  743. internal object SaveViewStateRecursive ()
  744. {
  745. if (!EnableViewState)
  746. return null;
  747. ArrayList controlList = null;
  748. ArrayList controlStates = null;
  749. foreach (Control ctrl in Controls) {
  750. object ctrlState = ctrl.SaveViewStateRecursive ();
  751. if (ctrlState == null || ctrl.ID == null)
  752. continue;
  753. if (controlList == null) {
  754. controlList = new ArrayList ();
  755. controlStates = new ArrayList ();
  756. }
  757. controlList.Add (ctrl.ID);
  758. controlStates.Add (ctrlState);
  759. }
  760. object thisState = SaveViewState ();
  761. if (thisState == null && controlList == null && controlStates == null)
  762. return null;
  763. return new Triplet (thisState, controlList, controlStates);
  764. }
  765. internal void LoadViewStateRecursive (object savedState)
  766. {
  767. if (!EnableViewState || !Visible || savedState == null)
  768. return;
  769. Triplet savedInfo = (Triplet) savedState;
  770. LoadViewState (savedInfo.First);
  771. ArrayList controlList = savedInfo.Second as ArrayList;
  772. if (controlList == null)
  773. return;
  774. ArrayList controlStates = savedInfo.Third as ArrayList;
  775. int nControls = controlList.Count;
  776. for (int i = 0; i < nControls; i++) {
  777. Control c = FindControl ((string) controlList [i]);
  778. if (c != null && controlStates != null)
  779. c.LoadViewStateRecursive (controlStates [i]);
  780. }
  781. }
  782. void IParserAccessor.AddParsedSubObject(object obj)
  783. {
  784. AddParsedSubObject(obj);
  785. }
  786. DataBindingCollection IDataBindingsAccessor.DataBindings
  787. {
  788. get
  789. {
  790. if(dataBindings == null)
  791. dataBindings = new DataBindingCollection();
  792. return dataBindings;
  793. }
  794. }
  795. bool IDataBindingsAccessor.HasDataBindings
  796. {
  797. get
  798. {
  799. return (dataBindings!=null && dataBindings.Count>0);
  800. }
  801. }
  802. internal bool AutoID
  803. {
  804. get { return autoID; }
  805. set {
  806. if (value == false && _isNamingContainer)
  807. return;
  808. autoID = value;
  809. }
  810. }
  811. internal void PreventAutoID()
  812. {
  813. AutoID = false;
  814. }
  815. protected internal virtual void RemovedControl (Control control)
  816. {
  817. control.UnloadRecursive (false);
  818. control._parent = null;
  819. control._page = null;
  820. control._namingContainer = null;
  821. }
  822. //TODO: I think there are some needed Interface implementations to do here.
  823. }
  824. }