Control.cs 57 KB

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