Control.cs 50 KB

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