Control.cs 48 KB

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