Control.cs 57 KB

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