Control.cs 49 KB

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