Control.cs 51 KB

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