Control.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  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. // Sanjay Gupta ([email protected])
  9. //
  10. // (C) Bob Smith
  11. // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  12. // (C) 2004 Novell, Inc. (http://www.novell.com)
  13. //
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System.Collections;
  35. using System.ComponentModel;
  36. using System.ComponentModel.Design;
  37. using System.ComponentModel.Design.Serialization;
  38. using System.Security.Permissions;
  39. using System.Web;
  40. using System.Web.Util;
  41. using System.Globalization;
  42. #if NET_2_0
  43. using System.Web.UI.Adapters;
  44. using System.IO;
  45. #endif
  46. namespace System.Web.UI
  47. {
  48. // CAS
  49. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  50. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  51. // attributes
  52. [DefaultProperty ("ID"), DesignerCategory ("Code"), ToolboxItemFilter ("System.Web.UI", ToolboxItemFilterType.Require)]
  53. [ToolboxItem ("System.Web.UI.Design.WebControlToolboxItem, " + Consts.AssemblySystem_Design)]
  54. [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
  55. #if NET_2_0
  56. [DesignerSerializer ("Microsoft.VisualStudio.Web.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VisualStudio_Web,
  57. "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  58. [Bindable (true)]
  59. [Themeable (false)]
  60. #else
  61. [DesignerSerializer ("Microsoft.VSDesigner.WebForms.ControlCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner,
  62. "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design)]
  63. #endif
  64. public partial class Control : IComponent, IDisposable, IParserAccessor, IDataBindingsAccessor
  65. #if NET_2_0
  66. , IUrlResolutionService, IControlBuilderAccessor, IControlDesignerAccessor, IExpressionsAccessor
  67. #endif
  68. {
  69. static readonly object DataBindingEvent = new object ();
  70. static readonly object DisposedEvent = new object ();
  71. static readonly object InitEvent = new object ();
  72. static readonly object LoadEvent = new object ();
  73. static readonly object PreRenderEvent = new object ();
  74. static readonly object UnloadEvent = new object ();
  75. static string [] defaultNameArray;
  76. /* */
  77. int event_mask;
  78. const int databinding_mask = 1;
  79. const int disposed_mask = 1 << 1;
  80. const int init_mask = 1 << 2;
  81. const int load_mask = 1 << 3;
  82. const int prerender_mask = 1 << 4;
  83. const int unload_mask = 1 << 5;
  84. /* */
  85. string uniqueID;
  86. string _userId;
  87. ControlCollection _controls;
  88. Control _namingContainer;
  89. Page _page;
  90. Control _parent;
  91. ISite _site;
  92. HttpContext _context;
  93. StateBag _viewState;
  94. EventHandlerList _events;
  95. RenderMethod _renderMethodDelegate;
  96. Hashtable _controlsCache;
  97. int defaultNumberID;
  98. DataBindingCollection dataBindings;
  99. Hashtable pendingVS; // may hold unused viewstate data from child controls
  100. #if NET_2_0
  101. TemplateControl _templateControl;
  102. bool _isChildControlStateCleared;
  103. #endif
  104. string _templateSourceDirectory;
  105. /*************/
  106. int stateMask;
  107. const int ENABLE_VIEWSTATE = 1;
  108. const int VISIBLE = 1 << 1;
  109. const int AUTOID = 1 << 2;
  110. const int CREATING_CONTROLS = 1 << 3;
  111. const int BINDING_CONTAINER = 1 << 4;
  112. const int AUTO_EVENT_WIREUP = 1 << 5;
  113. const int IS_NAMING_CONTAINER = 1 << 6;
  114. const int VISIBLE_CHANGED = 1 << 7;
  115. const int TRACK_VIEWSTATE = 1 << 8;
  116. const int CHILD_CONTROLS_CREATED = 1 << 9;
  117. const int ID_SET = 1 << 10;
  118. const int INITED = 1 << 11;
  119. const int INITING = 1 << 12;
  120. const int VIEWSTATE_LOADED = 1 << 13;
  121. const int LOADED = 1 << 14;
  122. const int PRERENDERED = 1 << 15;
  123. #if NET_2_0
  124. const int ENABLE_THEMING = 1 << 16;
  125. #endif
  126. const int AUTOID_SET = 1 << 17;
  127. /*************/
  128. static Control ()
  129. {
  130. defaultNameArray = new string [100];
  131. for (int i = 0; i < 100; i++)
  132. #if NET_2_0
  133. defaultNameArray [i] = String.Concat ("ctl", i.ToString ("D2"));
  134. #else
  135. defaultNameArray [i] = "_ctl" + i;
  136. #endif
  137. }
  138. public Control ()
  139. {
  140. stateMask = ENABLE_VIEWSTATE | VISIBLE | AUTOID | BINDING_CONTAINER | AUTO_EVENT_WIREUP;
  141. if (this is INamingContainer)
  142. stateMask |= IS_NAMING_CONTAINER;
  143. }
  144. #if NET_2_0
  145. [MonoTODO ("Not implemented, always returns null")]
  146. protected ControlAdapter Adapter {
  147. get {
  148. // for the time being, fool the
  149. // Control machinery into thinking we
  150. // don't have an Adapter. This will
  151. // allow us to write all the rest of
  152. // the Adapter handling code without
  153. // having to worry about *having*
  154. // adapters.
  155. return null;
  156. }
  157. }
  158. string _appRelativeTemplateSourceDirectory = null;
  159. [EditorBrowsable (EditorBrowsableState.Advanced)]
  160. [Browsable (false)]
  161. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  162. public string AppRelativeTemplateSourceDirectory {
  163. get {
  164. if (_appRelativeTemplateSourceDirectory != null)
  165. return _appRelativeTemplateSourceDirectory;
  166. string tempSrcDir = null;
  167. TemplateControl templateControl = TemplateControl;
  168. if (templateControl != null)
  169. if (!string.IsNullOrEmpty (templateControl.AppRelativeVirtualPath))
  170. tempSrcDir = VirtualPathUtility.GetDirectory (templateControl.AppRelativeVirtualPath, false);
  171. _appRelativeTemplateSourceDirectory = (tempSrcDir != null) ? tempSrcDir : "~/";
  172. return _appRelativeTemplateSourceDirectory;
  173. }
  174. [EditorBrowsable (EditorBrowsableState.Never)]
  175. set {
  176. _appRelativeTemplateSourceDirectory = value;
  177. _templateSourceDirectory = null;
  178. }
  179. }
  180. #endif
  181. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  182. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  183. public Control BindingContainer {
  184. get {
  185. Control container = NamingContainer;
  186. if (container != null && (container.stateMask & BINDING_CONTAINER) == 0)
  187. container = container.BindingContainer;
  188. return container;
  189. }
  190. }
  191. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  192. [Browsable (false)]
  193. [WebSysDescription ("An Identification of the control that is rendered.")]
  194. public virtual string ClientID {
  195. get {
  196. string client = UniqueID;
  197. if (client != null)
  198. #if NET_2_0
  199. client = UniqueID2ClientID (client);
  200. #else
  201. client = client.Replace (':', ClientIDSeparator);
  202. #endif
  203. stateMask |= ID_SET;
  204. return client;
  205. }
  206. }
  207. #if NET_2_0
  208. internal string UniqueID2ClientID (string uniqueId)
  209. {
  210. return uniqueId.Replace (IdSeparator, ClientIDSeparator);
  211. }
  212. protected char ClientIDSeparator
  213. #else
  214. char ClientIDSeparator
  215. #endif
  216. {
  217. get { return '_'; }
  218. }
  219. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  220. [Browsable (false)]
  221. [WebSysDescription ("The child controls of this control.")]
  222. public virtual ControlCollection Controls { //DIT
  223. get {
  224. if (_controls == null)
  225. _controls = CreateControlCollection ();
  226. return _controls;
  227. }
  228. }
  229. #if NET_2_0
  230. [MonoTODO ("revisit once we have a real design strategy")]
  231. protected internal bool DesignMode {
  232. get { return false; }
  233. }
  234. #endif
  235. [DefaultValue (true), WebCategory ("Behavior")]
  236. [WebSysDescription ("An Identification of the control that is rendered.")]
  237. #if NET_2_0
  238. [Themeable (false)]
  239. #endif
  240. public virtual bool EnableViewState {
  241. get { return ((stateMask & ENABLE_VIEWSTATE) != 0); }
  242. set { SetMask (ENABLE_VIEWSTATE, value); }
  243. }
  244. [MergableProperty (false), ParenthesizePropertyName (true)]
  245. [WebSysDescription ("The name of the control that is rendered.")]
  246. #if NET_2_0
  247. [Filterable (false), Themeable (false)]
  248. #endif
  249. public virtual string ID {
  250. get { return (((stateMask & ID_SET) != 0) ? _userId : null); }
  251. set {
  252. if (value == "")
  253. value = null;
  254. stateMask |= ID_SET;
  255. _userId = value;
  256. NullifyUniqueID ();
  257. }
  258. }
  259. #if NET_2_0
  260. protected char IdSeparator {
  261. get { return '$'; }
  262. }
  263. protected internal bool IsChildControlStateCleared {
  264. get { return _isChildControlStateCleared; }
  265. }
  266. protected internal bool IsViewStateEnabled {
  267. get {
  268. for (Control control = this; control != null; control = control.Parent)
  269. if (!control.EnableViewState)
  270. return false;
  271. return true;
  272. }
  273. }
  274. protected bool LoadViewStateByID {
  275. get { return false; }
  276. }
  277. #endif
  278. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  279. [Browsable (false)]
  280. [WebSysDescription ("The container that this control is part of. The control's name has to be unique within the container.")]
  281. public virtual Control NamingContainer {
  282. get {
  283. if (_namingContainer == null && _parent != null) {
  284. if ((_parent.stateMask & IS_NAMING_CONTAINER) == 0)
  285. _namingContainer = _parent.NamingContainer;
  286. else
  287. _namingContainer = _parent;
  288. }
  289. return _namingContainer;
  290. }
  291. }
  292. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  293. [Browsable (false)]
  294. [WebSysDescription ("The webpage that this control resides on.")]
  295. #if NET_2_0
  296. [Bindable (false)]
  297. #endif
  298. public virtual Page Page { //DIT
  299. get {
  300. if (_page == null){
  301. if (NamingContainer != null)
  302. _page = NamingContainer.Page;
  303. else if (Parent != null)
  304. _page = Parent.Page;
  305. }
  306. return _page;
  307. }
  308. set { _page = value; }
  309. }
  310. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  311. [Browsable (false)]
  312. [WebSysDescription ("The parent control of this control.")]
  313. public virtual Control Parent { //DIT
  314. get { return _parent; }
  315. }
  316. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  317. [EditorBrowsable (EditorBrowsableState.Advanced), Browsable (false)]
  318. [WebSysDescription ("The site this control is part of.")]
  319. public ISite Site { //DIT
  320. get { return _site; }
  321. set { _site = value; }
  322. }
  323. #if NET_2_0
  324. [Browsable (false)]
  325. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  326. public TemplateControl TemplateControl {
  327. get { return TemplateControlInternal; }
  328. [EditorBrowsable (EditorBrowsableState.Never)]
  329. set { _templateControl = value; }
  330. }
  331. internal virtual TemplateControl TemplateControlInternal {
  332. get {
  333. if (_templateControl != null)
  334. return _templateControl;
  335. if (_parent != null)
  336. return _parent.TemplateControl;
  337. return null;
  338. }
  339. }
  340. #endif
  341. #if !TARGET_J2EE
  342. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  343. [Browsable (false)]
  344. [WebSysDescription ("A virtual directory containing the parent of the control.")]
  345. public virtual string TemplateSourceDirectory {
  346. get {
  347. #if NET_2_0
  348. if (_templateSourceDirectory == null) {
  349. TemplateControl tc = TemplateControl;
  350. if (tc == null) {
  351. HttpContext ctx = Context;
  352. if (ctx != null)
  353. _templateSourceDirectory = VirtualPathUtility.GetDirectory (ctx.Request.CurrentExecutionFilePath);
  354. } else if (tc != this)
  355. _templateSourceDirectory = tc.TemplateSourceDirectory;
  356. if (_templateSourceDirectory == null && this is TemplateControl) {
  357. string path = ((TemplateControl) this).AppRelativeVirtualPath;
  358. if (path != null) {
  359. // Pretend our application virtual root is "/" even if it isn't - we just
  360. // want to get an absolute url out of relative one, without the real
  361. // application root prepended to it.
  362. string ret = VirtualPathUtility.GetDirectory (VirtualPathUtility.ToAbsolute (path, "/"));
  363. int len = ret.Length;
  364. if (len <= 1)
  365. return ret;
  366. if (ret [--len] == '/')
  367. _templateSourceDirectory = ret.Substring (0, len);
  368. } else
  369. _templateSourceDirectory = String.Empty;
  370. }
  371. if (_templateSourceDirectory == null)
  372. _templateSourceDirectory = String.Empty;
  373. }
  374. return _templateSourceDirectory;
  375. #else
  376. if (_templateSourceDirectory == null) {
  377. HttpContext ctx = HttpContext.Current;
  378. HttpRequest req = ctx != null ? ctx.Request : null;
  379. _templateSourceDirectory = (_parent == null)
  380. ? req != null ? VirtualPathUtility.RemoveTrailingSlash (
  381. VirtualPathUtility.GetDirectory (
  382. HttpContext.Current.Request.CurrentExecutionFilePath)) : String.Empty
  383. : _parent.TemplateSourceDirectory;
  384. }
  385. return _templateSourceDirectory;
  386. #endif
  387. }
  388. }
  389. #endif
  390. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  391. [Browsable (false)]
  392. [WebSysDescription ("The unique ID of the control.")]
  393. public virtual string UniqueID {
  394. get {
  395. if (uniqueID != null)
  396. return uniqueID;
  397. if (NamingContainer == null)
  398. return _userId;
  399. EnsureIDInternal ();
  400. string prefix = NamingContainer.UniqueID;
  401. if (NamingContainer == Page || prefix == null) {
  402. uniqueID = _userId;
  403. #if TARGET_J2EE
  404. if (getFacesContext () != null)
  405. uniqueID = getFacesContext ().getExternalContext ().encodeNamespace (uniqueID);
  406. #endif
  407. return uniqueID;
  408. }
  409. #if NET_2_0
  410. uniqueID = prefix + IdSeparator + _userId;
  411. #else
  412. uniqueID = prefix + ":" + _userId;
  413. #endif
  414. return uniqueID;
  415. }
  416. }
  417. void SetMask (int m, bool val) {
  418. if (val)
  419. stateMask |= m;
  420. else
  421. stateMask &= ~m;
  422. }
  423. [DefaultValue (true), Bindable (true), WebCategory ("Behavior")]
  424. [WebSysDescription ("Visiblity state of the control.")]
  425. public virtual bool Visible {
  426. get {
  427. if ((stateMask & VISIBLE) == 0)
  428. return false;
  429. if (_parent != null)
  430. return _parent.Visible;
  431. return true;
  432. }
  433. set {
  434. if ((value && (stateMask & VISIBLE) == 0) ||
  435. (!value && (stateMask & VISIBLE) != 0)) {
  436. if (IsTrackingViewState)
  437. stateMask |= VISIBLE_CHANGED;
  438. }
  439. SetMask (VISIBLE, value);
  440. }
  441. }
  442. protected bool ChildControlsCreated {
  443. get { return ((stateMask & CHILD_CONTROLS_CREATED) != 0); }
  444. set {
  445. if (value == false && (stateMask & CHILD_CONTROLS_CREATED) != 0) {
  446. if (_controls != null)
  447. _controls.Clear ();
  448. }
  449. SetMask (CHILD_CONTROLS_CREATED, value);
  450. }
  451. }
  452. [Browsable (false)]
  453. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  454. protected virtual HttpContext Context { //DIT
  455. get {
  456. HttpContext context;
  457. if (_context != null)
  458. return _context;
  459. if (_parent == null)
  460. return HttpContext.Current;
  461. context = _parent.Context;
  462. if (context != null)
  463. return context;
  464. return HttpContext.Current;
  465. }
  466. }
  467. protected EventHandlerList Events {
  468. get {
  469. if (_events == null)
  470. _events = new EventHandlerList ();
  471. return _events;
  472. }
  473. }
  474. protected bool HasChildViewState {
  475. get { return (pendingVS != null && pendingVS.Count > 0); }
  476. }
  477. protected bool IsTrackingViewState {
  478. get { return ((stateMask & TRACK_VIEWSTATE) != 0); }
  479. }
  480. [Browsable (false)]
  481. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  482. [WebSysDescription ("ViewState")]
  483. protected virtual StateBag ViewState {
  484. get {
  485. if (_viewState == null)
  486. _viewState = new StateBag (ViewStateIgnoresCase);
  487. if (IsTrackingViewState)
  488. _viewState.TrackViewState ();
  489. return _viewState;
  490. }
  491. }
  492. [Browsable (false)]
  493. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  494. protected virtual bool ViewStateIgnoresCase {
  495. get { return false; }
  496. }
  497. internal bool AutoEventWireup {
  498. get { return (stateMask & AUTO_EVENT_WIREUP) != 0; }
  499. set { SetMask (AUTO_EVENT_WIREUP, value); }
  500. }
  501. internal void SetBindingContainer (bool isBC)
  502. {
  503. SetMask (BINDING_CONTAINER, isBC);
  504. }
  505. internal void ResetChildNames ()
  506. {
  507. defaultNumberID = 0;
  508. }
  509. string GetDefaultName ()
  510. {
  511. string defaultName;
  512. if (defaultNumberID > 99) {
  513. #if NET_2_0
  514. defaultName = "ctl" + defaultNumberID++;
  515. #else
  516. defaultName = "_ctl" + defaultNumberID++;
  517. #endif
  518. } else {
  519. defaultName = defaultNameArray [defaultNumberID++];
  520. }
  521. return defaultName;
  522. }
  523. void NullifyUniqueID ()
  524. {
  525. uniqueID = null;
  526. if (!HasControls ())
  527. return;
  528. for (int i = 0; i < _controls.Count; i++)
  529. _controls [i].NullifyUniqueID ();
  530. }
  531. protected internal virtual void AddedControl (Control control, int index)
  532. {
  533. ResetControlsCache ();
  534. /* Ensure the control don't have more than 1 parent */
  535. if (control._parent != null)
  536. control._parent.Controls.Remove (control);
  537. control._parent = this;
  538. Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
  539. if ((stateMask & (INITING | INITED)) != 0)
  540. control.InitRecursive (nc);
  541. else {
  542. control.SetNamingContainer (nc);
  543. return;
  544. }
  545. if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
  546. if (pendingVS != null) {
  547. object vs = pendingVS [index];
  548. if (vs != null) {
  549. pendingVS.Remove (index);
  550. if (pendingVS.Count == 0)
  551. pendingVS = null;
  552. control.LoadViewStateRecursive (vs);
  553. }
  554. }
  555. }
  556. if ((stateMask & LOADED) != 0)
  557. control.LoadRecursive ();
  558. if ((stateMask & PRERENDERED) != 0)
  559. control.PreRenderRecursiveInternal ();
  560. }
  561. void SetNamingContainer (Control nc)
  562. {
  563. if (nc != null) {
  564. _namingContainer = nc;
  565. if (AutoID)
  566. EnsureIDInternal ();
  567. }
  568. }
  569. protected virtual void AddParsedSubObject (object obj) //DIT
  570. {
  571. Control c = obj as Control;
  572. if (c != null)
  573. Controls.Add (c);
  574. }
  575. #if NET_2_0
  576. [EditorBrowsable (EditorBrowsableState.Advanced)]
  577. public virtual void ApplyStyleSheetSkin (Page page)
  578. {
  579. if (!EnableTheming) /* this enough? */
  580. return;
  581. /* apply the style sheet skin here */
  582. if (page.StyleSheetPageTheme != null) {
  583. ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType (), SkinID);
  584. if (cs != null)
  585. cs.ApplySkin (this);
  586. }
  587. }
  588. #endif
  589. protected void BuildProfileTree (string parentId, bool calcViewState)
  590. {
  591. //TODO
  592. }
  593. #if NET_2_0
  594. protected void ClearChildControlState ()
  595. {
  596. _isChildControlStateCleared = true;
  597. }
  598. protected void ClearChildState ()
  599. {
  600. ClearChildViewState ();
  601. ClearChildControlState ();
  602. }
  603. #endif
  604. protected void ClearChildViewState ()
  605. {
  606. pendingVS = null;
  607. }
  608. #if NET_2_0
  609. protected internal
  610. #else
  611. protected
  612. #endif
  613. virtual void CreateChildControls () //DIT
  614. {
  615. }
  616. protected virtual ControlCollection CreateControlCollection () //DIT
  617. {
  618. return new ControlCollection (this);
  619. }
  620. protected virtual void EnsureChildControls ()
  621. {
  622. if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
  623. stateMask |= CREATING_CONTROLS;
  624. #if NET_2_0
  625. if (Adapter != null)
  626. Adapter.CreateChildControls ();
  627. else
  628. #endif
  629. CreateChildControls ();
  630. ChildControlsCreated = true;
  631. stateMask &= ~CREATING_CONTROLS;
  632. }
  633. }
  634. void EnsureIDInternal ()
  635. {
  636. if (_userId != null)
  637. return;
  638. _userId = NamingContainer.GetDefaultName ();
  639. SetMask (AUTOID_SET, true);
  640. }
  641. #if NET_2_0
  642. protected void EnsureID ()
  643. {
  644. if (NamingContainer == null)
  645. return;
  646. EnsureIDInternal ();
  647. SetMask (ID_SET, true);
  648. }
  649. protected bool HasEvents ()
  650. {
  651. return _events != null;
  652. }
  653. #endif
  654. void ResetControlsCache ()
  655. {
  656. _controlsCache = null;
  657. if ((this.stateMask & IS_NAMING_CONTAINER) == 0 && Parent != null)
  658. Parent.ResetControlsCache ();
  659. }
  660. Hashtable InitControlsCache ()
  661. {
  662. if (_controlsCache != null)
  663. return _controlsCache;
  664. if ((this.stateMask & IS_NAMING_CONTAINER) != 0 || Parent == null)
  665. //LAMESPEC: MS' docs don't mention it, but FindControl is case insensitive.
  666. #if NET_2_0
  667. _controlsCache = new Hashtable (StringComparer.OrdinalIgnoreCase);
  668. #else
  669. _controlsCache = new Hashtable (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
  670. #endif
  671. else
  672. _controlsCache = Parent.InitControlsCache ();
  673. return _controlsCache;
  674. }
  675. void EnsureControlsCache ()
  676. {
  677. if (_controlsCache != null)
  678. return;
  679. InitControlsCache ();
  680. FillControlCache (this);
  681. }
  682. void FillControlCache (Control control)
  683. {
  684. if (!HasControls ())
  685. return;
  686. foreach (Control c in control._controls) {
  687. try {
  688. if (c._userId != null)
  689. _controlsCache.Add (c._userId, c);
  690. } catch (Exception ex) {
  691. throw new HttpException ("Found more than one control with ID '" + c._userId + "'", ex);
  692. }
  693. if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ())
  694. FillControlCache (c);
  695. }
  696. }
  697. protected bool IsLiteralContent ()
  698. {
  699. if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
  700. return true;
  701. return false;
  702. }
  703. [WebSysDescription ("")]
  704. public virtual Control FindControl (string id)
  705. {
  706. return FindControl (id, 0);
  707. }
  708. Control LookForControlByName (string id)
  709. {
  710. EnsureControlsCache ();
  711. return (Control) _controlsCache [id];
  712. }
  713. protected virtual Control FindControl (string id, int pathOffset)
  714. {
  715. EnsureChildControls ();
  716. Control namingContainer = null;
  717. if ((stateMask & IS_NAMING_CONTAINER) == 0) {
  718. namingContainer = NamingContainer;
  719. if (namingContainer == null)
  720. return null;
  721. return namingContainer.FindControl (id, pathOffset);
  722. }
  723. if (!HasControls ())
  724. return null;
  725. #if NET_2_0
  726. int separatorIdx = id.IndexOf (IdSeparator, pathOffset);
  727. #else
  728. int separatorIdx = id.IndexOf (':', pathOffset);
  729. #endif
  730. if (separatorIdx == -1)
  731. return LookForControlByName (id.Substring (pathOffset));
  732. string idfound = id.Substring (pathOffset, separatorIdx - pathOffset);
  733. namingContainer = LookForControlByName (idfound);
  734. if (namingContainer == null)
  735. return null;
  736. return namingContainer.FindControl (id, separatorIdx + 1);
  737. }
  738. protected virtual void LoadViewState (object savedState)
  739. {
  740. if (savedState != null) {
  741. ViewState.LoadViewState (savedState);
  742. object o = ViewState ["Visible"];
  743. if (o != null) {
  744. SetMask (VISIBLE, (bool) o);
  745. stateMask |= VISIBLE_CHANGED;
  746. }
  747. }
  748. }
  749. // [MonoTODO("Secure?")]
  750. protected string MapPathSecure (string virtualPath)
  751. {
  752. string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  753. return Context.Request.MapPath (combined);
  754. }
  755. protected virtual bool OnBubbleEvent (object source, EventArgs args) //DIT
  756. {
  757. #if MONO_TRACE
  758. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  759. string type_name = null;
  760. if (trace != null) {
  761. type_name = GetType ().Name;
  762. trace.Write ("control", String.Concat ("OnBubbleEvent ", _userId, " ", type_name));
  763. }
  764. #endif
  765. return false;
  766. }
  767. protected virtual void OnDataBinding (EventArgs e)
  768. {
  769. if ((event_mask & databinding_mask) != 0) {
  770. EventHandler eh = (EventHandler) (_events [DataBindingEvent]);
  771. if (eh != null) {
  772. #if MONO_TRACE
  773. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  774. string type_name = null;
  775. if (trace != null) {
  776. type_name = GetType ().Name;
  777. trace.Write ("control", String.Concat ("OnDataBinding ", _userId, " ", type_name));
  778. }
  779. #endif
  780. eh (this, e);
  781. }
  782. }
  783. }
  784. #if NET_2_0
  785. protected internal
  786. #else
  787. protected
  788. #endif
  789. virtual void OnInit (EventArgs e)
  790. {
  791. if ((event_mask & init_mask) != 0) {
  792. EventHandler eh = (EventHandler) (_events [InitEvent]);
  793. if (eh != null) {
  794. #if MONO_TRACE
  795. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  796. string type_name = null;
  797. if (trace != null) {
  798. type_name = GetType ().Name;
  799. trace.Write ("control", String.Concat ("OnInit ", _userId, " ", type_name));
  800. }
  801. #endif
  802. eh (this, e);
  803. }
  804. }
  805. }
  806. #if NET_2_0
  807. protected internal
  808. #else
  809. protected
  810. #endif
  811. virtual void OnLoad (EventArgs e)
  812. {
  813. if ((event_mask & load_mask) != 0) {
  814. EventHandler eh = (EventHandler) (_events [LoadEvent]);
  815. if (eh != null) {
  816. #if MONO_TRACE
  817. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  818. string type_name = null;
  819. if (trace != null) {
  820. type_name = GetType ().Name;
  821. trace.Write ("control", String.Concat ("OnLoad ", _userId, " ", type_name));
  822. }
  823. #endif
  824. eh (this, e);
  825. }
  826. }
  827. }
  828. #if NET_2_0
  829. protected internal
  830. #else
  831. protected
  832. #endif
  833. virtual void OnPreRender (EventArgs e)
  834. {
  835. if ((event_mask & prerender_mask) != 0) {
  836. EventHandler eh = (EventHandler) (_events [PreRenderEvent]);
  837. if (eh != null) {
  838. #if MONO_TRACE
  839. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  840. string type_name = null;
  841. if (trace != null) {
  842. type_name = GetType ().Name;
  843. trace.Write ("control", String.Concat ("OnPreRender ", _userId, " ", type_name));
  844. }
  845. #endif
  846. eh (this, e);
  847. }
  848. }
  849. }
  850. #if NET_2_0
  851. protected internal
  852. #else
  853. protected
  854. #endif
  855. virtual void OnUnload (EventArgs e)
  856. {
  857. if ((event_mask & unload_mask) != 0) {
  858. EventHandler eh = (EventHandler) (_events [UnloadEvent]);
  859. if (eh != null) {
  860. #if MONO_TRACE
  861. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  862. string type_name = null;
  863. if (trace != null) {
  864. type_name = GetType ().Name;
  865. trace.Write ("control", String.Concat ("OnUnload ", _userId, " ", type_name));
  866. }
  867. #endif
  868. eh (this, e);
  869. }
  870. }
  871. }
  872. #if NET_2_0
  873. protected internal Stream OpenFile (string path)
  874. {
  875. try {
  876. string filePath = Context.Server.MapPath (path);
  877. return File.OpenRead (filePath);
  878. }
  879. catch (UnauthorizedAccessException) {
  880. throw new HttpException ("Access to the specified file was denied.");
  881. }
  882. }
  883. internal string GetPhysicalFilePath (string virtualPath)
  884. {
  885. Page page = Page;
  886. if (VirtualPathUtility.IsAbsolute (virtualPath))
  887. return page != null ? page.MapPath (virtualPath) : Context.Server.MapPath (virtualPath);
  888. // We need to determine whether one of our parents is a
  889. // master page. If so, we need to map the path
  890. // relatively to the master page and not our containing
  891. // page/control. This is necessary for cases when a
  892. // relative path is used in a control placed in a master
  893. // page and the master page is referenced from a
  894. // location other than its own. In such cases MS.NET looks
  895. // for the file in the directory where the master page
  896. // is.
  897. //
  898. // An example of where it is needed is at
  899. //
  900. // http://quickstarts.asp.net/QuickStartv20/aspnet/samples/masterpages/masterpages_cs/pages/default.aspx
  901. //
  902. MasterPage master = null;
  903. Control ctrl = Parent;
  904. while (ctrl != null) {
  905. if (ctrl is MasterPage) {
  906. master = ctrl as MasterPage;
  907. break;
  908. }
  909. ctrl = ctrl.Parent;
  910. }
  911. string path;
  912. if (master != null)
  913. path = VirtualPathUtility.Combine (master.TemplateSourceDirectory + "/", virtualPath);
  914. else
  915. path = VirtualPathUtility.Combine (TemplateSourceDirectory + "/", virtualPath);
  916. return page != null ? page.MapPath (path) : Context.Server.MapPath (path);
  917. }
  918. #endif
  919. protected void RaiseBubbleEvent (object source, EventArgs args)
  920. {
  921. Control c = Parent;
  922. while (c != null) {
  923. #if MONO_TRACE
  924. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  925. string type_name = null;
  926. if (trace != null) {
  927. type_name = GetType ().Name;
  928. trace.Write ("control", String.Concat ("RaiseBubbleEvent ", _userId, " ", type_name));
  929. }
  930. #endif
  931. if (c.OnBubbleEvent (source, args)) {
  932. #if MONO_TRACE
  933. if (trace != null)
  934. trace.Write ("control", String.Concat ("End RaiseBubbleEvent (false) ", _userId, " ", type_name));
  935. #endif
  936. break;
  937. }
  938. #if MONO_TRACE
  939. if (trace != null)
  940. trace.Write ("control", String.Concat ("End RaiseBubbleEvent (true) ", _userId, " ", type_name));
  941. #endif
  942. c = c.Parent;
  943. }
  944. }
  945. #if NET_2_0
  946. protected internal
  947. #else
  948. protected
  949. #endif
  950. virtual void Render (HtmlTextWriter writer) //DIT
  951. {
  952. RenderChildren (writer);
  953. }
  954. #if NET_2_0
  955. protected internal
  956. #else
  957. protected
  958. #endif
  959. virtual void RenderChildren (HtmlTextWriter writer) //DIT
  960. {
  961. if (_renderMethodDelegate != null) {
  962. _renderMethodDelegate (writer, this);
  963. } else if (_controls != null) {
  964. int len = _controls.Count;
  965. Control c;
  966. for (int i = 0; i < len; i++) {
  967. c = _controls [i];
  968. if (c == null)
  969. continue;
  970. #if NET_2_0
  971. if (c.Adapter != null)
  972. c.RenderControl (writer, c.Adapter);
  973. else
  974. #endif
  975. c.RenderControl (writer);
  976. }
  977. }
  978. }
  979. #if NET_2_0
  980. protected virtual ControlAdapter ResolveAdapter ()
  981. {
  982. throw new NotImplementedException ();
  983. }
  984. #endif
  985. protected virtual object SaveViewState ()
  986. {
  987. if ((stateMask & VISIBLE_CHANGED) != 0) {
  988. ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
  989. } else if (_viewState == null) {
  990. return null;
  991. }
  992. return _viewState.SaveViewState ();
  993. }
  994. protected virtual void TrackViewState ()
  995. {
  996. if (_viewState != null)
  997. _viewState.TrackViewState ();
  998. stateMask |= TRACK_VIEWSTATE;
  999. }
  1000. public virtual void Dispose ()
  1001. {
  1002. if ((event_mask & disposed_mask) != 0) {
  1003. EventHandler eh = (EventHandler) (_events [DisposedEvent]);
  1004. if (eh != null)
  1005. eh (this, EventArgs.Empty);
  1006. }
  1007. }
  1008. [WebCategory ("FIXME")]
  1009. [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
  1010. public event EventHandler DataBinding {
  1011. add {
  1012. event_mask |= databinding_mask;
  1013. Events.AddHandler (DataBindingEvent, value);
  1014. }
  1015. remove { Events.RemoveHandler (DataBindingEvent, value); }
  1016. }
  1017. [WebSysDescription ("Raised when the contol is disposed.")]
  1018. public event EventHandler Disposed {
  1019. add {
  1020. event_mask |= disposed_mask;
  1021. Events.AddHandler (DisposedEvent, value);
  1022. }
  1023. remove { Events.RemoveHandler (DisposedEvent, value); }
  1024. }
  1025. [WebSysDescription ("Raised when the page containing the control is initialized.")]
  1026. public event EventHandler Init {
  1027. add {
  1028. event_mask |= init_mask;
  1029. Events.AddHandler (InitEvent, value);
  1030. }
  1031. remove { Events.RemoveHandler (InitEvent, value); }
  1032. }
  1033. [WebSysDescription ("Raised after the page containing the control has been loaded.")]
  1034. public event EventHandler Load {
  1035. add {
  1036. event_mask |= load_mask;
  1037. Events.AddHandler (LoadEvent, value);
  1038. }
  1039. remove { Events.RemoveHandler (LoadEvent, value); }
  1040. }
  1041. [WebSysDescription ("Raised before the page containing the control is rendered.")]
  1042. public event EventHandler PreRender {
  1043. add {
  1044. event_mask |= prerender_mask;
  1045. Events.AddHandler (PreRenderEvent, value);
  1046. }
  1047. remove { Events.RemoveHandler (PreRenderEvent, value); }
  1048. }
  1049. [WebSysDescription ("Raised when the page containing the control is unloaded.")]
  1050. public event EventHandler Unload {
  1051. add {
  1052. event_mask |= unload_mask;
  1053. Events.AddHandler (UnloadEvent, value);
  1054. }
  1055. remove { Events.RemoveHandler (UnloadEvent, value); }
  1056. }
  1057. public virtual void DataBind () //DIT
  1058. {
  1059. #if NET_2_0
  1060. DataBind (true);
  1061. #else
  1062. OnDataBinding (EventArgs.Empty);
  1063. DataBindChildren();
  1064. #endif
  1065. }
  1066. #if NET_2_0
  1067. protected virtual
  1068. #endif
  1069. void DataBindChildren ()
  1070. {
  1071. if (!HasControls ())
  1072. return;
  1073. int len = _controls.Count;
  1074. for (int i = 0; i < len; i++) {
  1075. Control c = _controls [i];
  1076. c.DataBind ();
  1077. }
  1078. }
  1079. public virtual bool HasControls ()
  1080. {
  1081. return (_controls != null && _controls.Count > 0);
  1082. }
  1083. #if NET_2_0
  1084. public virtual
  1085. #else
  1086. public
  1087. #endif
  1088. void RenderControl (HtmlTextWriter writer)
  1089. {
  1090. if ((stateMask & VISIBLE) != 0) {
  1091. HttpContext ctx = Context;
  1092. TraceContext trace = (ctx != null) ? ctx.Trace : null;
  1093. int pos = 0;
  1094. if ((trace != null) && trace.IsEnabled)
  1095. pos = ctx.Response.GetOutputByteCount ();
  1096. Render (writer);
  1097. if ((trace != null) && trace.IsEnabled) {
  1098. int size = ctx.Response.GetOutputByteCount () - pos;
  1099. trace.SaveSize (this, size >= 0 ? size : 0);
  1100. }
  1101. }
  1102. }
  1103. #if NET_2_0
  1104. protected void RenderControl (HtmlTextWriter writer, ControlAdapter adapter)
  1105. {
  1106. if ((stateMask & VISIBLE) != 0) {
  1107. adapter.BeginRender (writer);
  1108. adapter.Render (writer);
  1109. adapter.EndRender (writer);
  1110. }
  1111. }
  1112. #endif
  1113. public string ResolveUrl (string relativeUrl)
  1114. {
  1115. if (relativeUrl == null)
  1116. throw new ArgumentNullException ("relativeUrl");
  1117. if (relativeUrl == "")
  1118. return "";
  1119. if (relativeUrl [0] == '#')
  1120. return relativeUrl;
  1121. string ts = TemplateSourceDirectory;
  1122. if (ts == null || ts.Length == 0 ||
  1123. Context == null || Context.Response == null ||
  1124. !UrlUtils.IsRelativeUrl (relativeUrl))
  1125. return relativeUrl;
  1126. HttpResponse resp = Context.Response;
  1127. return resp.ApplyAppPathModifier (UrlUtils.Combine (ts, relativeUrl));
  1128. }
  1129. #if NET_2_0
  1130. public
  1131. #else
  1132. internal
  1133. #endif
  1134. string ResolveClientUrl (string relativeUrl)
  1135. {
  1136. if (relativeUrl == null)
  1137. throw new ArgumentNullException ("relativeUrl");
  1138. if (relativeUrl.Length == 0)
  1139. return String.Empty;
  1140. #if TARGET_J2EE
  1141. relativeUrl = ResolveClientUrlInternal (relativeUrl);
  1142. javax.faces.context.FacesContext faces = getFacesContext ();
  1143. if (faces == null)
  1144. return relativeUrl;
  1145. string url;
  1146. if (relativeUrl.IndexOf (':') >= 0)
  1147. url = ResolveAppRelativeFromFullPath (relativeUrl);
  1148. else if (VirtualPathUtility.IsAbsolute (relativeUrl))
  1149. url = VirtualPathUtility.ToAppRelative (relativeUrl);
  1150. else
  1151. return faces.getApplication ().getViewHandler ().getResourceURL (faces, relativeUrl);
  1152. if (VirtualPathUtility.IsAppRelative (url)) {
  1153. url = url.Substring (1);
  1154. url = url.Length == 0 ? "/" : url;
  1155. return faces.getApplication ().getViewHandler ().getResourceURL (faces, url);
  1156. }
  1157. return relativeUrl;
  1158. }
  1159. string ResolveClientUrlInternal (string relativeUrl) {
  1160. #endif
  1161. if (VirtualPathUtility.IsAbsolute (relativeUrl) || relativeUrl.IndexOf (':') >= 0)
  1162. return relativeUrl;
  1163. HttpContext context = Context;
  1164. if (context != null && context.Request != null) {
  1165. string templateSourceDirectory = TemplateSourceDirectory;
  1166. if (templateSourceDirectory == null || templateSourceDirectory.Length == 0)
  1167. return relativeUrl;
  1168. string basePath = context.Request.CurrentExecutionFilePath;
  1169. if (basePath.Length > 1 && basePath [basePath.Length - 1] != '/')
  1170. basePath = VirtualPathUtility.GetDirectory (basePath, false);
  1171. if (VirtualPathUtility.IsAppRelative (relativeUrl))
  1172. return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
  1173. string templatePath = VirtualPathUtility.AppendTrailingSlash (templateSourceDirectory);
  1174. if (basePath.Length == templatePath.Length && String.CompareOrdinal (basePath, templatePath) == 0)
  1175. return relativeUrl;
  1176. relativeUrl = VirtualPathUtility.Combine (templatePath, relativeUrl);
  1177. return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
  1178. }
  1179. return relativeUrl;
  1180. }
  1181. internal bool HasRenderMethodDelegate ()
  1182. {
  1183. return _renderMethodDelegate != null;
  1184. }
  1185. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1186. public void SetRenderMethodDelegate (RenderMethod renderMethod) //DIT
  1187. {
  1188. _renderMethodDelegate = renderMethod;
  1189. }
  1190. internal void LoadRecursive ()
  1191. {
  1192. #if MONO_TRACE
  1193. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1194. string type_name = null;
  1195. if (trace != null) {
  1196. type_name = GetType ().Name;
  1197. trace.Write ("control", String.Concat ("LoadRecursive ", _userId, " ", type_name));
  1198. }
  1199. #endif
  1200. #if NET_2_0
  1201. if (Adapter != null)
  1202. Adapter.OnLoad (EventArgs.Empty);
  1203. else
  1204. #endif
  1205. OnLoad (EventArgs.Empty);
  1206. if (HasControls ()) {
  1207. int len = _controls.Count;
  1208. for (int i = 0; i < len; i++) {
  1209. Control c = _controls [i];
  1210. c.LoadRecursive ();
  1211. }
  1212. }
  1213. #if MONO_TRACE
  1214. if (trace != null)
  1215. trace.Write ("control", String.Concat ("End LoadRecursive ", _userId, " ", type_name));
  1216. #endif
  1217. stateMask |= LOADED;
  1218. }
  1219. internal void UnloadRecursive (Boolean dispose)
  1220. {
  1221. #if MONO_TRACE
  1222. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1223. string type_name = null;
  1224. if (trace != null) {
  1225. type_name = GetType ().Name;
  1226. trace.Write ("control", String.Concat ("UnloadRecursive ", _userId, " ", type_name));
  1227. }
  1228. #endif
  1229. if (HasControls ()) {
  1230. int len = _controls.Count;
  1231. for (int i = 0; i < len; i++) {
  1232. Control c = _controls [i];
  1233. c.UnloadRecursive (dispose);
  1234. }
  1235. }
  1236. #if MONO_TRACE
  1237. if (trace != null)
  1238. trace.Write ("control", String.Concat ("End UnloadRecursive ", _userId, " ", type_name));
  1239. #endif
  1240. #if NET_2_0
  1241. if (Adapter != null)
  1242. Adapter.OnUnload (EventArgs.Empty);
  1243. else
  1244. #endif
  1245. OnUnload (EventArgs.Empty);
  1246. if (dispose)
  1247. Dispose ();
  1248. }
  1249. internal void PreRenderRecursiveInternal ()
  1250. {
  1251. bool visible;
  1252. #if NET_2_0
  1253. visible = Visible;
  1254. #else
  1255. visible = (stateMask & VISIBLE) != 0;
  1256. #endif
  1257. if (visible) {
  1258. #if NET_2_0
  1259. SetMask (VISIBLE, true);
  1260. #endif
  1261. EnsureChildControls ();
  1262. #if MONO_TRACE
  1263. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1264. string type_name = null;
  1265. if (trace != null) {
  1266. type_name = GetType ().Name;
  1267. trace.Write ("control", String.Concat ("PreRenderRecursive ", _userId, " ", type_name));
  1268. }
  1269. #endif
  1270. #if NET_2_0
  1271. if (Adapter != null)
  1272. Adapter.OnPreRender (EventArgs.Empty);
  1273. else
  1274. #endif
  1275. OnPreRender (EventArgs.Empty);
  1276. if (!HasControls ())
  1277. return;
  1278. int len = _controls.Count;
  1279. for (int i = 0; i < len; i++) {
  1280. Control c = _controls [i];
  1281. c.PreRenderRecursiveInternal ();
  1282. }
  1283. #if MONO_TRACE
  1284. if (trace != null)
  1285. trace.Write ("control", String.Concat ("End PreRenderRecursive ", _userId, " ", type_name));
  1286. #endif
  1287. }
  1288. #if NET_2_0
  1289. else
  1290. SetMask (VISIBLE, false);
  1291. #endif
  1292. stateMask |= PRERENDERED;
  1293. }
  1294. internal void InitRecursive (Control namingContainer)
  1295. {
  1296. #if MONO_TRACE
  1297. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1298. string type_name = null;
  1299. if (trace != null) {
  1300. type_name = GetType ().Name;
  1301. trace.Write ("control", String.Concat ("InitRecursive ", _userId, " ", type_name));
  1302. }
  1303. #endif
  1304. SetNamingContainer (namingContainer);
  1305. if (HasControls ()) {
  1306. if ((stateMask & IS_NAMING_CONTAINER) != 0)
  1307. namingContainer = this;
  1308. int len = _controls.Count;
  1309. for (int i = 0; i < len; i++) {
  1310. Control c = _controls [i];
  1311. c.InitRecursive (namingContainer);
  1312. }
  1313. }
  1314. stateMask |= INITING;
  1315. #if NET_2_0
  1316. ApplyTheme ();
  1317. if (Adapter != null)
  1318. Adapter.OnInit (EventArgs.Empty);
  1319. else
  1320. #endif
  1321. OnInit (EventArgs.Empty);
  1322. #if MONO_TRACE
  1323. if (trace != null)
  1324. trace.Write ("control", String.Concat ("End InitRecursive ", _userId, " ", type_name));
  1325. #endif
  1326. TrackViewState ();
  1327. stateMask |= INITED;
  1328. stateMask &= ~INITING;
  1329. }
  1330. internal object SaveViewStateRecursive ()
  1331. {
  1332. if (!EnableViewState)
  1333. return null;
  1334. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1335. #if MONO_TRACE
  1336. string type_name = null;
  1337. if (trace != null) {
  1338. type_name = GetType ().Name;
  1339. trace.Write ("control", String.Concat ("SaveViewStateRecursive ", _userId, " ", type_name));
  1340. }
  1341. #endif
  1342. ArrayList controlList = null;
  1343. ArrayList controlStates = null;
  1344. int idx = -1;
  1345. if (HasControls ()) {
  1346. int len = _controls.Count;
  1347. for (int i = 0; i < len; i++) {
  1348. Control ctrl = _controls [i];
  1349. object ctrlState = ctrl.SaveViewStateRecursive ();
  1350. idx++;
  1351. if (ctrlState == null)
  1352. continue;
  1353. if (controlList == null) {
  1354. controlList = new ArrayList ();
  1355. controlStates = new ArrayList ();
  1356. }
  1357. controlList.Add (idx);
  1358. controlStates.Add (ctrlState);
  1359. }
  1360. }
  1361. object thisState = SaveViewState ();
  1362. if (thisState == null && controlList == null && controlStates == null) {
  1363. if (trace != null) {
  1364. #if MONO_TRACE
  1365. trace.Write ("control", "End SaveViewStateRecursive " + _userId + " " + type_name + " saved nothing");
  1366. #endif
  1367. trace.SaveViewState (this, null);
  1368. }
  1369. return null;
  1370. }
  1371. if (trace != null) {
  1372. #if MONO_TRACE
  1373. trace.Write ("control", "End SaveViewStateRecursive " + _userId + " " + type_name + " saved a Triplet");
  1374. #endif
  1375. trace.SaveViewState (this, thisState);
  1376. }
  1377. return new Triplet (thisState, controlList, controlStates);
  1378. }
  1379. internal void LoadViewStateRecursive (object savedState)
  1380. {
  1381. if (!EnableViewState || savedState == null)
  1382. return;
  1383. #if MONO_TRACE
  1384. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1385. string type_name = null;
  1386. if (trace != null) {
  1387. type_name = GetType ().Name;
  1388. trace.Write ("control", String.Concat ("LoadViewStateRecursive ", _userId, " ", type_name));
  1389. }
  1390. #endif
  1391. Triplet savedInfo = (Triplet) savedState;
  1392. LoadViewState (savedInfo.First);
  1393. ArrayList controlList = savedInfo.Second as ArrayList;
  1394. if (controlList == null)
  1395. return;
  1396. ArrayList controlStates = savedInfo.Third as ArrayList;
  1397. int nControls = controlList.Count;
  1398. for (int i = 0; i < nControls; i++) {
  1399. int k = (int) controlList [i];
  1400. if (k < Controls.Count && controlStates != null) {
  1401. Control c = Controls [k];
  1402. c.LoadViewStateRecursive (controlStates [i]);
  1403. }
  1404. else {
  1405. if (pendingVS == null)
  1406. pendingVS = new Hashtable ();
  1407. pendingVS [k] = controlStates [i];
  1408. }
  1409. }
  1410. #if MONO_TRACE
  1411. if (trace != null)
  1412. trace.Write ("control", String.Concat ("End LoadViewStateRecursive ", _userId, " ", type_name));
  1413. #endif
  1414. stateMask |= VIEWSTATE_LOADED;
  1415. }
  1416. #if NET_2_0
  1417. internal ControlSkin controlSkin;
  1418. internal void ApplyTheme ()
  1419. {
  1420. #if MONO_TRACE
  1421. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1422. string type_name = null;
  1423. if (trace != null) {
  1424. type_name = GetType ().Name;
  1425. trace.Write ("control", String.Concat ("ApplyThemeRecursive ", _userId, " ", type_name));
  1426. }
  1427. #endif
  1428. if (Page.PageTheme != null && EnableTheming) {
  1429. ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
  1430. if (controlSkin != null)
  1431. controlSkin.ApplySkin (this);
  1432. }
  1433. #if MONO_TRACE
  1434. if (trace != null)
  1435. trace.Write ("control", String.Concat ("End ApplyThemeRecursive ", _userId, " ", type_name));
  1436. #endif
  1437. }
  1438. #endif
  1439. internal bool AutoID {
  1440. get { return (stateMask & AUTOID) != 0; }
  1441. set {
  1442. if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
  1443. return;
  1444. SetMask (AUTOID, value);
  1445. }
  1446. }
  1447. protected internal virtual void RemovedControl (Control control)
  1448. {
  1449. control.UnloadRecursive (false);
  1450. control._parent = null;
  1451. control._page = null;
  1452. control._namingContainer = null;
  1453. if ((control.stateMask & AUTOID_SET) != 0) {
  1454. control._userId = null;
  1455. control.SetMask (ID_SET, false);
  1456. }
  1457. control.NullifyUniqueID ();
  1458. }
  1459. #if NET_2_0
  1460. string skinId = string.Empty;
  1461. bool _enableTheming = true;
  1462. [Browsable (false)]
  1463. [Themeable (false)]
  1464. [DefaultValue (true)]
  1465. public virtual bool EnableTheming {
  1466. get {
  1467. if ((stateMask & ENABLE_THEMING) != 0)
  1468. return _enableTheming;
  1469. if (_parent != null)
  1470. return _parent.EnableTheming;
  1471. return true;
  1472. }
  1473. set {
  1474. SetMask (ENABLE_THEMING, true);
  1475. _enableTheming = value;
  1476. }
  1477. }
  1478. [Browsable (false)]
  1479. [DefaultValue ("")]
  1480. [Filterable (false)]
  1481. public virtual string SkinID {
  1482. get { return skinId; }
  1483. set { skinId = value; }
  1484. }
  1485. ControlBuilder IControlBuilderAccessor.ControlBuilder {
  1486. get { throw new NotImplementedException (); }
  1487. }
  1488. IDictionary IControlDesignerAccessor.GetDesignModeState ()
  1489. {
  1490. throw new NotImplementedException ();
  1491. }
  1492. void IControlDesignerAccessor.SetDesignModeState (IDictionary designData)
  1493. {
  1494. SetDesignModeState (designData);
  1495. }
  1496. void IControlDesignerAccessor.SetOwnerControl (Control control)
  1497. {
  1498. throw new NotImplementedException ();
  1499. }
  1500. IDictionary IControlDesignerAccessor.UserData {
  1501. get { throw new NotImplementedException (); }
  1502. }
  1503. ExpressionBindingCollection expressionBindings;
  1504. ExpressionBindingCollection IExpressionsAccessor.Expressions {
  1505. get {
  1506. if (expressionBindings == null)
  1507. expressionBindings = new ExpressionBindingCollection ();
  1508. return expressionBindings;
  1509. }
  1510. }
  1511. bool IExpressionsAccessor.HasExpressions {
  1512. get { return (expressionBindings != null && expressionBindings.Count > 0); }
  1513. }
  1514. public virtual void Focus ()
  1515. {
  1516. Page.SetFocus (this);
  1517. }
  1518. protected internal virtual void LoadControlState (object state)
  1519. {
  1520. }
  1521. protected internal virtual object SaveControlState ()
  1522. {
  1523. return null;
  1524. }
  1525. protected virtual void DataBind (bool raiseOnDataBinding)
  1526. {
  1527. bool foundDataItem = false;
  1528. if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
  1529. object o = DataBinder.GetDataItem (this, out foundDataItem);
  1530. if (foundDataItem)
  1531. Page.PushDataItemContext (o);
  1532. }
  1533. try {
  1534. if (raiseOnDataBinding)
  1535. OnDataBinding (EventArgs.Empty);
  1536. DataBindChildren ();
  1537. } finally {
  1538. if (foundDataItem)
  1539. Page.PopDataItemContext ();
  1540. }
  1541. }
  1542. protected virtual IDictionary GetDesignModeState ()
  1543. {
  1544. throw new NotImplementedException ();
  1545. }
  1546. protected virtual void SetDesignModeState (IDictionary data)
  1547. {
  1548. throw new NotImplementedException ();
  1549. }
  1550. internal bool IsInited {
  1551. get { return (stateMask & INITED) != 0; }
  1552. }
  1553. internal bool IsLoaded {
  1554. get { return (stateMask & LOADED) != 0; }
  1555. }
  1556. internal bool IsPrerendered {
  1557. get { return (stateMask & PRERENDERED) != 0; }
  1558. }
  1559. #endif
  1560. void IParserAccessor.AddParsedSubObject (object obj)
  1561. {
  1562. this.AddParsedSubObject (obj);
  1563. }
  1564. DataBindingCollection IDataBindingsAccessor.DataBindings {
  1565. get {
  1566. if (dataBindings == null) {
  1567. dataBindings = new DataBindingCollection ();
  1568. }
  1569. return dataBindings;
  1570. }
  1571. }
  1572. bool IDataBindingsAccessor.HasDataBindings {
  1573. get {
  1574. if (dataBindings != null && dataBindings.Count > 0) {
  1575. return true;
  1576. }
  1577. return false;
  1578. }
  1579. }
  1580. }
  1581. }