Control.cs 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922
  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 is INonBindingContainer || (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. ResetChildNames (-1);
  512. }
  513. internal void ResetChildNames (int value)
  514. {
  515. if (value < 0)
  516. defaultNumberID = 0;
  517. else
  518. defaultNumberID = value;
  519. }
  520. internal int GetDefaultNumberID ()
  521. {
  522. return defaultNumberID;
  523. }
  524. string GetDefaultName ()
  525. {
  526. string defaultName;
  527. if (defaultNumberID > 99) {
  528. #if NET_2_0
  529. defaultName = "ctl" + defaultNumberID++;
  530. #else
  531. defaultName = "_ctl" + defaultNumberID++;
  532. #endif
  533. } else {
  534. defaultName = defaultNameArray [defaultNumberID++];
  535. }
  536. return defaultName;
  537. }
  538. void NullifyUniqueID ()
  539. {
  540. uniqueID = null;
  541. if (!HasControls ())
  542. return;
  543. for (int i = 0; i < _controls.Count; i++)
  544. _controls [i].NullifyUniqueID ();
  545. }
  546. protected internal virtual void AddedControl (Control control, int index)
  547. {
  548. ResetControlsCache ();
  549. /* Ensure the control don't have more than 1 parent */
  550. if (control._parent != null)
  551. control._parent.Controls.Remove (control);
  552. control._parent = this;
  553. Control nc = ((stateMask & IS_NAMING_CONTAINER) != 0) ? this : NamingContainer;
  554. if ((stateMask & (INITING | INITED)) != 0)
  555. control.InitRecursive (nc);
  556. else {
  557. control.SetNamingContainer (nc);
  558. return;
  559. }
  560. if ((stateMask & (VIEWSTATE_LOADED | LOADED)) != 0) {
  561. if (pendingVS != null) {
  562. object vs = pendingVS [index];
  563. if (vs != null) {
  564. pendingVS.Remove (index);
  565. if (pendingVS.Count == 0)
  566. pendingVS = null;
  567. control.LoadViewStateRecursive (vs);
  568. }
  569. }
  570. }
  571. if ((stateMask & LOADED) != 0)
  572. control.LoadRecursive ();
  573. if ((stateMask & PRERENDERED) != 0)
  574. control.PreRenderRecursiveInternal ();
  575. }
  576. void SetNamingContainer (Control nc)
  577. {
  578. if (nc != null) {
  579. _namingContainer = nc;
  580. if (AutoID)
  581. EnsureIDInternal ();
  582. }
  583. }
  584. protected virtual void AddParsedSubObject (object obj) //DIT
  585. {
  586. Control c = obj as Control;
  587. if (c != null)
  588. Controls.Add (c);
  589. }
  590. #if NET_2_0
  591. [EditorBrowsable (EditorBrowsableState.Advanced)]
  592. public virtual void ApplyStyleSheetSkin (Page page)
  593. {
  594. if (!EnableTheming) /* this enough? */
  595. return;
  596. /* apply the style sheet skin here */
  597. if (page.StyleSheetPageTheme != null) {
  598. ControlSkin cs = page.StyleSheetPageTheme.GetControlSkin (GetType (), SkinID);
  599. if (cs != null)
  600. cs.ApplySkin (this);
  601. }
  602. }
  603. #endif
  604. protected void BuildProfileTree (string parentId, bool calcViewState)
  605. {
  606. //TODO
  607. }
  608. #if NET_2_0
  609. protected void ClearChildControlState ()
  610. {
  611. _isChildControlStateCleared = true;
  612. }
  613. protected void ClearChildState ()
  614. {
  615. ClearChildViewState ();
  616. ClearChildControlState ();
  617. }
  618. #endif
  619. protected void ClearChildViewState ()
  620. {
  621. pendingVS = null;
  622. }
  623. #if NET_2_0
  624. protected internal
  625. #else
  626. protected
  627. #endif
  628. virtual void CreateChildControls () //DIT
  629. {
  630. }
  631. protected virtual ControlCollection CreateControlCollection () //DIT
  632. {
  633. return new ControlCollection (this);
  634. }
  635. protected virtual void EnsureChildControls ()
  636. {
  637. if (ChildControlsCreated == false && (stateMask & CREATING_CONTROLS) == 0) {
  638. stateMask |= CREATING_CONTROLS;
  639. #if NET_2_0
  640. if (Adapter != null)
  641. Adapter.CreateChildControls ();
  642. else
  643. #endif
  644. CreateChildControls ();
  645. ChildControlsCreated = true;
  646. stateMask &= ~CREATING_CONTROLS;
  647. }
  648. }
  649. void EnsureIDInternal ()
  650. {
  651. if (_userId != null)
  652. return;
  653. _userId = NamingContainer.GetDefaultName ();
  654. SetMask (AUTOID_SET, true);
  655. }
  656. #if NET_2_0
  657. protected void EnsureID ()
  658. {
  659. if (NamingContainer == null)
  660. return;
  661. EnsureIDInternal ();
  662. SetMask (ID_SET, true);
  663. }
  664. protected bool HasEvents ()
  665. {
  666. return _events != null;
  667. }
  668. #endif
  669. void ResetControlsCache ()
  670. {
  671. _controlsCache = null;
  672. if ((this.stateMask & IS_NAMING_CONTAINER) == 0 && Parent != null)
  673. Parent.ResetControlsCache ();
  674. }
  675. Hashtable InitControlsCache ()
  676. {
  677. if (_controlsCache != null)
  678. return _controlsCache;
  679. if ((this.stateMask & IS_NAMING_CONTAINER) != 0 || Parent == null)
  680. //LAMESPEC: MS' docs don't mention it, but FindControl is case insensitive.
  681. #if NET_2_0
  682. _controlsCache = new Hashtable (StringComparer.OrdinalIgnoreCase);
  683. #else
  684. _controlsCache = new Hashtable (CaseInsensitiveHashCodeProvider.Default, CaseInsensitiveComparer.Default);
  685. #endif
  686. else
  687. _controlsCache = Parent.InitControlsCache ();
  688. return _controlsCache;
  689. }
  690. void EnsureControlsCache ()
  691. {
  692. if (_controlsCache != null)
  693. return;
  694. InitControlsCache ();
  695. FillControlCache (this, Controls);
  696. }
  697. void FillControlCache (Control control, ControlCollection controls)
  698. {
  699. foreach (Control c in controls) {
  700. try {
  701. if (c._userId != null)
  702. control._controlsCache.Add (c._userId, c);
  703. } catch (ArgumentException) {
  704. throw new HttpException (
  705. "Multiple controls with the same ID '" +
  706. c._userId +
  707. "' were found. FindControl requires that controls have unique IDs. ");
  708. }
  709. if ((c.stateMask & IS_NAMING_CONTAINER) == 0 && c.HasControls ())
  710. control.FillControlCache (control, c.Controls);
  711. }
  712. }
  713. protected bool IsLiteralContent ()
  714. {
  715. if (HasControls () && _controls.Count == 1 && (_controls [0] is LiteralControl))
  716. return true;
  717. return false;
  718. }
  719. [WebSysDescription ("")]
  720. public virtual Control FindControl (string id)
  721. {
  722. return FindControl (id, 0);
  723. }
  724. Control LookForControlByName (string id)
  725. {
  726. EnsureControlsCache ();
  727. return (Control) _controlsCache [id];
  728. }
  729. protected virtual Control FindControl (string id, int pathOffset)
  730. {
  731. EnsureChildControls ();
  732. Control namingContainer = null;
  733. if ((stateMask & IS_NAMING_CONTAINER) == 0) {
  734. namingContainer = NamingContainer;
  735. if (namingContainer == null)
  736. return null;
  737. return namingContainer.FindControl (id, pathOffset);
  738. }
  739. if (!HasControls ())
  740. return null;
  741. int separatorIdx = id.IndexOf (IdSeparator, pathOffset);
  742. if (separatorIdx == -1)
  743. return LookForControlByName (id.Substring (pathOffset));
  744. string idfound = id.Substring (pathOffset, separatorIdx - pathOffset);
  745. namingContainer = LookForControlByName (idfound);
  746. if (namingContainer == null)
  747. return null;
  748. return namingContainer.FindControl (id, separatorIdx + 1);
  749. }
  750. protected virtual void LoadViewState (object savedState)
  751. {
  752. if (savedState != null) {
  753. ViewState.LoadViewState (savedState);
  754. object o = ViewState ["Visible"];
  755. if (o != null) {
  756. SetMask (VISIBLE, (bool) o);
  757. stateMask |= VISIBLE_CHANGED;
  758. }
  759. }
  760. }
  761. // [MonoTODO("Secure?")]
  762. protected string MapPathSecure (string virtualPath)
  763. {
  764. string combined = UrlUtils.Combine (TemplateSourceDirectory, virtualPath);
  765. return Context.Request.MapPath (combined);
  766. }
  767. protected virtual bool OnBubbleEvent (object source, EventArgs args) //DIT
  768. {
  769. #if MONO_TRACE
  770. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  771. string type_name = null;
  772. if (trace != null) {
  773. type_name = GetType ().Name;
  774. trace.Write ("control", String.Concat ("OnBubbleEvent ", _userId, " ", type_name));
  775. }
  776. #endif
  777. return false;
  778. }
  779. protected virtual void OnDataBinding (EventArgs e)
  780. {
  781. if ((event_mask & databinding_mask) != 0) {
  782. EventHandler eh = (EventHandler) (_events [DataBindingEvent]);
  783. if (eh != null) {
  784. #if MONO_TRACE
  785. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  786. string type_name = null;
  787. if (trace != null) {
  788. type_name = GetType ().Name;
  789. trace.Write ("control", String.Concat ("OnDataBinding ", _userId, " ", type_name));
  790. }
  791. #endif
  792. eh (this, e);
  793. }
  794. }
  795. }
  796. #if NET_2_0
  797. protected internal
  798. #else
  799. protected
  800. #endif
  801. virtual void OnInit (EventArgs e)
  802. {
  803. if ((event_mask & init_mask) != 0) {
  804. EventHandler eh = (EventHandler) (_events [InitEvent]);
  805. if (eh != null) {
  806. #if MONO_TRACE
  807. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  808. string type_name = null;
  809. if (trace != null) {
  810. type_name = GetType ().Name;
  811. trace.Write ("control", String.Concat ("OnInit ", _userId, " ", type_name));
  812. }
  813. #endif
  814. eh (this, e);
  815. }
  816. }
  817. }
  818. #if NET_2_0
  819. protected internal
  820. #else
  821. protected
  822. #endif
  823. virtual void OnLoad (EventArgs e)
  824. {
  825. if ((event_mask & load_mask) != 0) {
  826. EventHandler eh = (EventHandler) (_events [LoadEvent]);
  827. if (eh != null) {
  828. #if MONO_TRACE
  829. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  830. string type_name = null;
  831. if (trace != null) {
  832. type_name = GetType ().Name;
  833. trace.Write ("control", String.Concat ("OnLoad ", _userId, " ", type_name));
  834. }
  835. #endif
  836. eh (this, e);
  837. }
  838. }
  839. }
  840. #if NET_2_0
  841. protected internal
  842. #else
  843. protected
  844. #endif
  845. virtual void OnPreRender (EventArgs e)
  846. {
  847. if ((event_mask & prerender_mask) != 0) {
  848. EventHandler eh = (EventHandler) (_events [PreRenderEvent]);
  849. if (eh != null) {
  850. #if MONO_TRACE
  851. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  852. string type_name = null;
  853. if (trace != null) {
  854. type_name = GetType ().Name;
  855. trace.Write ("control", String.Concat ("OnPreRender ", _userId, " ", type_name));
  856. }
  857. #endif
  858. eh (this, e);
  859. }
  860. }
  861. }
  862. #if NET_2_0
  863. protected internal
  864. #else
  865. protected
  866. #endif
  867. virtual void OnUnload (EventArgs e)
  868. {
  869. if ((event_mask & unload_mask) != 0) {
  870. EventHandler eh = (EventHandler) (_events [UnloadEvent]);
  871. if (eh != null) {
  872. #if MONO_TRACE
  873. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  874. string type_name = null;
  875. if (trace != null) {
  876. type_name = GetType ().Name;
  877. trace.Write ("control", String.Concat ("OnUnload ", _userId, " ", type_name));
  878. }
  879. #endif
  880. eh (this, e);
  881. }
  882. }
  883. }
  884. #if NET_2_0
  885. protected internal Stream OpenFile (string path)
  886. {
  887. try {
  888. string filePath = Context.Server.MapPath (path);
  889. return File.OpenRead (filePath);
  890. }
  891. catch (UnauthorizedAccessException) {
  892. throw new HttpException ("Access to the specified file was denied.");
  893. }
  894. }
  895. internal string GetPhysicalFilePath (string virtualPath)
  896. {
  897. Page page = Page;
  898. if (VirtualPathUtility.IsAbsolute (virtualPath))
  899. return page != null ? page.MapPath (virtualPath) : Context.Server.MapPath (virtualPath);
  900. // We need to determine whether one of our parents is a
  901. // master page. If so, we need to map the path
  902. // relatively to the master page and not our containing
  903. // page/control. This is necessary for cases when a
  904. // relative path is used in a control placed in a master
  905. // page and the master page is referenced from a
  906. // location other than its own. In such cases MS.NET looks
  907. // for the file in the directory where the master page
  908. // is.
  909. //
  910. // An example of where it is needed is at
  911. //
  912. // http://quickstarts.asp.net/QuickStartv20/aspnet/samples/masterpages/masterpages_cs/pages/default.aspx
  913. //
  914. MasterPage master = null;
  915. Control ctrl = Parent;
  916. while (ctrl != null) {
  917. if (ctrl is MasterPage) {
  918. master = ctrl as MasterPage;
  919. break;
  920. }
  921. ctrl = ctrl.Parent;
  922. }
  923. string path;
  924. if (master != null)
  925. path = VirtualPathUtility.Combine (master.TemplateSourceDirectory + "/", virtualPath);
  926. else
  927. path = VirtualPathUtility.Combine (TemplateSourceDirectory + "/", virtualPath);
  928. return page != null ? page.MapPath (path) : Context.Server.MapPath (path);
  929. }
  930. #endif
  931. protected void RaiseBubbleEvent (object source, EventArgs args)
  932. {
  933. Control c = Parent;
  934. while (c != null) {
  935. #if MONO_TRACE
  936. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  937. string type_name = null;
  938. if (trace != null) {
  939. type_name = GetType ().Name;
  940. trace.Write ("control", String.Concat ("RaiseBubbleEvent ", _userId, " ", type_name));
  941. }
  942. #endif
  943. if (c.OnBubbleEvent (source, args)) {
  944. #if MONO_TRACE
  945. if (trace != null)
  946. trace.Write ("control", String.Concat ("End RaiseBubbleEvent (false) ", _userId, " ", type_name));
  947. #endif
  948. break;
  949. }
  950. #if MONO_TRACE
  951. if (trace != null)
  952. trace.Write ("control", String.Concat ("End RaiseBubbleEvent (true) ", _userId, " ", type_name));
  953. #endif
  954. c = c.Parent;
  955. }
  956. }
  957. #if NET_2_0
  958. protected internal
  959. #else
  960. protected
  961. #endif
  962. virtual void Render (HtmlTextWriter writer) //DIT
  963. {
  964. RenderChildren (writer);
  965. }
  966. #if NET_2_0
  967. protected internal
  968. #else
  969. protected
  970. #endif
  971. virtual void RenderChildren (HtmlTextWriter writer) //DIT
  972. {
  973. if (_renderMethodDelegate != null) {
  974. _renderMethodDelegate (writer, this);
  975. } else if (_controls != null) {
  976. int len = _controls.Count;
  977. Control c;
  978. for (int i = 0; i < len; i++) {
  979. c = _controls [i];
  980. if (c == null)
  981. continue;
  982. #if NET_2_0
  983. ControlAdapter tmp = c.Adapter;
  984. if (tmp != null)
  985. c.RenderControl (writer, tmp);
  986. else
  987. #endif
  988. c.RenderControl (writer);
  989. }
  990. }
  991. }
  992. #if NET_2_0
  993. protected virtual ControlAdapter ResolveAdapter ()
  994. {
  995. HttpContext context = Context;
  996. if (context == null)
  997. return null;
  998. if (!context.Request.BrowserMightHaveAdapters)
  999. return null;
  1000. // Search up the type hierarchy until we find a control with an adapter.
  1001. IDictionary typeMap = context.Request.Browser.Adapters;
  1002. Type controlType = GetType ();
  1003. Type adapterType = (Type)typeMap [controlType];
  1004. while (adapterType == null && controlType != typeof (Control)) {
  1005. controlType = controlType.BaseType;
  1006. adapterType = (Type)typeMap [controlType];
  1007. }
  1008. ControlAdapter a = null;
  1009. if (adapterType != null)
  1010. a = (ControlAdapter)Activator.CreateInstance (adapterType);
  1011. return a;
  1012. }
  1013. #endif
  1014. protected virtual object SaveViewState ()
  1015. {
  1016. if ((stateMask & VISIBLE_CHANGED) != 0) {
  1017. ViewState ["Visible"] = (stateMask & VISIBLE) != 0;
  1018. } else if (_viewState == null) {
  1019. return null;
  1020. }
  1021. return _viewState.SaveViewState ();
  1022. }
  1023. protected virtual void TrackViewState ()
  1024. {
  1025. if (_viewState != null)
  1026. _viewState.TrackViewState ();
  1027. stateMask |= TRACK_VIEWSTATE;
  1028. }
  1029. public virtual void Dispose ()
  1030. {
  1031. if ((event_mask & disposed_mask) != 0) {
  1032. EventHandler eh = (EventHandler) (_events [DisposedEvent]);
  1033. if (eh != null)
  1034. eh (this, EventArgs.Empty);
  1035. }
  1036. }
  1037. [WebCategory ("FIXME")]
  1038. [WebSysDescription ("Raised when the contols databound properties are evaluated.")]
  1039. public event EventHandler DataBinding {
  1040. add {
  1041. event_mask |= databinding_mask;
  1042. Events.AddHandler (DataBindingEvent, value);
  1043. }
  1044. remove { Events.RemoveHandler (DataBindingEvent, value); }
  1045. }
  1046. [WebSysDescription ("Raised when the contol is disposed.")]
  1047. public event EventHandler Disposed {
  1048. add {
  1049. event_mask |= disposed_mask;
  1050. Events.AddHandler (DisposedEvent, value);
  1051. }
  1052. remove { Events.RemoveHandler (DisposedEvent, value); }
  1053. }
  1054. [WebSysDescription ("Raised when the page containing the control is initialized.")]
  1055. public event EventHandler Init {
  1056. add {
  1057. event_mask |= init_mask;
  1058. Events.AddHandler (InitEvent, value);
  1059. }
  1060. remove { Events.RemoveHandler (InitEvent, value); }
  1061. }
  1062. [WebSysDescription ("Raised after the page containing the control has been loaded.")]
  1063. public event EventHandler Load {
  1064. add {
  1065. event_mask |= load_mask;
  1066. Events.AddHandler (LoadEvent, value);
  1067. }
  1068. remove { Events.RemoveHandler (LoadEvent, value); }
  1069. }
  1070. [WebSysDescription ("Raised before the page containing the control is rendered.")]
  1071. public event EventHandler PreRender {
  1072. add {
  1073. event_mask |= prerender_mask;
  1074. Events.AddHandler (PreRenderEvent, value);
  1075. }
  1076. remove { Events.RemoveHandler (PreRenderEvent, value); }
  1077. }
  1078. [WebSysDescription ("Raised when the page containing the control is unloaded.")]
  1079. public event EventHandler Unload {
  1080. add {
  1081. event_mask |= unload_mask;
  1082. Events.AddHandler (UnloadEvent, value);
  1083. }
  1084. remove { Events.RemoveHandler (UnloadEvent, value); }
  1085. }
  1086. public virtual void DataBind () //DIT
  1087. {
  1088. #if NET_2_0
  1089. DataBind (true);
  1090. #else
  1091. OnDataBinding (EventArgs.Empty);
  1092. DataBindChildren();
  1093. #endif
  1094. }
  1095. #if NET_2_0
  1096. protected virtual
  1097. #endif
  1098. void DataBindChildren ()
  1099. {
  1100. if (!HasControls ())
  1101. return;
  1102. int len = _controls.Count;
  1103. for (int i = 0; i < len; i++) {
  1104. Control c = _controls [i];
  1105. c.DataBind ();
  1106. }
  1107. }
  1108. public virtual bool HasControls ()
  1109. {
  1110. return (_controls != null && _controls.Count > 0);
  1111. }
  1112. #if NET_2_0
  1113. public virtual
  1114. #else
  1115. public
  1116. #endif
  1117. void RenderControl (HtmlTextWriter writer)
  1118. {
  1119. #if NET_2_0
  1120. if (this.adapter != null) {
  1121. RenderControl (writer, this.adapter);
  1122. return;
  1123. }
  1124. #endif
  1125. if ((stateMask & VISIBLE) != 0) {
  1126. HttpContext ctx = Context;
  1127. TraceContext trace = (ctx != null) ? ctx.Trace : null;
  1128. int pos = 0;
  1129. if ((trace != null) && trace.IsEnabled)
  1130. pos = ctx.Response.GetOutputByteCount ();
  1131. Render (writer);
  1132. if ((trace != null) && trace.IsEnabled) {
  1133. int size = ctx.Response.GetOutputByteCount () - pos;
  1134. trace.SaveSize (this, size >= 0 ? size : 0);
  1135. }
  1136. }
  1137. }
  1138. #if NET_2_0
  1139. protected void RenderControl (HtmlTextWriter writer, ControlAdapter adapter)
  1140. {
  1141. if ((stateMask & VISIBLE) != 0) {
  1142. adapter.BeginRender (writer);
  1143. adapter.Render (writer);
  1144. adapter.EndRender (writer);
  1145. }
  1146. }
  1147. #endif
  1148. public string ResolveUrl (string relativeUrl)
  1149. {
  1150. if (relativeUrl == null)
  1151. throw new ArgumentNullException ("relativeUrl");
  1152. if (relativeUrl == String.Empty)
  1153. return relativeUrl;
  1154. if (VirtualPathUtility.IsAbsolute (relativeUrl))
  1155. return relativeUrl;
  1156. if (relativeUrl [0] == '#')
  1157. return relativeUrl;
  1158. #if NET_2_0
  1159. string ts = AppRelativeTemplateSourceDirectory;
  1160. #else
  1161. string ts = TemplateSourceDirectory;
  1162. #endif
  1163. if (ts == null || ts.Length == 0 ||
  1164. Context == null || Context.Response == null ||
  1165. relativeUrl.IndexOf (':') >= 0)
  1166. return relativeUrl;
  1167. HttpResponse resp = Context.Response;
  1168. if (!VirtualPathUtility.IsAppRelative (relativeUrl))
  1169. relativeUrl = VirtualPathUtility.Combine (VirtualPathUtility.AppendTrailingSlash (ts), relativeUrl);
  1170. return resp.ApplyAppPathModifier (relativeUrl);
  1171. }
  1172. #if NET_2_0
  1173. public
  1174. #else
  1175. internal
  1176. #endif
  1177. string ResolveClientUrl (string relativeUrl)
  1178. {
  1179. if (relativeUrl == null)
  1180. throw new ArgumentNullException ("relativeUrl");
  1181. if (relativeUrl.Length == 0)
  1182. return String.Empty;
  1183. #if TARGET_J2EE
  1184. relativeUrl = ResolveClientUrlInternal (relativeUrl);
  1185. javax.faces.context.FacesContext faces = getFacesContext ();
  1186. if (faces == null)
  1187. return relativeUrl;
  1188. string url;
  1189. if (relativeUrl.IndexOf (':') >= 0)
  1190. url = ResolveAppRelativeFromFullPath (relativeUrl);
  1191. else if (VirtualPathUtility.IsAbsolute (relativeUrl))
  1192. url = VirtualPathUtility.ToAppRelative (relativeUrl);
  1193. else
  1194. return faces.getApplication ().getViewHandler ().getResourceURL (faces, relativeUrl);
  1195. if (VirtualPathUtility.IsAppRelative (url)) {
  1196. url = url.Substring (1);
  1197. url = url.Length == 0 ? "/" : url;
  1198. return faces.getApplication ().getViewHandler ().getResourceURL (faces, url);
  1199. }
  1200. return relativeUrl;
  1201. }
  1202. string ResolveClientUrlInternal (string relativeUrl) {
  1203. if (relativeUrl.StartsWith (J2EE.J2EEConsts.ACTION_URL_PREFIX, StringComparison.Ordinal))
  1204. return CreateActionUrl (relativeUrl.Substring (J2EE.J2EEConsts.ACTION_URL_PREFIX.Length));
  1205. if (relativeUrl.StartsWith (J2EE.J2EEConsts.RENDER_URL_PREFIX, StringComparison.Ordinal))
  1206. return ResolveClientUrl (relativeUrl.Substring (J2EE.J2EEConsts.RENDER_URL_PREFIX.Length));
  1207. #endif
  1208. if (VirtualPathUtility.IsAbsolute (relativeUrl) || relativeUrl.IndexOf (':') >= 0)
  1209. return relativeUrl;
  1210. HttpContext context = Context;
  1211. if (context != null && context.Request != null) {
  1212. string templateSourceDirectory = TemplateSourceDirectory;
  1213. if (templateSourceDirectory == null || templateSourceDirectory.Length == 0)
  1214. return relativeUrl;
  1215. string basePath = context.Request.FilePath;
  1216. if (basePath.Length > 1 && basePath [basePath.Length - 1] != '/')
  1217. basePath = VirtualPathUtility.GetDirectory (basePath, false);
  1218. if (VirtualPathUtility.IsAppRelative (relativeUrl))
  1219. return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
  1220. string templatePath = VirtualPathUtility.AppendTrailingSlash (templateSourceDirectory);
  1221. if (basePath.Length == templatePath.Length && String.CompareOrdinal (basePath, templatePath) == 0)
  1222. return relativeUrl;
  1223. relativeUrl = VirtualPathUtility.Combine (templatePath, relativeUrl);
  1224. return VirtualPathUtility.MakeRelative (basePath, relativeUrl);
  1225. }
  1226. return relativeUrl;
  1227. }
  1228. internal bool HasRenderMethodDelegate ()
  1229. {
  1230. return _renderMethodDelegate != null;
  1231. }
  1232. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1233. public void SetRenderMethodDelegate (RenderMethod renderMethod) //DIT
  1234. {
  1235. _renderMethodDelegate = renderMethod;
  1236. }
  1237. internal void LoadRecursive ()
  1238. {
  1239. #if MONO_TRACE
  1240. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1241. string type_name = null;
  1242. if (trace != null) {
  1243. type_name = GetType ().Name;
  1244. trace.Write ("control", String.Concat ("LoadRecursive ", _userId, " ", type_name));
  1245. }
  1246. #endif
  1247. #if NET_2_0
  1248. if (Adapter != null)
  1249. Adapter.OnLoad (EventArgs.Empty);
  1250. else
  1251. #endif
  1252. OnLoad (EventArgs.Empty);
  1253. if (HasControls ()) {
  1254. int len = _controls.Count;
  1255. for (int i = 0; i < len; i++) {
  1256. Control c = _controls [i];
  1257. c.LoadRecursive ();
  1258. }
  1259. }
  1260. #if MONO_TRACE
  1261. if (trace != null)
  1262. trace.Write ("control", String.Concat ("End LoadRecursive ", _userId, " ", type_name));
  1263. #endif
  1264. stateMask |= LOADED;
  1265. }
  1266. internal void UnloadRecursive (Boolean dispose)
  1267. {
  1268. #if MONO_TRACE
  1269. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1270. string type_name = null;
  1271. if (trace != null) {
  1272. type_name = GetType ().Name;
  1273. trace.Write ("control", String.Concat ("UnloadRecursive ", _userId, " ", type_name));
  1274. }
  1275. #endif
  1276. if (HasControls ()) {
  1277. int len = _controls.Count;
  1278. for (int i = 0; i < len; i++) {
  1279. Control c = _controls [i];
  1280. c.UnloadRecursive (dispose);
  1281. }
  1282. }
  1283. #if MONO_TRACE
  1284. if (trace != null)
  1285. trace.Write ("control", String.Concat ("End UnloadRecursive ", _userId, " ", type_name));
  1286. #endif
  1287. #if NET_2_0
  1288. ControlAdapter tmp = Adapter;
  1289. if (tmp != null)
  1290. tmp.OnUnload (EventArgs.Empty);
  1291. else
  1292. #endif
  1293. OnUnload (EventArgs.Empty);
  1294. if (dispose)
  1295. Dispose ();
  1296. }
  1297. internal void PreRenderRecursiveInternal ()
  1298. {
  1299. bool visible;
  1300. #if NET_2_0
  1301. visible = Visible;
  1302. #else
  1303. visible = (stateMask & VISIBLE) != 0;
  1304. #endif
  1305. if (visible) {
  1306. #if NET_2_0
  1307. SetMask (VISIBLE, true);
  1308. #endif
  1309. EnsureChildControls ();
  1310. #if MONO_TRACE
  1311. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1312. string type_name = null;
  1313. if (trace != null) {
  1314. type_name = GetType ().Name;
  1315. trace.Write ("control", String.Concat ("PreRenderRecursive ", _userId, " ", type_name));
  1316. }
  1317. #endif
  1318. #if NET_2_0
  1319. if (Adapter != null)
  1320. Adapter.OnPreRender (EventArgs.Empty);
  1321. else
  1322. #endif
  1323. OnPreRender (EventArgs.Empty);
  1324. if (!HasControls ())
  1325. return;
  1326. int len = _controls.Count;
  1327. for (int i = 0; i < len; i++) {
  1328. Control c = _controls [i];
  1329. c.PreRenderRecursiveInternal ();
  1330. }
  1331. #if MONO_TRACE
  1332. if (trace != null)
  1333. trace.Write ("control", String.Concat ("End PreRenderRecursive ", _userId, " ", type_name));
  1334. #endif
  1335. }
  1336. #if NET_2_0
  1337. else
  1338. SetMask (VISIBLE, false);
  1339. #endif
  1340. stateMask |= PRERENDERED;
  1341. }
  1342. internal void InitRecursive (Control namingContainer)
  1343. {
  1344. #if MONO_TRACE
  1345. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1346. string type_name = null;
  1347. if (trace != null) {
  1348. type_name = GetType ().Name;
  1349. trace.Write ("control", String.Concat ("InitRecursive ", _userId, " ", type_name));
  1350. }
  1351. #endif
  1352. SetNamingContainer (namingContainer);
  1353. if (HasControls ()) {
  1354. if ((stateMask & IS_NAMING_CONTAINER) != 0)
  1355. namingContainer = this;
  1356. int len = _controls.Count;
  1357. for (int i = 0; i < len; i++) {
  1358. Control c = _controls [i];
  1359. c.InitRecursive (namingContainer);
  1360. }
  1361. }
  1362. stateMask |= INITING;
  1363. #if NET_2_0
  1364. ApplyTheme ();
  1365. ControlAdapter tmp = Adapter;
  1366. if (tmp != null)
  1367. tmp.OnInit (EventArgs.Empty);
  1368. else
  1369. #endif
  1370. OnInit (EventArgs.Empty);
  1371. #if MONO_TRACE
  1372. if (trace != null)
  1373. trace.Write ("control", String.Concat ("End InitRecursive ", _userId, " ", type_name));
  1374. #endif
  1375. TrackViewState ();
  1376. stateMask |= INITED;
  1377. stateMask &= ~INITING;
  1378. }
  1379. internal object SaveViewStateRecursive ()
  1380. {
  1381. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1382. #if MONO_TRACE
  1383. string type_name = null;
  1384. if (trace != null) {
  1385. type_name = GetType ().Name;
  1386. trace.Write ("control", String.Concat ("SaveViewStateRecursive ", _userId, " ", type_name));
  1387. }
  1388. #endif
  1389. ArrayList controlList = null;
  1390. ArrayList controlStates = null;
  1391. int idx = -1;
  1392. if (HasControls ()) {
  1393. int len = _controls.Count;
  1394. for (int i = 0; i < len; i++) {
  1395. Control ctrl = _controls [i];
  1396. object ctrlState = ctrl.SaveViewStateRecursive ();
  1397. idx++;
  1398. if (ctrlState == null)
  1399. continue;
  1400. if (controlList == null) {
  1401. controlList = new ArrayList ();
  1402. controlStates = new ArrayList ();
  1403. }
  1404. controlList.Add (idx);
  1405. controlStates.Add (ctrlState);
  1406. }
  1407. }
  1408. #if NET_2_0
  1409. object thisAdapterViewState = null;
  1410. if (Adapter != null)
  1411. thisAdapterViewState = Adapter.SaveAdapterViewState ();
  1412. #endif
  1413. object thisState = SaveViewState ();
  1414. if (thisState == null && controlList == null && controlStates == null) {
  1415. if (trace != null) {
  1416. #if MONO_TRACE
  1417. trace.Write ("control", "End SaveViewStateRecursive " + _userId + " " + type_name + " saved nothing");
  1418. #endif
  1419. trace.SaveViewState (this, null);
  1420. }
  1421. return null;
  1422. }
  1423. if (trace != null) {
  1424. #if MONO_TRACE
  1425. trace.Write ("control", "End SaveViewStateRecursive " + _userId + " " + type_name + " saved a Triplet");
  1426. #endif
  1427. trace.SaveViewState (this, thisState);
  1428. }
  1429. #if NET_2_0
  1430. thisState = new object[] { thisState, thisAdapterViewState };
  1431. #endif
  1432. return new Triplet (thisState, controlList, controlStates);
  1433. }
  1434. internal void LoadViewStateRecursive (object savedState)
  1435. {
  1436. if (savedState == null)
  1437. return;
  1438. #if MONO_TRACE
  1439. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1440. string type_name = null;
  1441. if (trace != null) {
  1442. type_name = GetType ().Name;
  1443. trace.Write ("control", String.Concat ("LoadViewStateRecursive ", _userId, " ", type_name));
  1444. }
  1445. #endif
  1446. Triplet savedInfo = (Triplet) savedState;
  1447. #if NET_2_0
  1448. object[] controlAndAdapterViewStates = (object [])savedInfo.First;
  1449. if (Adapter != null)
  1450. Adapter.LoadAdapterViewState (controlAndAdapterViewStates [1]);
  1451. LoadViewState (controlAndAdapterViewStates [0]);
  1452. #else
  1453. LoadViewState (savedInfo.First);
  1454. #endif
  1455. ArrayList controlList = savedInfo.Second as ArrayList;
  1456. if (controlList == null)
  1457. return;
  1458. ArrayList controlStates = savedInfo.Third as ArrayList;
  1459. int nControls = controlList.Count;
  1460. for (int i = 0; i < nControls; i++) {
  1461. int k = (int) controlList [i];
  1462. if (k < Controls.Count && controlStates != null) {
  1463. Control c = Controls [k];
  1464. c.LoadViewStateRecursive (controlStates [i]);
  1465. }
  1466. else {
  1467. if (pendingVS == null)
  1468. pendingVS = new Hashtable ();
  1469. pendingVS [k] = controlStates [i];
  1470. }
  1471. }
  1472. #if MONO_TRACE
  1473. if (trace != null)
  1474. trace.Write ("control", String.Concat ("End LoadViewStateRecursive ", _userId, " ", type_name));
  1475. #endif
  1476. stateMask |= VIEWSTATE_LOADED;
  1477. }
  1478. #if NET_2_0
  1479. internal ControlSkin controlSkin;
  1480. internal void ApplyTheme ()
  1481. {
  1482. #if MONO_TRACE
  1483. TraceContext trace = (Context != null && Context.Trace.IsEnabled) ? Context.Trace : null;
  1484. string type_name = null;
  1485. if (trace != null) {
  1486. type_name = GetType ().Name;
  1487. trace.Write ("control", String.Concat ("ApplyThemeRecursive ", _userId, " ", type_name));
  1488. }
  1489. #endif
  1490. if (Page.PageTheme != null && EnableTheming) {
  1491. ControlSkin controlSkin = Page.PageTheme.GetControlSkin (GetType (), SkinID);
  1492. if (controlSkin != null)
  1493. controlSkin.ApplySkin (this);
  1494. }
  1495. #if MONO_TRACE
  1496. if (trace != null)
  1497. trace.Write ("control", String.Concat ("End ApplyThemeRecursive ", _userId, " ", type_name));
  1498. #endif
  1499. }
  1500. #endif
  1501. internal bool AutoID {
  1502. get { return (stateMask & AUTOID) != 0; }
  1503. set {
  1504. if (value == false && (stateMask & IS_NAMING_CONTAINER) != 0)
  1505. return;
  1506. SetMask (AUTOID, value);
  1507. }
  1508. }
  1509. protected internal virtual void RemovedControl (Control control)
  1510. {
  1511. control.UnloadRecursive (false);
  1512. control._parent = null;
  1513. control._page = null;
  1514. control._namingContainer = null;
  1515. if ((control.stateMask & AUTOID_SET) != 0) {
  1516. control._userId = null;
  1517. control.SetMask (ID_SET, false);
  1518. }
  1519. control.NullifyUniqueID ();
  1520. }
  1521. #if NET_2_0
  1522. string skinId = string.Empty;
  1523. bool _enableTheming = true;
  1524. [Browsable (false)]
  1525. [Themeable (false)]
  1526. [DefaultValue (true)]
  1527. public virtual bool EnableTheming {
  1528. get {
  1529. if ((stateMask & ENABLE_THEMING) != 0)
  1530. return _enableTheming;
  1531. if (_parent != null)
  1532. return _parent.EnableTheming;
  1533. return true;
  1534. }
  1535. set {
  1536. SetMask (ENABLE_THEMING, true);
  1537. _enableTheming = value;
  1538. }
  1539. }
  1540. [Browsable (false)]
  1541. [DefaultValue ("")]
  1542. [Filterable (false)]
  1543. public virtual string SkinID {
  1544. get { return skinId; }
  1545. set { skinId = value; }
  1546. }
  1547. ControlBuilder IControlBuilderAccessor.ControlBuilder {
  1548. get { throw new NotImplementedException (); }
  1549. }
  1550. IDictionary IControlDesignerAccessor.GetDesignModeState ()
  1551. {
  1552. throw new NotImplementedException ();
  1553. }
  1554. void IControlDesignerAccessor.SetDesignModeState (IDictionary designData)
  1555. {
  1556. SetDesignModeState (designData);
  1557. }
  1558. void IControlDesignerAccessor.SetOwnerControl (Control control)
  1559. {
  1560. throw new NotImplementedException ();
  1561. }
  1562. IDictionary IControlDesignerAccessor.UserData {
  1563. get { throw new NotImplementedException (); }
  1564. }
  1565. ExpressionBindingCollection expressionBindings;
  1566. ExpressionBindingCollection IExpressionsAccessor.Expressions {
  1567. get {
  1568. if (expressionBindings == null)
  1569. expressionBindings = new ExpressionBindingCollection ();
  1570. return expressionBindings;
  1571. }
  1572. }
  1573. bool IExpressionsAccessor.HasExpressions {
  1574. get { return (expressionBindings != null && expressionBindings.Count > 0); }
  1575. }
  1576. public virtual void Focus ()
  1577. {
  1578. Page.SetFocus (this);
  1579. }
  1580. protected internal virtual void LoadControlState (object state)
  1581. {
  1582. }
  1583. protected internal virtual object SaveControlState ()
  1584. {
  1585. return null;
  1586. }
  1587. protected virtual void DataBind (bool raiseOnDataBinding)
  1588. {
  1589. bool foundDataItem = false;
  1590. if ((stateMask & IS_NAMING_CONTAINER) != 0 && Page != null) {
  1591. object o = DataBinder.GetDataItem (this, out foundDataItem);
  1592. if (foundDataItem)
  1593. Page.PushDataItemContext (o);
  1594. }
  1595. try {
  1596. if (raiseOnDataBinding)
  1597. OnDataBinding (EventArgs.Empty);
  1598. DataBindChildren ();
  1599. } finally {
  1600. if (foundDataItem)
  1601. Page.PopDataItemContext ();
  1602. }
  1603. }
  1604. protected virtual IDictionary GetDesignModeState ()
  1605. {
  1606. throw new NotImplementedException ();
  1607. }
  1608. protected virtual void SetDesignModeState (IDictionary data)
  1609. {
  1610. throw new NotImplementedException ();
  1611. }
  1612. internal bool IsInited {
  1613. get { return (stateMask & INITED) != 0; }
  1614. }
  1615. internal bool IsLoaded {
  1616. get { return (stateMask & LOADED) != 0; }
  1617. }
  1618. internal bool IsPrerendered {
  1619. get { return (stateMask & PRERENDERED) != 0; }
  1620. }
  1621. bool CheckForValidationSupport ()
  1622. {
  1623. return GetType ().GetCustomAttributes (typeof (SupportsEventValidationAttribute), false).Length > 0;
  1624. }
  1625. //
  1626. // Apparently this is where .NET routes validation from all the controls which
  1627. // support it. See:
  1628. //
  1629. // http://odetocode.com/Blogs/scott/archive/2006/03/20/3145.aspx
  1630. // Sample in here contains ValidateEvent in the stack trace
  1631. //
  1632. // http://odetocode.com/blogs/scott/archive/2006/03/21/3153.aspx
  1633. //
  1634. // http://www.alexthissen.nl/blogs/main/archive/2005/12/13/event-validation-of-controls-in-asp-net-2-0.aspx
  1635. //
  1636. // It also seems that it's the control's responsibility to call this method or
  1637. // validation won't take place. Also, the SupportsEventValidation attribute must be
  1638. // present on the control for validation to take place.
  1639. //
  1640. internal void ValidateEvent (String uniqueId, String argument)
  1641. {
  1642. Page page = Page;
  1643. if (page != null && CheckForValidationSupport ())
  1644. page.ClientScript.ValidateEvent (uniqueId, argument);
  1645. }
  1646. #endif
  1647. void IParserAccessor.AddParsedSubObject (object obj)
  1648. {
  1649. this.AddParsedSubObject (obj);
  1650. }
  1651. DataBindingCollection IDataBindingsAccessor.DataBindings {
  1652. get {
  1653. if (dataBindings == null) {
  1654. dataBindings = new DataBindingCollection ();
  1655. }
  1656. return dataBindings;
  1657. }
  1658. }
  1659. bool IDataBindingsAccessor.HasDataBindings {
  1660. get {
  1661. if (dataBindings != null && dataBindings.Count > 0) {
  1662. return true;
  1663. }
  1664. return false;
  1665. }
  1666. }
  1667. }
  1668. }