Control.cs 57 KB

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