Page.cs 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768
  1. //
  2. // System.Web.UI.Page.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. // Andreas Nahr ([email protected])
  8. //
  9. // (C) 2002,2003 Ximian, Inc. (http://www.ximian.com)
  10. // Copyright (C) 2003,2005 Novell, Inc (http://www.novell.com)
  11. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Collections.Specialized;
  34. using System.ComponentModel;
  35. using System.ComponentModel.Design;
  36. using System.ComponentModel.Design.Serialization;
  37. using System.Globalization;
  38. using System.IO;
  39. using System.Security.Cryptography;
  40. using System.Security.Permissions;
  41. using System.Security.Principal;
  42. using System.Text;
  43. using System.Threading;
  44. using System.Web;
  45. using System.Web.Caching;
  46. using System.Web.Configuration;
  47. using System.Web.SessionState;
  48. using System.Web.Util;
  49. using System.Web.UI.HtmlControls;
  50. using System.Web.UI.WebControls;
  51. #if NET_2_0
  52. using System.Web.UI.Adapters;
  53. using System.Collections.Generic;
  54. using System.Reflection;
  55. #endif
  56. namespace System.Web.UI
  57. {
  58. // CAS
  59. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  60. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  61. #if !NET_2_0
  62. [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
  63. #endif
  64. [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
  65. [ToolboxItem (false)]
  66. #if NET_2_0
  67. [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
  68. #else
  69. [Designer ("Microsoft.VSDesigner.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VSDesigner, typeof (IRootDesigner))]
  70. #endif
  71. public partial class Page : TemplateControl, IHttpHandler
  72. {
  73. static string machineKeyConfigPath = "system.web/machineKey";
  74. #if NET_2_0
  75. private PageLifeCycle _lifeCycle = PageLifeCycle.Unknown;
  76. private bool _eventValidation = true;
  77. private object [] _savedControlState;
  78. private bool _doLoadPreviousPage;
  79. string _focusedControlID;
  80. bool _hasEnabledControlArray;
  81. #endif
  82. private bool _viewState = true;
  83. private bool _viewStateMac;
  84. private string _errorPage;
  85. private bool is_validated;
  86. private bool _smartNavigation;
  87. private int _transactionMode;
  88. private ValidatorCollection _validators;
  89. private bool renderingForm;
  90. private string _savedViewState;
  91. private ArrayList _requiresPostBack;
  92. private ArrayList _requiresPostBackCopy;
  93. private ArrayList requiresPostDataChanged;
  94. private IPostBackEventHandler requiresRaiseEvent;
  95. private NameValueCollection secondPostData;
  96. private bool requiresPostBackScript;
  97. private bool postBackScriptRendered;
  98. bool handleViewState;
  99. string viewStateUserKey;
  100. NameValueCollection _requestValueCollection;
  101. string clientTarget;
  102. ClientScriptManager scriptManager;
  103. bool allow_load; // true when the Form collection belongs to this page (GetTypeHashCode)
  104. PageStatePersister page_state_persister;
  105. CultureInfo _appCulture;
  106. CultureInfo _appUICulture;
  107. // The initial context
  108. private HttpContext _context;
  109. // cached from the initial context
  110. HttpApplicationState _application;
  111. HttpResponse _response;
  112. HttpRequest _request;
  113. Cache _cache;
  114. HttpSessionState _session;
  115. [EditorBrowsable (EditorBrowsableState.Never)]
  116. #if NET_2_0
  117. public
  118. #else
  119. protected
  120. #endif
  121. const string postEventArgumentID = "__EVENTARGUMENT";
  122. [EditorBrowsable (EditorBrowsableState.Never)]
  123. #if NET_2_0
  124. public
  125. #else
  126. protected
  127. #endif
  128. const string postEventSourceID = "__EVENTTARGET";
  129. #if NET_2_0
  130. const string ScrollPositionXID = "__SCROLLPOSITIONX";
  131. const string ScrollPositionYID = "__SCROLLPOSITIONY";
  132. const string EnabledControlArrayID = "__enabledControlArray";
  133. #endif
  134. #if NET_2_0
  135. internal const string LastFocusID = "__LASTFOCUS";
  136. internal const string CallbackArgumentID = "__CALLBACKARGUMENT";
  137. internal const string CallbackSourceID = "__CALLBACKTARGET";
  138. internal const string PreviousPageID = "__PREVIOUSPAGE";
  139. HtmlHead htmlHeader;
  140. MasterPage masterPage;
  141. string masterPageFile;
  142. Page previousPage;
  143. bool isCrossPagePostBack;
  144. bool isPostBack;
  145. bool isCallback;
  146. ArrayList requireStateControls;
  147. HtmlForm _form;
  148. string _title;
  149. string _theme;
  150. string _styleSheetTheme;
  151. Hashtable items;
  152. bool _maintainScrollPositionOnPostBack;
  153. private bool asyncMode = false;
  154. private TimeSpan asyncTimeout;
  155. private const double DefaultAsyncTimeout = 45.0;
  156. private List<PageAsyncTask> parallelTasks;
  157. private List<PageAsyncTask> serialTasks;
  158. private ViewStateEncryptionMode viewStateEncryptionMode;
  159. private bool controlRegisteredForViewStateEncryption = false;
  160. #endif
  161. #region Constructor
  162. public Page ()
  163. {
  164. scriptManager = new ClientScriptManager (this);
  165. Page = this;
  166. ID = "__Page";
  167. #if NET_2_0
  168. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  169. if (ps != null) {
  170. asyncTimeout = ps.AsyncTimeout;
  171. viewStateEncryptionMode = ps.ViewStateEncryptionMode;
  172. } else {
  173. asyncTimeout = TimeSpan.FromSeconds (DefaultAsyncTimeout);
  174. viewStateEncryptionMode = ViewStateEncryptionMode.Auto;
  175. }
  176. #endif
  177. }
  178. #endregion
  179. #region Properties
  180. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  181. [Browsable (false)]
  182. public HttpApplicationState Application {
  183. get { return _application; }
  184. }
  185. [EditorBrowsable (EditorBrowsableState.Never)]
  186. protected bool AspCompatMode {
  187. #if NET_2_0
  188. get { return false; }
  189. #endif
  190. set { throw new NotImplementedException (); }
  191. }
  192. [EditorBrowsable (EditorBrowsableState.Never)]
  193. #if NET_2_0
  194. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  195. [BrowsableAttribute (false)]
  196. public bool Buffer {
  197. get { return Response.BufferOutput; }
  198. set { Response.BufferOutput = value; }
  199. }
  200. #else
  201. protected bool Buffer {
  202. set { Response.BufferOutput = value; }
  203. }
  204. #endif
  205. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  206. [Browsable (false)]
  207. public Cache Cache {
  208. get {
  209. if (_cache == null)
  210. throw new HttpException ("Cache is not available.");
  211. return _cache;
  212. }
  213. }
  214. #if NET_2_0
  215. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  216. #endif
  217. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  218. [Browsable (false), DefaultValue ("")]
  219. [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
  220. public string ClientTarget {
  221. get { return (clientTarget == null) ? "" : clientTarget; }
  222. set {
  223. clientTarget = value;
  224. if (value == "")
  225. clientTarget = null;
  226. }
  227. }
  228. [EditorBrowsable (EditorBrowsableState.Never)]
  229. #if NET_2_0
  230. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  231. [BrowsableAttribute (false)]
  232. public int CodePage {
  233. get { return Response.ContentEncoding.CodePage; }
  234. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  235. }
  236. #else
  237. protected int CodePage {
  238. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  239. }
  240. #endif
  241. [EditorBrowsable (EditorBrowsableState.Never)]
  242. #if NET_2_0
  243. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  244. [BrowsableAttribute (false)]
  245. public string ContentType {
  246. get { return Response.ContentType; }
  247. set { Response.ContentType = value; }
  248. }
  249. #else
  250. protected string ContentType {
  251. set { Response.ContentType = value; }
  252. }
  253. #endif
  254. protected override HttpContext Context {
  255. get {
  256. if (_context == null)
  257. return HttpContext.Current;
  258. return _context;
  259. }
  260. }
  261. #if NET_2_0
  262. [EditorBrowsable (EditorBrowsableState.Advanced)]
  263. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  264. [BrowsableAttribute (false)]
  265. public string Culture {
  266. get { return Thread.CurrentThread.CurrentCulture.Name; }
  267. set { Thread.CurrentThread.CurrentCulture = GetPageCulture (value, Thread.CurrentThread.CurrentCulture); }
  268. }
  269. #else
  270. [EditorBrowsable (EditorBrowsableState.Never)]
  271. protected string Culture {
  272. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  273. }
  274. #endif
  275. #if NET_2_0
  276. public virtual bool EnableEventValidation {
  277. get { return _eventValidation; }
  278. set {
  279. if (_lifeCycle > PageLifeCycle.Init)
  280. throw new InvalidOperationException ("The 'EnableEventValidation' property can be set only in the Page_init, the Page directive or in the <pages> configuration section.");
  281. _eventValidation = value;
  282. }
  283. }
  284. internal PageLifeCycle LifeCycle {
  285. get { return _lifeCycle; }
  286. }
  287. #endif
  288. [Browsable (false)]
  289. public override bool EnableViewState {
  290. get { return _viewState; }
  291. set { _viewState = value; }
  292. }
  293. #if NET_2_0
  294. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  295. [BrowsableAttribute (false)]
  296. #endif
  297. [EditorBrowsable (EditorBrowsableState.Never)]
  298. #if NET_2_0
  299. public
  300. #else
  301. protected
  302. #endif
  303. bool EnableViewStateMac {
  304. get { return _viewStateMac; }
  305. set { _viewStateMac = value; }
  306. }
  307. #if NET_1_1
  308. internal bool EnableViewStateMacInternal {
  309. get { return _viewStateMac; }
  310. set { _viewStateMac = value; }
  311. }
  312. #endif
  313. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  314. [Browsable (false), DefaultValue ("")]
  315. [WebSysDescription ("The URL of a page used for error redirection.")]
  316. public string ErrorPage {
  317. get { return _errorPage; }
  318. set {
  319. HttpContext ctx = Context;
  320. _errorPage = value;
  321. if (ctx != null)
  322. ctx.ErrorPage = value;
  323. }
  324. }
  325. #if NET_2_0
  326. [Obsolete]
  327. #endif
  328. [EditorBrowsable (EditorBrowsableState.Never)]
  329. protected ArrayList FileDependencies {
  330. set {
  331. if (Response != null)
  332. Response.AddFileDependencies (value);
  333. }
  334. }
  335. [Browsable (false)]
  336. #if NET_2_0
  337. [EditorBrowsable (EditorBrowsableState.Never)]
  338. #endif
  339. public override string ID {
  340. get { return base.ID; }
  341. set { base.ID = value; }
  342. }
  343. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  344. [Browsable (false)]
  345. public bool IsPostBack {
  346. get {
  347. #if NET_2_0
  348. return isPostBack;
  349. #else
  350. return _requestValueCollection != null;
  351. #endif
  352. }
  353. }
  354. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  355. public bool IsReusable {
  356. get { return false; }
  357. }
  358. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  359. [Browsable (false)]
  360. public bool IsValid {
  361. get {
  362. if (!is_validated)
  363. throw new HttpException (Locale.GetText ("Page hasn't been validated."));
  364. #if NET_2_0
  365. foreach (IValidator val in Validators)
  366. if (!val.IsValid)
  367. return false;
  368. return true;
  369. #else
  370. return ValidateCollection (_validators);
  371. #endif
  372. }
  373. }
  374. #if NET_2_0
  375. public IDictionary Items {
  376. get {
  377. if (items == null)
  378. items = new Hashtable ();
  379. return items;
  380. }
  381. }
  382. #endif
  383. [EditorBrowsable (EditorBrowsableState.Never)]
  384. #if NET_2_0
  385. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  386. [BrowsableAttribute (false)]
  387. public int LCID {
  388. get { return Thread.CurrentThread.CurrentCulture.LCID; }
  389. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  390. }
  391. #else
  392. protected int LCID {
  393. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  394. }
  395. #endif
  396. #if NET_2_0
  397. [Browsable (false)]
  398. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  399. public bool MaintainScrollPositionOnPostBack {
  400. get { return _maintainScrollPositionOnPostBack; }
  401. set { _maintainScrollPositionOnPostBack = value; }
  402. }
  403. #endif
  404. #if NET_2_0
  405. public PageAdapter PageAdapter {
  406. get {
  407. return (PageAdapter)Adapter;
  408. }
  409. }
  410. #endif
  411. #if !TARGET_J2EE
  412. internal string theForm {
  413. get {
  414. return "theForm";
  415. }
  416. }
  417. internal bool IsMultiForm {
  418. get { return false; }
  419. }
  420. #endif
  421. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  422. [Browsable (false)]
  423. public HttpRequest Request {
  424. get {
  425. if (_request == null)
  426. throw new HttpException("Request is not available in this context.");
  427. return _request;
  428. }
  429. }
  430. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  431. [Browsable (false)]
  432. public HttpResponse Response {
  433. get {
  434. if (_response == null)
  435. throw new HttpException ("Response is not available in this context.");
  436. return _response;
  437. }
  438. }
  439. [EditorBrowsable (EditorBrowsableState.Never)]
  440. #if NET_2_0
  441. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  442. [BrowsableAttribute (false)]
  443. public string ResponseEncoding {
  444. get { return Response.ContentEncoding.WebName; }
  445. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  446. }
  447. #else
  448. protected string ResponseEncoding {
  449. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  450. }
  451. #endif
  452. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  453. [Browsable (false)]
  454. public HttpServerUtility Server {
  455. get { return Context.Server; }
  456. }
  457. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  458. [Browsable (false)]
  459. public virtual HttpSessionState Session {
  460. get {
  461. if (_session != null)
  462. return _session;
  463. try {
  464. _session = Context.Session;
  465. } catch {
  466. // ignore, should not throw
  467. }
  468. if (_session == null)
  469. throw new HttpException ("Session state can only be used " +
  470. "when enableSessionState is set to true, either " +
  471. "in a configuration file or in the Page directive.");
  472. return _session;
  473. }
  474. }
  475. #if NET_2_0
  476. [FilterableAttribute (false)]
  477. [Obsolete]
  478. #endif
  479. [Browsable (false)]
  480. public bool SmartNavigation {
  481. get { return _smartNavigation; }
  482. set { _smartNavigation = value; }
  483. }
  484. #if NET_2_0
  485. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  486. [Filterable (false)]
  487. [Browsable (false)]
  488. public virtual string StyleSheetTheme {
  489. get { return _styleSheetTheme; }
  490. set { _styleSheetTheme = value; }
  491. }
  492. [Browsable (false)]
  493. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  494. public virtual string Theme {
  495. get { return _theme; }
  496. set { _theme = value; }
  497. }
  498. void InitializeStyleSheet ()
  499. {
  500. if (_styleSheetTheme == null) {
  501. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  502. if (ps != null)
  503. _styleSheetTheme = ps.StyleSheetTheme;
  504. }
  505. if (_styleSheetTheme != null && _styleSheetTheme != "")
  506. _styleSheetPageTheme = ThemeDirectoryCompiler.GetCompiledInstance (_styleSheetTheme, Context);
  507. }
  508. void InitializeTheme ()
  509. {
  510. if (_theme == null) {
  511. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  512. if (ps != null)
  513. _theme = ps.Theme;
  514. }
  515. if (_theme != null && _theme != "") {
  516. _pageTheme = ThemeDirectoryCompiler.GetCompiledInstance (_theme, Context);
  517. _pageTheme.SetPage (this);
  518. }
  519. }
  520. #endif
  521. #if NET_2_0
  522. [Localizable (true)]
  523. [Bindable (true)]
  524. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  525. public string Title {
  526. get {
  527. if (_title == null) {
  528. if (htmlHeader != null)
  529. return htmlHeader.Title;
  530. return String.Empty;
  531. }
  532. return _title;
  533. }
  534. set {
  535. if (htmlHeader != null)
  536. htmlHeader.Title = value;
  537. else
  538. _title = value;
  539. }
  540. }
  541. #endif
  542. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  543. [Browsable (false)]
  544. public TraceContext Trace {
  545. get { return Context.Trace; }
  546. }
  547. [EditorBrowsable (EditorBrowsableState.Never)]
  548. #if NET_2_0
  549. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  550. [BrowsableAttribute (false)]
  551. public bool TraceEnabled {
  552. get { return Trace.IsEnabled; }
  553. set { Trace.IsEnabled = value; }
  554. }
  555. #else
  556. protected bool TraceEnabled {
  557. set { Trace.IsEnabled = value; }
  558. }
  559. #endif
  560. [EditorBrowsable (EditorBrowsableState.Never)]
  561. #if NET_2_0
  562. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  563. [BrowsableAttribute (false)]
  564. public TraceMode TraceModeValue {
  565. get { return Trace.TraceMode; }
  566. set { Trace.TraceMode = value; }
  567. }
  568. #else
  569. protected TraceMode TraceModeValue {
  570. set { Trace.TraceMode = value; }
  571. }
  572. #endif
  573. [EditorBrowsable (EditorBrowsableState.Never)]
  574. protected int TransactionMode {
  575. #if NET_2_0
  576. get { return _transactionMode; }
  577. #endif
  578. set { _transactionMode = value; }
  579. }
  580. #if !NET_2_0
  581. //
  582. // This method is here just to remove the warning about "_transactionMode" not being
  583. // used. We probably will use it internally at some point.
  584. //
  585. internal int GetTransactionMode ()
  586. {
  587. return _transactionMode;
  588. }
  589. #endif
  590. #if NET_2_0
  591. [EditorBrowsable (EditorBrowsableState.Advanced)]
  592. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  593. [BrowsableAttribute (false)]
  594. public string UICulture {
  595. get { return Thread.CurrentThread.CurrentUICulture.Name; }
  596. set { Thread.CurrentThread.CurrentUICulture = GetPageCulture (value, Thread.CurrentThread.CurrentUICulture); }
  597. }
  598. #else
  599. [EditorBrowsable (EditorBrowsableState.Never)]
  600. protected string UICulture {
  601. set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
  602. }
  603. #endif
  604. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  605. [Browsable (false)]
  606. public IPrincipal User {
  607. get { return Context.User; }
  608. }
  609. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  610. [Browsable (false)]
  611. public ValidatorCollection Validators {
  612. get {
  613. if (_validators == null)
  614. _validators = new ValidatorCollection ();
  615. return _validators;
  616. }
  617. }
  618. [MonoTODO ("Use this when encrypting/decrypting ViewState")]
  619. [Browsable (false)]
  620. public string ViewStateUserKey {
  621. get { return viewStateUserKey; }
  622. set { viewStateUserKey = value; }
  623. }
  624. [Browsable (false)]
  625. public override bool Visible {
  626. get { return base.Visible; }
  627. set { base.Visible = value; }
  628. }
  629. #endregion
  630. #region Methods
  631. #if NET_2_0
  632. CultureInfo GetPageCulture (string culture, CultureInfo deflt)
  633. {
  634. if (culture == null)
  635. return deflt;
  636. CultureInfo ret = null;
  637. if (culture.StartsWith ("auto", StringComparison.InvariantCultureIgnoreCase)) {
  638. #if TARGET_J2EE
  639. //if (Context.IsPortletRequest)
  640. return deflt;
  641. #endif
  642. string[] languages = Request.UserLanguages;
  643. try {
  644. if (languages != null && languages.Length > 0)
  645. ret = CultureInfo.CreateSpecificCulture (languages[0]);
  646. } catch {
  647. }
  648. if (ret == null)
  649. ret = deflt;
  650. } else
  651. ret = CultureInfo.CreateSpecificCulture (culture);
  652. return ret;
  653. }
  654. #endif
  655. [EditorBrowsable (EditorBrowsableState.Never)]
  656. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  657. AsyncCallback cb,
  658. object extraData)
  659. {
  660. throw new NotImplementedException ();
  661. }
  662. [EditorBrowsable (EditorBrowsableState.Never)]
  663. protected void AspCompatEndProcessRequest (IAsyncResult result)
  664. {
  665. throw new NotImplementedException ();
  666. }
  667. [EditorBrowsable (EditorBrowsableState.Advanced)]
  668. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  669. {
  670. return new HtmlTextWriter (tw);
  671. }
  672. [EditorBrowsable (EditorBrowsableState.Never)]
  673. public void DesignerInitialize ()
  674. {
  675. InitRecursive (null);
  676. }
  677. [EditorBrowsable (EditorBrowsableState.Advanced)]
  678. protected virtual NameValueCollection DeterminePostBackMode ()
  679. {
  680. #if !TARGET_J2EE
  681. // if request was transfered from other page such Transfer
  682. #if NET_2_0
  683. if(!isCrossPagePostBack)
  684. #endif
  685. if (this != _context.Handler)
  686. return null;
  687. #endif
  688. HttpRequest req = Request;
  689. if (req == null)
  690. return null;
  691. NameValueCollection coll = null;
  692. if (0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture))
  693. coll = req.Form;
  694. else
  695. coll = req.QueryString;
  696. WebROCollection c = (WebROCollection) coll;
  697. allow_load = !c.GotID;
  698. if (allow_load)
  699. c.ID = GetTypeHashCode ();
  700. else
  701. allow_load = (c.ID == GetTypeHashCode ());
  702. if (coll != null && coll ["__VIEWSTATE"] == null && coll ["__EVENTTARGET"] == null)
  703. return null;
  704. #if TARGET_J2EE
  705. if (this != _context.Handler) {
  706. if (getFacesContext () != null) {
  707. // check if it is PreviousPage
  708. string prevViewId = coll [PreviousPageID];
  709. if (!String.IsNullOrEmpty (prevViewId)) {
  710. string appPath = VirtualPathUtility.RemoveTrailingSlash (Request.ApplicationPath);
  711. prevViewId = prevViewId.Substring (appPath.Length);
  712. isCrossPagePostBack = String.Compare (prevViewId, getFacesContext ().getExternalContext ().getRequestPathInfo (), StringComparison.OrdinalIgnoreCase) == 0;
  713. }
  714. }
  715. if (!isCrossPagePostBack)
  716. return null;
  717. }
  718. #endif
  719. return coll;
  720. }
  721. #if NET_2_0
  722. public override Control FindControl (string id) {
  723. #if TARGET_J2EE
  724. id = DecodeNamespace (id);
  725. #endif
  726. if (id == ID)
  727. return this;
  728. else
  729. return base.FindControl (id);
  730. }
  731. #endif
  732. #if NET_2_0
  733. [Obsolete]
  734. #endif
  735. [EditorBrowsable (EditorBrowsableState.Advanced)]
  736. public string GetPostBackClientEvent (Control control, string argument)
  737. {
  738. return scriptManager.GetPostBackEventReference (control, argument);
  739. }
  740. #if NET_2_0
  741. [Obsolete]
  742. #endif
  743. [EditorBrowsable (EditorBrowsableState.Advanced)]
  744. public string GetPostBackClientHyperlink (Control control, string argument)
  745. {
  746. return scriptManager.GetPostBackClientHyperlink (control, argument);
  747. }
  748. #if NET_2_0
  749. [Obsolete]
  750. #endif
  751. [EditorBrowsable (EditorBrowsableState.Advanced)]
  752. public string GetPostBackEventReference (Control control)
  753. {
  754. return scriptManager.GetPostBackEventReference (control, "");
  755. }
  756. #if NET_2_0
  757. [Obsolete]
  758. #endif
  759. [EditorBrowsable (EditorBrowsableState.Advanced)]
  760. public string GetPostBackEventReference (Control control, string argument)
  761. {
  762. return scriptManager.GetPostBackEventReference (control, argument);
  763. }
  764. internal void RequiresPostBackScript ()
  765. {
  766. #if NET_2_0
  767. if (requiresPostBackScript)
  768. return;
  769. ClientScript.RegisterHiddenField (postEventSourceID, String.Empty);
  770. ClientScript.RegisterHiddenField (postEventArgumentID, String.Empty);
  771. #endif
  772. requiresPostBackScript = true;
  773. }
  774. [EditorBrowsable (EditorBrowsableState.Never)]
  775. public virtual int GetTypeHashCode ()
  776. {
  777. return 0;
  778. }
  779. #if NET_2_0
  780. [MonoTODO("The following properties of OutputCacheParameters are silently ignored: CacheProfile, NoStore, SqlDependency")]
  781. protected internal virtual void InitOutputCache(OutputCacheParameters cacheSettings)
  782. {
  783. if (cacheSettings.Enabled)
  784. InitOutputCache(cacheSettings.Duration,
  785. cacheSettings.VaryByHeader,
  786. cacheSettings.VaryByCustom,
  787. cacheSettings.Location,
  788. cacheSettings.VaryByParam);
  789. }
  790. #endif
  791. [EditorBrowsable (EditorBrowsableState.Never)]
  792. protected virtual void InitOutputCache (int duration,
  793. string varyByHeader,
  794. string varyByCustom,
  795. OutputCacheLocation location,
  796. string varyByParam)
  797. {
  798. HttpCachePolicy cache = Response.Cache;
  799. bool set_vary = false;
  800. HttpContext ctx = Context;
  801. DateTime timestamp = ctx != null ? ctx.Timestamp : DateTime.Now;
  802. switch (location) {
  803. case OutputCacheLocation.Any:
  804. cache.SetCacheability (HttpCacheability.Public);
  805. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  806. cache.SetLastModified (timestamp);
  807. set_vary = true;
  808. break;
  809. case OutputCacheLocation.Client:
  810. cache.SetCacheability (HttpCacheability.Private);
  811. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  812. cache.SetLastModified (timestamp);
  813. break;
  814. case OutputCacheLocation.Downstream:
  815. cache.SetCacheability (HttpCacheability.Public);
  816. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  817. cache.SetLastModified (timestamp);
  818. break;
  819. case OutputCacheLocation.Server:
  820. cache.SetCacheability (HttpCacheability.Server);
  821. set_vary = true;
  822. break;
  823. case OutputCacheLocation.None:
  824. break;
  825. }
  826. if (set_vary) {
  827. if (varyByCustom != null)
  828. cache.SetVaryByCustom (varyByCustom);
  829. if (varyByParam != null && varyByParam.Length > 0) {
  830. string[] prms = varyByParam.Split (';');
  831. foreach (string p in prms)
  832. cache.VaryByParams [p.Trim ()] = true;
  833. cache.VaryByParams.IgnoreParams = false;
  834. } else {
  835. cache.VaryByParams.IgnoreParams = true;
  836. }
  837. if (varyByHeader != null && varyByHeader.Length > 0) {
  838. string[] hdrs = varyByHeader.Split (';');
  839. foreach (string h in hdrs)
  840. cache.VaryByHeaders [h.Trim ()] = true;
  841. }
  842. }
  843. cache.Duration = duration;
  844. cache.SetExpires (timestamp.AddSeconds (duration));
  845. }
  846. #if NET_2_0
  847. [Obsolete]
  848. #else
  849. [EditorBrowsable (EditorBrowsableState.Advanced)]
  850. #endif
  851. public bool IsClientScriptBlockRegistered (string key)
  852. {
  853. return scriptManager.IsClientScriptBlockRegistered (key);
  854. }
  855. #if NET_2_0
  856. [Obsolete]
  857. #else
  858. [EditorBrowsable (EditorBrowsableState.Advanced)]
  859. #endif
  860. public bool IsStartupScriptRegistered (string key)
  861. {
  862. return scriptManager.IsStartupScriptRegistered (key);
  863. }
  864. public string MapPath (string virtualPath)
  865. {
  866. return Request.MapPath (virtualPath);
  867. }
  868. #if NET_2_0
  869. protected internal override void Render (HtmlTextWriter writer) {
  870. if (MaintainScrollPositionOnPostBack) {
  871. ClientScript.RegisterWebFormClientScript ();
  872. ClientScript.RegisterHiddenField (ScrollPositionXID, Request [ScrollPositionXID]);
  873. ClientScript.RegisterHiddenField (ScrollPositionYID, Request [ScrollPositionYID]);
  874. string webForm = IsMultiForm ? theForm + "." : null;
  875. StringBuilder script = new StringBuilder ();
  876. script.AppendLine ("<script type=\"text/javascript\">");
  877. script.AppendLine (ClientScriptManager.SCRIPT_BLOCK_START);
  878. script.AppendLine (theForm + ".oldSubmit = " + theForm + ".submit;");
  879. script.AppendLine (theForm + ".submit = function () { " + webForm + "WebForm_SaveScrollPositionSubmit(); }");
  880. script.AppendLine (theForm + ".oldOnSubmit = " + theForm + ".onsubmit;");
  881. script.AppendLine (theForm + ".onsubmit = function () { " + webForm + "WebForm_SaveScrollPositionOnSubmit(); }");
  882. if (IsPostBack) {
  883. script.AppendLine (theForm + ".oldOnLoad = window.onload;");
  884. script.AppendLine ("window.onload = function () { " + webForm + "WebForm_RestoreScrollPosition (); };");
  885. }
  886. script.AppendLine (ClientScriptManager.SCRIPT_BLOCK_END);
  887. script.AppendLine ("</script>");
  888. ClientScript.RegisterStartupScript (typeof (Page), "MaintainScrollPositionOnPostBackStartup", script.ToString());
  889. }
  890. base.Render (writer);
  891. }
  892. #endif
  893. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  894. {
  895. #if ONLY_1_1
  896. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
  897. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
  898. #endif
  899. writer.WriteLine ();
  900. ClientScriptManager.WriteBeginScriptBlock (writer);
  901. #if ONLY_1_1
  902. RenderClientScriptFormDeclaration (writer, formUniqueID);
  903. #endif
  904. if (IsMultiForm) {
  905. writer.WriteLine ("{0}._form = {0};", theForm);
  906. writer.Write (theForm + ".");
  907. } else {
  908. writer.WriteLine ("window._form = {0};", theForm);
  909. }
  910. writer.WriteLine ("__doPostBack = function (eventTarget, eventArgument) {");
  911. #if NET_2_0
  912. writer.WriteLine ("\tif(this._form.onsubmit && this._form.onsubmit() == false) return;");
  913. #else
  914. writer.WriteLine ("\tif(document.ValidatorOnSubmit && !ValidatorOnSubmit()) return;");
  915. #endif
  916. writer.WriteLine ("\tthis._form.{0}.value = eventTarget;", postEventSourceID);
  917. writer.WriteLine ("\tthis._form.{0}.value = eventArgument;", postEventArgumentID);
  918. writer.WriteLine ("\tthis._form.submit();");
  919. writer.WriteLine ("}");
  920. ClientScriptManager.WriteEndScriptBlock (writer);
  921. }
  922. void RenderClientScriptFormDeclaration (HtmlTextWriter writer, string formUniqueID)
  923. {
  924. writer.WriteLine ("\tvar {0};\n\tif (document.getElementById) {{ {0} = document.getElementById ('{1}'); }}", theForm, formUniqueID);
  925. writer.WriteLine ("\telse {{ {0} = document.{1}; }}", theForm, formUniqueID);
  926. writer.WriteLine ("\t{0}._instanceVariableName = '{0}';", theForm);
  927. #if TARGET_J2EE
  928. string serverUrl = EncodeURL (Request.RawUrl);
  929. writer.WriteLine ("\t{0}.serverURL = {1};", theForm, ClientScriptManager.GetScriptLiteral (serverUrl));
  930. writer.WriteLine ("\twindow.TARGET_J2EE = true;");
  931. writer.WriteLine ("\twindow.IsMultiForm = {0};", IsMultiForm ? "true" : "false");
  932. #endif
  933. }
  934. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  935. {
  936. if (renderingForm)
  937. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  938. renderingForm = true;
  939. writer.WriteLine ();
  940. #if NET_2_0
  941. ClientScriptManager.WriteBeginScriptBlock (writer);
  942. RenderClientScriptFormDeclaration (writer, formUniqueID);
  943. ClientScriptManager.WriteEndScriptBlock (writer);
  944. #endif
  945. if (handleViewState)
  946. #if TARGET_J2EE
  947. if (getFacesContext () != null) {
  948. javax.faces.application.ViewHandler viewHandler = getFacesContext ().getApplication ().getViewHandler ();
  949. javax.faces.context.ResponseWriter oldFacesWriter = SetupResponseWriter (writer);
  950. try {
  951. viewHandler.writeState (getFacesContext ());
  952. }
  953. finally {
  954. getFacesContext ().setResponseWriter (oldFacesWriter);
  955. }
  956. }
  957. #endif
  958. scriptManager.RegisterHiddenField ("__VIEWSTATE", _savedViewState);
  959. scriptManager.WriteHiddenFields (writer);
  960. if (requiresPostBackScript) {
  961. RenderPostBackScript (writer, formUniqueID);
  962. postBackScriptRendered = true;
  963. }
  964. #if NET_2_0
  965. scriptManager.WriteWebFormClientScript (writer);
  966. #endif
  967. scriptManager.WriteClientScriptBlocks (writer);
  968. }
  969. internal IStateFormatter GetFormatter ()
  970. {
  971. return new ObjectStateFormatter (this);
  972. }
  973. internal string GetSavedViewState ()
  974. {
  975. return _savedViewState;
  976. }
  977. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  978. {
  979. #if NET_2_0
  980. scriptManager.SaveEventValidationState ();
  981. scriptManager.WriteExpandoAttributes (writer);
  982. #endif
  983. scriptManager.WriteHiddenFields (writer);
  984. if (!postBackScriptRendered && requiresPostBackScript)
  985. RenderPostBackScript (writer, formUniqueID);
  986. #if NET_2_0
  987. scriptManager.WriteWebFormClientScript (writer);
  988. #endif
  989. scriptManager.WriteArrayDeclares (writer);
  990. scriptManager.WriteStartupScriptBlocks (writer);
  991. renderingForm = false;
  992. postBackScriptRendered = false;
  993. }
  994. private void ProcessPostData (NameValueCollection data, bool second)
  995. {
  996. NameValueCollection requestValues = _requestValueCollection == null ?
  997. new NameValueCollection () :
  998. _requestValueCollection;
  999. if (data != null && data.Count > 0) {
  1000. Hashtable used = new Hashtable ();
  1001. foreach (string id in data.AllKeys){
  1002. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID ||
  1003. id == ClientScriptManager.EventStateFieldName)
  1004. continue;
  1005. if (used.ContainsKey (id))
  1006. continue;
  1007. used.Add (id, id);
  1008. Control ctrl = FindControl (id);
  1009. if (ctrl != null){
  1010. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  1011. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  1012. if (pbdh == null) {
  1013. if (pbeh != null)
  1014. RegisterRequiresRaiseEvent (pbeh);
  1015. continue;
  1016. }
  1017. if (pbdh.LoadPostData (id, requestValues) == true) {
  1018. if (requiresPostDataChanged == null)
  1019. requiresPostDataChanged = new ArrayList ();
  1020. requiresPostDataChanged.Add (pbdh);
  1021. }
  1022. if (_requiresPostBackCopy != null)
  1023. _requiresPostBackCopy.Remove (id);
  1024. } else if (!second) {
  1025. if (secondPostData == null)
  1026. secondPostData = new NameValueCollection ();
  1027. secondPostData.Add (id, data [id]);
  1028. }
  1029. }
  1030. }
  1031. ArrayList list1 = null;
  1032. if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
  1033. string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
  1034. foreach (string id in handlers) {
  1035. IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
  1036. if (pbdh != null) {
  1037. _requiresPostBackCopy.Remove (id);
  1038. if (pbdh.LoadPostData (id, requestValues)) {
  1039. if (requiresPostDataChanged == null)
  1040. requiresPostDataChanged = new ArrayList ();
  1041. requiresPostDataChanged.Add (pbdh);
  1042. }
  1043. } else if (!second) {
  1044. if (list1 == null)
  1045. list1 = new ArrayList ();
  1046. list1.Add (id);
  1047. }
  1048. }
  1049. }
  1050. _requiresPostBackCopy = second ? null : list1;
  1051. if (second)
  1052. secondPostData = null;
  1053. }
  1054. [EditorBrowsable (EditorBrowsableState.Never)]
  1055. #if NET_2_0
  1056. public virtual void ProcessRequest (HttpContext context)
  1057. #else
  1058. public void ProcessRequest (HttpContext context)
  1059. #endif
  1060. {
  1061. #if NET_2_0
  1062. _lifeCycle = PageLifeCycle.Unknown;
  1063. #endif
  1064. SetContext (context);
  1065. if (clientTarget != null)
  1066. Request.ClientTarget = clientTarget;
  1067. WireupAutomaticEvents ();
  1068. //-- Control execution lifecycle in the docs
  1069. // Save culture information because it can be modified in FrameworkInitialize()
  1070. _appCulture = Thread.CurrentThread.CurrentCulture;
  1071. _appUICulture = Thread.CurrentThread.CurrentUICulture;
  1072. FrameworkInitialize ();
  1073. context.ErrorPage = _errorPage;
  1074. try {
  1075. InternalProcessRequest ();
  1076. }
  1077. #if TARGET_J2EE
  1078. catch (Exception ex) {
  1079. HandleException (ex);
  1080. throw;
  1081. }
  1082. #else
  1083. catch (ThreadAbortException) {
  1084. // Do nothing, just ignore it by now.
  1085. } catch (Exception e) {
  1086. ProcessException (e);
  1087. throw;
  1088. }
  1089. finally {
  1090. ProcessUnload ();
  1091. }
  1092. #endif
  1093. }
  1094. void ProcessException (Exception ex) {
  1095. _context.AddError (ex); // OnError might access LastError
  1096. OnError (EventArgs.Empty);
  1097. _context.ClearError (ex);
  1098. // We want to remove that error, as we're rethrowing to stop
  1099. // further processing.
  1100. Trace.Warn ("Unhandled Exception", ex.ToString (), ex);
  1101. }
  1102. void ProcessUnload () {
  1103. try {
  1104. #if NET_2_0
  1105. _lifeCycle = PageLifeCycle.Unload;
  1106. #endif
  1107. RenderTrace ();
  1108. UnloadRecursive (true);
  1109. #if NET_2_0
  1110. _lifeCycle = PageLifeCycle.End;
  1111. #endif
  1112. } catch {}
  1113. #if TARGET_J2EE
  1114. if (getFacesContext () != null) {
  1115. if(IsCrossPagePostBack)
  1116. _context.Items [CrossPagePostBack] = this;
  1117. }
  1118. #endif
  1119. if (Thread.CurrentThread.CurrentCulture.Equals (_appCulture) == false)
  1120. Thread.CurrentThread.CurrentCulture = _appCulture;
  1121. if (Thread.CurrentThread.CurrentUICulture.Equals (_appUICulture) == false)
  1122. Thread.CurrentThread.CurrentUICulture = _appUICulture;
  1123. _appCulture = null;
  1124. _appUICulture = null;
  1125. }
  1126. #if NET_2_0
  1127. delegate void ProcessRequestDelegate (HttpContext context);
  1128. private sealed class DummyAsyncResult : IAsyncResult
  1129. {
  1130. readonly object state;
  1131. readonly WaitHandle asyncWaitHandle;
  1132. readonly bool completedSynchronously;
  1133. readonly bool isCompleted;
  1134. public DummyAsyncResult (bool isCompleted, bool completedSynchronously, object state)
  1135. {
  1136. this.isCompleted = isCompleted;
  1137. this.completedSynchronously = completedSynchronously;
  1138. this.state = state;
  1139. if (isCompleted) {
  1140. asyncWaitHandle = new ManualResetEvent (true);
  1141. }
  1142. else {
  1143. asyncWaitHandle = new ManualResetEvent (false);
  1144. }
  1145. }
  1146. #region IAsyncResult Members
  1147. public object AsyncState {
  1148. get { return state; }
  1149. }
  1150. public WaitHandle AsyncWaitHandle {
  1151. get { return asyncWaitHandle; }
  1152. }
  1153. public bool CompletedSynchronously {
  1154. get { return completedSynchronously; }
  1155. }
  1156. public bool IsCompleted {
  1157. get { return isCompleted; }
  1158. }
  1159. #endregion
  1160. }
  1161. protected IAsyncResult AsyncPageBeginProcessRequest (HttpContext context, AsyncCallback callback, object extraData)
  1162. {
  1163. ProcessRequest (context);
  1164. DummyAsyncResult asyncResult = new DummyAsyncResult (true, true, extraData);
  1165. if (callback != null) {
  1166. callback (asyncResult);
  1167. }
  1168. return asyncResult;
  1169. }
  1170. protected void AsyncPageEndProcessRequest (IAsyncResult result)
  1171. {
  1172. }
  1173. #endif
  1174. void InternalProcessRequest ()
  1175. {
  1176. _requestValueCollection = this.DeterminePostBackMode();
  1177. #if NET_2_0
  1178. // http://msdn2.microsoft.com/en-us/library/ms178141.aspx
  1179. if (_requestValueCollection != null) {
  1180. if (!isCrossPagePostBack && _requestValueCollection [PreviousPageID] != null && _requestValueCollection [PreviousPageID] != Request.FilePath) {
  1181. _doLoadPreviousPage = true;
  1182. }
  1183. else {
  1184. isCallback = _requestValueCollection [CallbackArgumentID] != null;
  1185. // LAMESPEC: on Callback IsPostBack is set to false, but true.
  1186. //isPostBack = !isCallback;
  1187. isPostBack = true;
  1188. }
  1189. string lastFocus = _requestValueCollection [LastFocusID];
  1190. if (!String.IsNullOrEmpty (lastFocus)) {
  1191. _focusedControlID = UniqueID2ClientID (lastFocus);
  1192. }
  1193. }
  1194. if (!isCrossPagePostBack) {
  1195. if (_context.PreviousHandler is Page) {
  1196. previousPage = (Page) _context.PreviousHandler;
  1197. }
  1198. }
  1199. _lifeCycle = PageLifeCycle.PreInit;
  1200. Trace.Write ("aspx.page", "Begin PreInit");
  1201. OnPreInit (EventArgs.Empty);
  1202. Trace.Write ("aspx.page", "End PreInit");
  1203. InitializeTheme ();
  1204. ApplyMasterPage ();
  1205. _lifeCycle = PageLifeCycle.Init;
  1206. #endif
  1207. Trace.Write ("aspx.page", "Begin Init");
  1208. InitRecursive (null);
  1209. Trace.Write ("aspx.page", "End Init");
  1210. #if NET_2_0
  1211. _lifeCycle = PageLifeCycle.InitComplete;
  1212. Trace.Write ("aspx.page", "Begin InitComplete");
  1213. OnInitComplete (EventArgs.Empty);
  1214. Trace.Write ("aspx.page", "End InitComplete");
  1215. #endif
  1216. renderingForm = false;
  1217. #if TARGET_J2EE
  1218. if (getFacesContext () != null) {
  1219. if (!(IsPostBack || IsCallback))
  1220. getFacesContext ().renderResponse ();
  1221. return;
  1222. }
  1223. #endif
  1224. RestorePageState ();
  1225. ProcessPostData ();
  1226. ProcessRaiseChangedEvents ();
  1227. ProcessRaisePostBackEvents ();
  1228. if (ProcessLoadComplete ())
  1229. return;
  1230. #if TARGET_J2EE
  1231. if (getFacesContext () != null)
  1232. return;
  1233. #endif
  1234. RenderPage ();
  1235. }
  1236. void RestorePageState () {
  1237. #if NET_2_0
  1238. if (IsPostBack || IsCallback) {
  1239. #else
  1240. if (IsPostBack) {
  1241. #endif
  1242. #if NET_2_0
  1243. _lifeCycle = PageLifeCycle.PreLoad;
  1244. if (_requestValueCollection != null)
  1245. scriptManager.RestoreEventValidationState (
  1246. _requestValueCollection [ClientScriptManager.EventStateFieldName]);
  1247. #endif
  1248. Trace.Write ("aspx.page", "Begin LoadViewState");
  1249. LoadPageViewState ();
  1250. Trace.Write ("aspx.page", "End LoadViewState");
  1251. }
  1252. }
  1253. void ProcessPostData () {
  1254. #if NET_2_0
  1255. //#if TARGET_J2EE
  1256. // if (!IsGetBack)
  1257. //#endif
  1258. if (IsPostBack || IsCallback) {
  1259. #else
  1260. if (IsPostBack) {
  1261. #endif
  1262. Trace.Write ("aspx.page", "Begin ProcessPostData");
  1263. ProcessPostData (_requestValueCollection, false);
  1264. Trace.Write ("aspx.page", "End ProcessPostData");
  1265. }
  1266. ProcessLoad ();
  1267. #if NET_2_0
  1268. //#if TARGET_J2EE
  1269. // if (IsGetBack)
  1270. // RestoreValidatorsState ();
  1271. // else
  1272. //#endif
  1273. if (IsPostBack || IsCallback) {
  1274. #else
  1275. if (IsPostBack) {
  1276. #endif
  1277. #if NET_2_0
  1278. _lifeCycle = PageLifeCycle.ControlEvents;
  1279. #endif
  1280. Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
  1281. ProcessPostData (secondPostData, true);
  1282. Trace.Write ("aspx.page", "End ProcessPostData Second Try");
  1283. }
  1284. }
  1285. void ProcessLoad () {
  1286. #if NET_2_0
  1287. Trace.Write ("aspx.page", "Begin PreLoad");
  1288. OnPreLoad (EventArgs.Empty);
  1289. Trace.Write ("aspx.page", "End PreLoad");
  1290. _lifeCycle = PageLifeCycle.Load;
  1291. #endif
  1292. Trace.Write ("aspx.page", "Begin Load");
  1293. LoadRecursive ();
  1294. Trace.Write ("aspx.page", "End Load");
  1295. }
  1296. void ProcessRaiseChangedEvents () {
  1297. #if NET_2_0
  1298. //#if TARGET_J2EE
  1299. // if (!IsGetBack)
  1300. //#endif
  1301. if (IsPostBack || IsCallback) {
  1302. #else
  1303. if (IsPostBack) {
  1304. #endif
  1305. Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
  1306. RaiseChangedEvents ();
  1307. Trace.Write ("aspx.page", "End Raise ChangedEvents");
  1308. }
  1309. }
  1310. void ProcessRaisePostBackEvents () {
  1311. #if NET_2_0
  1312. //#if TARGET_J2EE
  1313. // if (!IsGetBack)
  1314. //#endif
  1315. if (IsPostBack || IsCallback) {
  1316. #else
  1317. if (IsPostBack) {
  1318. #endif
  1319. Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
  1320. RaisePostBackEvents ();
  1321. Trace.Write ("aspx.page", "End Raise PostBackEvent");
  1322. }
  1323. }
  1324. bool ProcessLoadComplete() {
  1325. #if NET_2_0
  1326. _lifeCycle = PageLifeCycle.LoadComplete;
  1327. Trace.Write ("aspx.page", "Begin LoadComplete");
  1328. OnLoadComplete (EventArgs.Empty);
  1329. Trace.Write ("aspx.page", "End LoadComplete");
  1330. if (IsCrossPagePostBack)
  1331. return true;
  1332. if (IsCallback) {
  1333. #if TARGET_J2EE
  1334. if (getFacesContext () != null) {
  1335. _callbackTarget = GetCallbackTarget ();
  1336. ProcessRaiseCallbackEvent (_callbackTarget, ref _callbackEventError);
  1337. return true;
  1338. }
  1339. #endif
  1340. string result = ProcessCallbackData ();
  1341. HtmlTextWriter callbackOutput = new HtmlTextWriter (Response.Output);
  1342. callbackOutput.Write (result);
  1343. callbackOutput.Flush ();
  1344. return true;
  1345. }
  1346. _lifeCycle = PageLifeCycle.PreRender;
  1347. #endif
  1348. Trace.Write ("aspx.page", "Begin PreRender");
  1349. PreRenderRecursiveInternal ();
  1350. Trace.Write ("aspx.page", "End PreRender");
  1351. #if NET_2_0
  1352. ExecuteRegisteredAsyncTasks ();
  1353. _lifeCycle = PageLifeCycle.PreRenderComplete;
  1354. Trace.Write ("aspx.page", "Begin PreRenderComplete");
  1355. OnPreRenderComplete (EventArgs.Empty);
  1356. Trace.Write ("aspx.page", "End PreRenderComplete");
  1357. #endif
  1358. Trace.Write ("aspx.page", "Begin SaveViewState");
  1359. SavePageViewState ();
  1360. Trace.Write ("aspx.page", "End SaveViewState");
  1361. #if NET_2_0
  1362. _lifeCycle = PageLifeCycle.SaveStateComplete;
  1363. Trace.Write ("aspx.page", "Begin SaveStateComplete");
  1364. OnSaveStateComplete (EventArgs.Empty);
  1365. Trace.Write ("aspx.page", "End SaveStateComplete");
  1366. #endif // NET_2_0
  1367. return false;
  1368. }
  1369. internal void RenderPage () {
  1370. #if NET_2_0
  1371. _lifeCycle = PageLifeCycle.Render;
  1372. scriptManager.ResetEventValidationState ();
  1373. #endif
  1374. //--
  1375. Trace.Write ("aspx.page", "Begin Render");
  1376. HtmlTextWriter output = new HtmlTextWriter (Response.Output);
  1377. #if TARGET_J2EE
  1378. if (getFacesContext () != null)
  1379. SetupResponseWriter (output);
  1380. #endif
  1381. RenderControl (output);
  1382. Trace.Write ("aspx.page", "End Render");
  1383. }
  1384. void SetContext (HttpContext context) {
  1385. _context = context;
  1386. _application = context.Application;
  1387. _response = context.Response;
  1388. _request = context.Request;
  1389. _cache = context.Cache;
  1390. }
  1391. private void RenderTrace ()
  1392. {
  1393. TraceManager traceManager = HttpRuntime.TraceManager;
  1394. if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
  1395. return;
  1396. Trace.SaveData ();
  1397. if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
  1398. return;
  1399. if (!traceManager.LocalOnly || Context.Request.IsLocal) {
  1400. HtmlTextWriter output = new HtmlTextWriter (Response.Output);
  1401. Trace.Render (output);
  1402. }
  1403. }
  1404. #if NET_2_0
  1405. bool CheckForValidationSupport (Control targetControl)
  1406. {
  1407. if (targetControl == null)
  1408. return false;
  1409. Type type = targetControl.GetType ();
  1410. object[] attributes = type.GetCustomAttributes (false);
  1411. foreach (object attr in attributes)
  1412. if (attr is SupportsEventValidationAttribute)
  1413. return true;
  1414. return false;
  1415. }
  1416. #endif
  1417. void RaisePostBackEvents ()
  1418. {
  1419. #if NET_2_0
  1420. Control targetControl;
  1421. #endif
  1422. if (requiresRaiseEvent != null) {
  1423. RaisePostBackEvent (requiresRaiseEvent, null);
  1424. return;
  1425. }
  1426. NameValueCollection postdata = _requestValueCollection;
  1427. if (postdata == null)
  1428. return;
  1429. string eventTarget = postdata [postEventSourceID];
  1430. if (eventTarget == null || eventTarget.Length == 0) {
  1431. Validate ();
  1432. return;
  1433. }
  1434. #if NET_2_0
  1435. targetControl = FindControl (eventTarget);
  1436. IPostBackEventHandler target = targetControl as IPostBackEventHandler;
  1437. #else
  1438. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  1439. #endif
  1440. if (target == null)
  1441. return;
  1442. string eventArgument = postdata [postEventArgumentID];
  1443. RaisePostBackEvent (target, eventArgument);
  1444. }
  1445. internal void RaiseChangedEvents ()
  1446. {
  1447. if (requiresPostDataChanged == null)
  1448. return;
  1449. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  1450. ipdh.RaisePostDataChangedEvent ();
  1451. requiresPostDataChanged = null;
  1452. }
  1453. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1454. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  1455. {
  1456. #if NET_2_0
  1457. Control targetControl = sourceControl as Control;
  1458. if (targetControl != null && CheckForValidationSupport (targetControl))
  1459. scriptManager.ValidateEvent (targetControl.UniqueID, eventArgument);
  1460. #endif
  1461. sourceControl.RaisePostBackEvent (eventArgument);
  1462. }
  1463. #if NET_2_0
  1464. [Obsolete]
  1465. #endif
  1466. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1467. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  1468. {
  1469. scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
  1470. }
  1471. #if NET_2_0
  1472. [Obsolete]
  1473. #endif
  1474. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1475. public virtual void RegisterClientScriptBlock (string key, string script)
  1476. {
  1477. scriptManager.RegisterClientScriptBlock (key, script);
  1478. }
  1479. #if NET_2_0
  1480. [Obsolete]
  1481. #endif
  1482. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1483. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  1484. {
  1485. scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  1486. }
  1487. [MonoTODO("Not implemented, Used in HtmlForm")]
  1488. internal void RegisterClientScriptFile (string a, string b, string c)
  1489. {
  1490. throw new NotImplementedException ();
  1491. }
  1492. #if NET_2_0
  1493. [Obsolete]
  1494. #endif
  1495. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1496. public void RegisterOnSubmitStatement (string key, string script)
  1497. {
  1498. scriptManager.RegisterOnSubmitStatement (key, script);
  1499. }
  1500. internal string GetSubmitStatements ()
  1501. {
  1502. return scriptManager.WriteSubmitStatements ();
  1503. }
  1504. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1505. public void RegisterRequiresPostBack (Control control)
  1506. {
  1507. #if NET_2_0
  1508. if (!(control is IPostBackDataHandler))
  1509. throw new HttpException ("The control to register does not implement the IPostBackDataHandler interface.");
  1510. #endif
  1511. if (_requiresPostBack == null)
  1512. _requiresPostBack = new ArrayList ();
  1513. if (_requiresPostBack.Contains (control.UniqueID))
  1514. return;
  1515. _requiresPostBack.Add (control.UniqueID);
  1516. }
  1517. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1518. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  1519. {
  1520. requiresRaiseEvent = control;
  1521. }
  1522. #if NET_2_0
  1523. [Obsolete]
  1524. #endif
  1525. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1526. public virtual void RegisterStartupScript (string key, string script)
  1527. {
  1528. scriptManager.RegisterStartupScript (key, script);
  1529. }
  1530. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1531. public void RegisterViewStateHandler ()
  1532. {
  1533. handleViewState = true;
  1534. }
  1535. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1536. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  1537. {
  1538. PageStatePersister persister = this.PageStatePersister;
  1539. if (persister == null)
  1540. return;
  1541. Pair pair = viewState as Pair;
  1542. if (pair != null) {
  1543. persister.ViewState = pair.First;
  1544. persister.ControlState = pair.Second;
  1545. } else
  1546. persister.ViewState = viewState;
  1547. persister.Save ();
  1548. }
  1549. internal string RawViewState {
  1550. get {
  1551. NameValueCollection postdata = _requestValueCollection;
  1552. string view_state;
  1553. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  1554. return null;
  1555. if (view_state == "")
  1556. return null;
  1557. return view_state;
  1558. }
  1559. set { _savedViewState = value; }
  1560. }
  1561. #if NET_2_0
  1562. protected virtual
  1563. #else
  1564. internal
  1565. #endif
  1566. PageStatePersister PageStatePersister {
  1567. get {
  1568. if (page_state_persister == null)
  1569. #if TARGET_J2EE
  1570. if (getFacesContext () != null)
  1571. return new FacesPageStatePersister (this);
  1572. else
  1573. #endif
  1574. page_state_persister = new HiddenFieldPageStatePersister (this);
  1575. return page_state_persister;
  1576. }
  1577. }
  1578. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1579. protected virtual object LoadPageStateFromPersistenceMedium ()
  1580. {
  1581. PageStatePersister persister = this.PageStatePersister;
  1582. if (persister == null)
  1583. return null;
  1584. persister.Load ();
  1585. return new Pair (persister.ViewState, persister.ControlState);
  1586. }
  1587. internal void LoadPageViewState()
  1588. {
  1589. Pair sState = LoadPageStateFromPersistenceMedium () as Pair;
  1590. if (sState != null) {
  1591. if (allow_load) {
  1592. #if NET_2_0
  1593. LoadPageControlState (sState.Second);
  1594. #endif
  1595. Pair vsr = sState.First as Pair;
  1596. if (vsr != null) {
  1597. LoadViewStateRecursive (vsr.First);
  1598. _requiresPostBackCopy = vsr.Second as ArrayList;
  1599. }
  1600. }
  1601. }
  1602. }
  1603. internal void SavePageViewState ()
  1604. {
  1605. if (!handleViewState)
  1606. return;
  1607. #if NET_2_0
  1608. object controlState = SavePageControlState ();
  1609. #endif
  1610. object viewState = SaveViewStateRecursive ();
  1611. object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
  1612. Pair vsr = null;
  1613. if (viewState != null || reqPostback != null)
  1614. vsr = new Pair (viewState, reqPostback);
  1615. Pair pair = new Pair ();
  1616. pair.First = vsr;
  1617. #if NET_2_0
  1618. pair.Second = controlState;
  1619. #else
  1620. pair.Second = null;
  1621. #endif
  1622. if (pair.First == null && pair.Second == null)
  1623. SavePageStateToPersistenceMedium (null);
  1624. else
  1625. SavePageStateToPersistenceMedium (pair);
  1626. }
  1627. public virtual void Validate ()
  1628. {
  1629. is_validated = true;
  1630. ValidateCollection (_validators);
  1631. }
  1632. #if NET_2_0
  1633. internal bool AreValidatorsUplevel () {
  1634. return AreValidatorsUplevel (String.Empty);
  1635. }
  1636. internal bool AreValidatorsUplevel (string valGroup)
  1637. #else
  1638. internal virtual bool AreValidatorsUplevel ()
  1639. #endif
  1640. {
  1641. bool uplevel = false;
  1642. foreach (IValidator v in Validators) {
  1643. BaseValidator bv = v as BaseValidator;
  1644. if (bv == null) continue;
  1645. #if NET_2_0
  1646. if (valGroup != bv.ValidationGroup)
  1647. continue;
  1648. #endif
  1649. if (bv.GetRenderUplevel()) {
  1650. uplevel = true;
  1651. break;
  1652. }
  1653. }
  1654. return uplevel;
  1655. }
  1656. bool ValidateCollection (ValidatorCollection validators)
  1657. {
  1658. #if NET_2_0
  1659. if (!_eventValidation)
  1660. return true;
  1661. #endif
  1662. if (validators == null || validators.Count == 0)
  1663. return true;
  1664. bool all_valid = true;
  1665. foreach (IValidator v in validators){
  1666. v.Validate ();
  1667. if (v.IsValid == false)
  1668. all_valid = false;
  1669. }
  1670. return all_valid;
  1671. }
  1672. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1673. public virtual void VerifyRenderingInServerForm (Control control)
  1674. {
  1675. if (Context == null)
  1676. return;
  1677. #if NET_2_0
  1678. if (IsCallback)
  1679. return;
  1680. #endif
  1681. if (!renderingForm)
  1682. throw new HttpException ("Control '" +
  1683. control.ClientID +
  1684. "' of type '" +
  1685. control.GetType ().Name +
  1686. "' must be placed inside a form tag with runat=server.");
  1687. }
  1688. protected override void FrameworkInitialize ()
  1689. {
  1690. base.FrameworkInitialize ();
  1691. #if NET_2_0
  1692. InitializeStyleSheet ();
  1693. #endif
  1694. }
  1695. #endregion
  1696. #if NET_2_0
  1697. public
  1698. #else
  1699. internal
  1700. #endif
  1701. ClientScriptManager ClientScript {
  1702. get { return scriptManager; }
  1703. }
  1704. #if NET_2_0
  1705. static readonly object InitCompleteEvent = new object ();
  1706. static readonly object LoadCompleteEvent = new object ();
  1707. static readonly object PreInitEvent = new object ();
  1708. static readonly object PreLoadEvent = new object ();
  1709. static readonly object PreRenderCompleteEvent = new object ();
  1710. static readonly object SaveStateCompleteEvent = new object ();
  1711. int event_mask;
  1712. const int initcomplete_mask = 1;
  1713. const int loadcomplete_mask = 1 << 1;
  1714. const int preinit_mask = 1 << 2;
  1715. const int preload_mask = 1 << 3;
  1716. const int prerendercomplete_mask = 1 << 4;
  1717. const int savestatecomplete_mask = 1 << 5;
  1718. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1719. public event EventHandler InitComplete {
  1720. add {
  1721. event_mask |= initcomplete_mask;
  1722. Events.AddHandler (InitCompleteEvent, value);
  1723. }
  1724. remove { Events.RemoveHandler (InitCompleteEvent, value); }
  1725. }
  1726. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1727. public event EventHandler LoadComplete {
  1728. add {
  1729. event_mask |= loadcomplete_mask;
  1730. Events.AddHandler (LoadCompleteEvent, value);
  1731. }
  1732. remove { Events.RemoveHandler (LoadCompleteEvent, value); }
  1733. }
  1734. public event EventHandler PreInit {
  1735. add {
  1736. event_mask |= preinit_mask;
  1737. Events.AddHandler (PreInitEvent, value);
  1738. }
  1739. remove { Events.RemoveHandler (PreInitEvent, value); }
  1740. }
  1741. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1742. public event EventHandler PreLoad {
  1743. add {
  1744. event_mask |= preload_mask;
  1745. Events.AddHandler (PreLoadEvent, value);
  1746. }
  1747. remove { Events.RemoveHandler (PreLoadEvent, value); }
  1748. }
  1749. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1750. public event EventHandler PreRenderComplete {
  1751. add {
  1752. event_mask |= prerendercomplete_mask;
  1753. Events.AddHandler (PreRenderCompleteEvent, value);
  1754. }
  1755. remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
  1756. }
  1757. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1758. public event EventHandler SaveStateComplete {
  1759. add {
  1760. event_mask |= savestatecomplete_mask;
  1761. Events.AddHandler (SaveStateCompleteEvent, value);
  1762. }
  1763. remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
  1764. }
  1765. protected virtual void OnInitComplete (EventArgs e)
  1766. {
  1767. if ((event_mask & initcomplete_mask) != 0) {
  1768. EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
  1769. if (eh != null) eh (this, e);
  1770. }
  1771. }
  1772. protected virtual void OnLoadComplete (EventArgs e)
  1773. {
  1774. if ((event_mask & loadcomplete_mask) != 0) {
  1775. EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
  1776. if (eh != null) eh (this, e);
  1777. }
  1778. }
  1779. protected virtual void OnPreInit (EventArgs e)
  1780. {
  1781. if ((event_mask & preinit_mask) != 0) {
  1782. EventHandler eh = (EventHandler) (Events [PreInitEvent]);
  1783. if (eh != null) eh (this, e);
  1784. }
  1785. }
  1786. protected virtual void OnPreLoad (EventArgs e)
  1787. {
  1788. if ((event_mask & preload_mask) != 0) {
  1789. EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
  1790. if (eh != null) eh (this, e);
  1791. }
  1792. }
  1793. protected virtual void OnPreRenderComplete (EventArgs e)
  1794. {
  1795. if ((event_mask & prerendercomplete_mask) != 0) {
  1796. EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
  1797. if (eh != null) eh (this, e);
  1798. }
  1799. if (Form == null)
  1800. return;
  1801. if (!Form.DetermineRenderUplevel ())
  1802. return;
  1803. string defaultButtonId = Form.DefaultButton;
  1804. /* figure out if we have some control we're going to focus */
  1805. if (String.IsNullOrEmpty (_focusedControlID)) {
  1806. _focusedControlID = Form.DefaultFocus;
  1807. if (String.IsNullOrEmpty (_focusedControlID))
  1808. _focusedControlID = defaultButtonId;
  1809. }
  1810. if (!String.IsNullOrEmpty (_focusedControlID)) {
  1811. ClientScript.RegisterWebFormClientScript ();
  1812. ClientScript.RegisterStartupScript (
  1813. "HtmlForm-DefaultButton-StartupScript",
  1814. String.Format (
  1815. "<script type=\"text/javascript\">\n{0}\nWebForm_AutoFocus('{1}');\n{2}\n</script>\n",
  1816. ClientScriptManager.SCRIPT_BLOCK_START,
  1817. _focusedControlID,
  1818. ClientScriptManager.SCRIPT_BLOCK_END)
  1819. );
  1820. }
  1821. if (Form.SubmitDisabledControls && _hasEnabledControlArray) {
  1822. ClientScript.RegisterWebFormClientScript ();
  1823. ClientScript.RegisterOnSubmitStatement ("HtmlForm-SubmitDisabledControls-SubmitStatement",
  1824. "WebForm_ReEnableControls(this);");
  1825. }
  1826. }
  1827. internal void RegisterEnabledControl (Control control)
  1828. {
  1829. if (Form == null || !Page.Form.SubmitDisabledControls || !Page.Form.DetermineRenderUplevel ())
  1830. return;
  1831. _hasEnabledControlArray = true;
  1832. Page.ClientScript.RegisterArrayDeclaration (EnabledControlArrayID, String.Format ("'{0}'", control.ClientID));
  1833. }
  1834. protected virtual void OnSaveStateComplete (EventArgs e)
  1835. {
  1836. if ((event_mask & savestatecomplete_mask) != 0) {
  1837. EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
  1838. if (eh != null) eh (this, e);
  1839. }
  1840. }
  1841. public HtmlForm Form {
  1842. get { return _form; }
  1843. }
  1844. internal void RegisterForm (HtmlForm form)
  1845. {
  1846. _form = form;
  1847. }
  1848. [BrowsableAttribute (false)]
  1849. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1850. public Page PreviousPage {
  1851. get {
  1852. if (_doLoadPreviousPage) {
  1853. _doLoadPreviousPage = false;
  1854. LoadPreviousPageReference ();
  1855. }
  1856. return previousPage;
  1857. }
  1858. }
  1859. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1860. [BrowsableAttribute (false)]
  1861. public bool IsCallback {
  1862. get { return isCallback; }
  1863. }
  1864. [BrowsableAttribute (false)]
  1865. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1866. public bool IsCrossPagePostBack {
  1867. get { return isCrossPagePostBack; }
  1868. }
  1869. public new virtual char IdSeparator {
  1870. get {
  1871. //TODO: why override?
  1872. return base.IdSeparator;
  1873. }
  1874. }
  1875. string ProcessCallbackData ()
  1876. {
  1877. ICallbackEventHandler target = GetCallbackTarget ();
  1878. string callbackEventError = String.Empty;
  1879. ProcessRaiseCallbackEvent (target, ref callbackEventError);
  1880. return ProcessGetCallbackResult (target, callbackEventError);
  1881. }
  1882. ICallbackEventHandler GetCallbackTarget () {
  1883. string callbackTarget = _requestValueCollection [CallbackSourceID];
  1884. if (callbackTarget == null || callbackTarget.Length == 0)
  1885. throw new HttpException ("Callback target not provided.");
  1886. Control targetControl = FindControl (callbackTarget);
  1887. ICallbackEventHandler target = targetControl as ICallbackEventHandler;
  1888. if (target == null)
  1889. throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
  1890. return target;
  1891. }
  1892. void ProcessRaiseCallbackEvent (ICallbackEventHandler target, ref string callbackEventError) {
  1893. string callbackArgument = _requestValueCollection [CallbackArgumentID];
  1894. try {
  1895. target.RaiseCallbackEvent (callbackArgument);
  1896. }
  1897. catch (Exception ex) {
  1898. callbackEventError = String.Format ("e{0}", ex.Message);
  1899. }
  1900. }
  1901. string ProcessGetCallbackResult (ICallbackEventHandler target, string callbackEventError) {
  1902. string callBackResult;
  1903. try {
  1904. callBackResult = target.GetCallbackResult ();
  1905. }
  1906. catch (Exception ex) {
  1907. return String.Format ("e{0}", ex.Message);
  1908. }
  1909. string eventValidation = ClientScript.GetEventValidationStateFormatted ();
  1910. return String.Format ("{0}{1}|{2}{3}", callbackEventError, eventValidation == null ? 0 : eventValidation.Length, eventValidation, callBackResult);
  1911. }
  1912. [BrowsableAttribute (false)]
  1913. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1914. public HtmlHead Header {
  1915. get { return htmlHeader; }
  1916. }
  1917. internal void SetHeader (HtmlHead header)
  1918. {
  1919. htmlHeader = header;
  1920. if (_title != null) {
  1921. htmlHeader.Title = _title;
  1922. _title = null;
  1923. }
  1924. }
  1925. protected bool AsyncMode {
  1926. get { return asyncMode; }
  1927. set { asyncMode = value; }
  1928. }
  1929. public TimeSpan AsyncTimeout {
  1930. get { return asyncTimeout; }
  1931. set { asyncTimeout = value; }
  1932. }
  1933. public bool IsAsync {
  1934. get { return AsyncMode; }
  1935. }
  1936. [MonoTODO ("Not Implemented")]
  1937. protected internal virtual string UniqueFilePathSuffix {
  1938. get {
  1939. throw new NotImplementedException ();
  1940. }
  1941. }
  1942. [MonoTODO ("Not Implemented")]
  1943. public int MaxPageStateFieldLength {
  1944. get {
  1945. throw new NotImplementedException ();
  1946. }
  1947. set {
  1948. throw new NotImplementedException ();
  1949. }
  1950. }
  1951. public void AddOnPreRenderCompleteAsync (BeginEventHandler beginHandler, EndEventHandler endHandler)
  1952. {
  1953. AddOnPreRenderCompleteAsync (beginHandler, endHandler, null);
  1954. }
  1955. public void AddOnPreRenderCompleteAsync (BeginEventHandler beginHandler, EndEventHandler endHandler, Object state)
  1956. {
  1957. if (!IsAsync) {
  1958. throw new InvalidOperationException ("AddOnPreRenderCompleteAsync called and Page.IsAsync == false");
  1959. }
  1960. if (_lifeCycle >= PageLifeCycle.PreRender) {
  1961. throw new InvalidOperationException ("AddOnPreRenderCompleteAsync can only be called before PreRender.");
  1962. }
  1963. if (beginHandler == null) {
  1964. throw new ArgumentNullException ("beginHandler");
  1965. }
  1966. if (endHandler == null) {
  1967. throw new ArgumentNullException ("endHandler");
  1968. }
  1969. RegisterAsyncTask (new PageAsyncTask (beginHandler, endHandler, null, state, false));
  1970. }
  1971. private List<PageAsyncTask> ParallelTasks {
  1972. get {
  1973. if (parallelTasks == null) {
  1974. parallelTasks = new List<PageAsyncTask>();
  1975. }
  1976. return parallelTasks;
  1977. }
  1978. }
  1979. private List<PageAsyncTask> SerialTasks {
  1980. get {
  1981. if (serialTasks == null) {
  1982. serialTasks = new List<PageAsyncTask> ();
  1983. }
  1984. return serialTasks;
  1985. }
  1986. }
  1987. public void RegisterAsyncTask (PageAsyncTask task)
  1988. {
  1989. if (task == null) {
  1990. throw new ArgumentNullException ("task");
  1991. }
  1992. if (task.ExecuteInParallel) {
  1993. ParallelTasks.Add (task);
  1994. }
  1995. else {
  1996. SerialTasks.Add (task);
  1997. }
  1998. }
  1999. public void ExecuteRegisteredAsyncTasks ()
  2000. {
  2001. if ((parallelTasks == null || parallelTasks.Count == 0) &&
  2002. (serialTasks == null || serialTasks.Count == 0)){
  2003. return;
  2004. }
  2005. if (parallelTasks != null) {
  2006. DateTime startExecution = DateTime.Now;
  2007. List<PageAsyncTask> localParallelTasks = parallelTasks;
  2008. parallelTasks = null; // Shouldn't execute tasks twice
  2009. List<IAsyncResult> asyncResults = new List<IAsyncResult>();
  2010. foreach (PageAsyncTask parallelTask in localParallelTasks) {
  2011. IAsyncResult result = parallelTask.BeginHandler (this, EventArgs.Empty, new AsyncCallback (EndAsyncTaskCallback), parallelTask.State);
  2012. if (result.CompletedSynchronously) {
  2013. parallelTask.EndHandler (result);
  2014. }
  2015. else {
  2016. asyncResults.Add (result);
  2017. }
  2018. }
  2019. if (asyncResults.Count > 0) {
  2020. #if TARGET_JVM
  2021. TimeSpan timeout = AsyncTimeout;
  2022. long t1 = DateTime.Now.Ticks;
  2023. bool signalled = true;
  2024. for (int i = 0; i < asyncResults.Count; i++) {
  2025. if (asyncResults [i].IsCompleted)
  2026. continue;
  2027. if (signalled)
  2028. signalled = asyncResults [i].AsyncWaitHandle.WaitOne (timeout, false);
  2029. if (signalled) {
  2030. long t2 = DateTime.Now.Ticks;
  2031. timeout = AsyncTimeout - TimeSpan.FromTicks (t2 - t1);
  2032. if (timeout.Ticks <= 0)
  2033. signalled = false;
  2034. }
  2035. else {
  2036. localParallelTasks [i].TimeoutHandler (asyncResults [i]);
  2037. }
  2038. }
  2039. #else
  2040. WaitHandle [] waitArray = new WaitHandle [asyncResults.Count];
  2041. int i = 0;
  2042. for (i = 0; i < asyncResults.Count; i++) {
  2043. waitArray [i] = asyncResults [i].AsyncWaitHandle;
  2044. }
  2045. bool allSignalled = WaitHandle.WaitAll (waitArray, AsyncTimeout, false);
  2046. if (!allSignalled) {
  2047. for (i = 0; i < asyncResults.Count; i++) {
  2048. if (!asyncResults [i].IsCompleted) {
  2049. localParallelTasks [i].TimeoutHandler (asyncResults [i]);
  2050. }
  2051. }
  2052. }
  2053. #endif
  2054. }
  2055. DateTime endWait = DateTime.Now;
  2056. TimeSpan elapsed = endWait - startExecution;
  2057. if (elapsed <= AsyncTimeout) {
  2058. AsyncTimeout -= elapsed;
  2059. }
  2060. else {
  2061. AsyncTimeout = TimeSpan.FromTicks(0);
  2062. }
  2063. }
  2064. if (serialTasks != null) {
  2065. List<PageAsyncTask> localSerialTasks = serialTasks;
  2066. serialTasks = null; // Shouldn't execute tasks twice
  2067. foreach (PageAsyncTask serialTask in localSerialTasks) {
  2068. DateTime startExecution = DateTime.Now;
  2069. IAsyncResult result = serialTask.BeginHandler (this, EventArgs.Empty, new AsyncCallback (EndAsyncTaskCallback), serialTask);
  2070. if (result.CompletedSynchronously) {
  2071. serialTask.EndHandler (result);
  2072. }
  2073. else {
  2074. bool done = result.AsyncWaitHandle.WaitOne (AsyncTimeout, false);
  2075. if (!done && !result.IsCompleted) {
  2076. serialTask.TimeoutHandler (result);
  2077. }
  2078. }
  2079. DateTime endWait = DateTime.Now;
  2080. TimeSpan elapsed = endWait - startExecution;
  2081. if (elapsed <= AsyncTimeout) {
  2082. AsyncTimeout -= elapsed;
  2083. }
  2084. else {
  2085. AsyncTimeout = TimeSpan.FromTicks (0);
  2086. }
  2087. }
  2088. }
  2089. AsyncTimeout = TimeSpan.FromSeconds (DefaultAsyncTimeout);
  2090. }
  2091. void EndAsyncTaskCallback (IAsyncResult result)
  2092. {
  2093. PageAsyncTask task = (PageAsyncTask)result.AsyncState;
  2094. task.EndHandler (result);
  2095. }
  2096. public static HtmlTextWriter CreateHtmlTextWriterFromType (TextWriter tw, Type writerType)
  2097. {
  2098. Type htmlTextWriterType = typeof (HtmlTextWriter);
  2099. if (!htmlTextWriterType.IsAssignableFrom (writerType)) {
  2100. throw new HttpException (String.Format ("Type '{0}' cannot be assigned to HtmlTextWriter", writerType.FullName));
  2101. }
  2102. ConstructorInfo constructor = writerType.GetConstructor (new Type [] { typeof (TextWriter) });
  2103. if (constructor == null) {
  2104. throw new HttpException (String.Format ("Type '{0}' does not have a consturctor that takes a TextWriter as parameter", writerType.FullName));
  2105. }
  2106. return (HtmlTextWriter) Activator.CreateInstance(writerType, tw);
  2107. }
  2108. public ViewStateEncryptionMode ViewStateEncryptionMode {
  2109. get { return viewStateEncryptionMode; }
  2110. set { viewStateEncryptionMode = value; }
  2111. }
  2112. public void RegisterRequiresViewStateEncryption ()
  2113. {
  2114. controlRegisteredForViewStateEncryption = true;
  2115. }
  2116. private static byte [] AES_IV = null;
  2117. private static byte [] TripleDES_IV = null;
  2118. private static object locker = new object ();
  2119. private static bool isEncryptionInitialized = false;
  2120. private static void InitializeEncryption ()
  2121. {
  2122. if (isEncryptionInitialized) {
  2123. return;
  2124. }
  2125. lock (locker) {
  2126. if (isEncryptionInitialized) {
  2127. return;
  2128. }
  2129. string iv_string = "0BA48A9E-736D-40f8-954B-B2F62241F282";
  2130. AES_IV = new byte [16];
  2131. TripleDES_IV = new byte [8];
  2132. int i;
  2133. for (i = 0; i < AES_IV.Length; i++) {
  2134. AES_IV [i] = (byte) iv_string [i];
  2135. }
  2136. for (i = 0; i < TripleDES_IV.Length; i++) {
  2137. TripleDES_IV [i] = (byte) iv_string [i];
  2138. }
  2139. isEncryptionInitialized = true;
  2140. }
  2141. }
  2142. internal ICryptoTransform GetCryptoTransform (CryptoStreamMode cryptoStreamMode)
  2143. {
  2144. ICryptoTransform transform = null;
  2145. MachineKeySection config = (MachineKeySection) WebConfigurationManager.GetSection (machineKeyConfigPath);
  2146. byte [] vk = MachineKeySectionUtils.ValidationKeyBytes (config);
  2147. switch (config.Validation) {
  2148. case MachineKeyValidation.SHA1:
  2149. transform = SHA1.Create ();
  2150. break;
  2151. case MachineKeyValidation.MD5:
  2152. transform = MD5.Create ();
  2153. break;
  2154. case MachineKeyValidation.AES:
  2155. if (cryptoStreamMode == CryptoStreamMode.Read){
  2156. transform = Rijndael.Create().CreateDecryptor(vk, AES_IV);
  2157. } else {
  2158. transform = Rijndael.Create().CreateEncryptor(vk, AES_IV);
  2159. }
  2160. break;
  2161. case MachineKeyValidation.TripleDES:
  2162. if (cryptoStreamMode == CryptoStreamMode.Read){
  2163. transform = TripleDES.Create().CreateDecryptor(vk, TripleDES_IV);
  2164. } else {
  2165. transform = TripleDES.Create().CreateEncryptor(vk, TripleDES_IV);
  2166. }
  2167. break;
  2168. }
  2169. return transform;
  2170. }
  2171. internal bool NeedViewStateEncryption {
  2172. get {
  2173. return (ViewStateEncryptionMode == ViewStateEncryptionMode.Always ||
  2174. (ViewStateEncryptionMode == ViewStateEncryptionMode.Auto &&
  2175. controlRegisteredForViewStateEncryption));
  2176. }
  2177. }
  2178. void ApplyMasterPage ()
  2179. {
  2180. if (masterPageFile != null && masterPageFile.Length > 0) {
  2181. ArrayList appliedMasterPageFiles = new ArrayList ();
  2182. if (Master != null) {
  2183. MasterPage.ApplyMasterPageRecursive (Master, appliedMasterPageFiles);
  2184. Master.Page = this;
  2185. Controls.Clear ();
  2186. Controls.Add (Master);
  2187. }
  2188. }
  2189. }
  2190. [DefaultValueAttribute ("")]
  2191. public virtual string MasterPageFile {
  2192. get { return masterPageFile; }
  2193. set {
  2194. masterPageFile = value;
  2195. masterPage = null;
  2196. }
  2197. }
  2198. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  2199. [BrowsableAttribute (false)]
  2200. public MasterPage Master {
  2201. get {
  2202. if (Context == null || String.IsNullOrEmpty (masterPageFile))
  2203. return null;
  2204. if (masterPage == null)
  2205. masterPage = MasterPage.CreateMasterPage (this, Context, masterPageFile, contentTemplates);
  2206. return masterPage;
  2207. }
  2208. }
  2209. public void SetFocus (string clientID)
  2210. {
  2211. if (String.IsNullOrEmpty (clientID))
  2212. throw new ArgumentNullException ("control");
  2213. if (_lifeCycle > PageLifeCycle.PreRender)
  2214. throw new InvalidOperationException ("SetFocus can only be called before and during PreRender.");
  2215. if(Form==null)
  2216. throw new InvalidOperationException ("A form tag with runat=server must exist on the Page to use SetFocus() or the Focus property.");
  2217. _focusedControlID = clientID;
  2218. }
  2219. public void SetFocus (Control control)
  2220. {
  2221. if (control == null)
  2222. throw new ArgumentNullException ("control");
  2223. SetFocus (control.ClientID);
  2224. }
  2225. [EditorBrowsable (EditorBrowsableState.Advanced)]
  2226. public void RegisterRequiresControlState (Control control)
  2227. {
  2228. if (control == null)
  2229. throw new ArgumentNullException ("control");
  2230. if (RequiresControlState (control))
  2231. return;
  2232. if (requireStateControls == null)
  2233. requireStateControls = new ArrayList ();
  2234. int n = requireStateControls.Add (control);
  2235. if (_savedControlState == null || n >= _savedControlState.Length)
  2236. return;
  2237. for (Control parent = control.Parent; parent != null; parent = parent.Parent)
  2238. if (parent.IsChildControlStateCleared)
  2239. return;
  2240. object state = _savedControlState [n];
  2241. if (state != null)
  2242. control.LoadControlState (state);
  2243. }
  2244. public bool RequiresControlState (Control control)
  2245. {
  2246. if (requireStateControls == null) return false;
  2247. return requireStateControls.Contains (control);
  2248. }
  2249. [EditorBrowsable (EditorBrowsableState.Advanced)]
  2250. public void UnregisterRequiresControlState (Control control)
  2251. {
  2252. if (requireStateControls != null)
  2253. requireStateControls.Remove (control);
  2254. }
  2255. public ValidatorCollection GetValidators (string validationGroup)
  2256. {
  2257. if (validationGroup == String.Empty)
  2258. validationGroup = null;
  2259. ValidatorCollection col = new ValidatorCollection ();
  2260. if (_validators == null)
  2261. return col;
  2262. foreach (IValidator v in _validators)
  2263. if (BelongsToGroup(v, validationGroup))
  2264. col.Add(v);
  2265. return col;
  2266. }
  2267. bool BelongsToGroup(IValidator v, string validationGroup) {
  2268. BaseValidator validator = v as BaseValidator;
  2269. if (validationGroup == null)
  2270. return validator == null || String.IsNullOrEmpty (validator.ValidationGroup);
  2271. else
  2272. return validator != null && validator.ValidationGroup == validationGroup;
  2273. }
  2274. public virtual void Validate (string validationGroup)
  2275. {
  2276. is_validated = true;
  2277. ValidateCollection (GetValidators (validationGroup));
  2278. }
  2279. object SavePageControlState ()
  2280. {
  2281. if (requireStateControls == null) return null;
  2282. object[] state = new object [requireStateControls.Count];
  2283. bool allNull = true;
  2284. for (int n=0; n<state.Length; n++) {
  2285. state [n] = ((Control) requireStateControls [n]).SaveControlState ();
  2286. if (state [n] != null) allNull = false;
  2287. }
  2288. if (allNull) return null;
  2289. else return state;
  2290. }
  2291. void LoadPageControlState (object data)
  2292. {
  2293. _savedControlState = (object []) data;
  2294. if (requireStateControls == null) return;
  2295. int max = Math.Min (requireStateControls.Count, _savedControlState != null ? _savedControlState.Length : requireStateControls.Count);
  2296. for (int n=0; n < max; n++) {
  2297. Control ctl = (Control) requireStateControls [n];
  2298. ctl.LoadControlState (_savedControlState != null ? _savedControlState [n] : null);
  2299. }
  2300. }
  2301. void LoadPreviousPageReference ()
  2302. {
  2303. if (_requestValueCollection != null) {
  2304. string prevPage = _requestValueCollection [PreviousPageID];
  2305. if (prevPage != null) {
  2306. #if TARGET_J2EE
  2307. if (getFacesContext () != null) {
  2308. IHttpHandler handler = Context.ApplicationInstance.GetHandler (Context, prevPage);
  2309. Server.Execute (handler, null, true);
  2310. if (_context.Items.Contains (CrossPagePostBack)) {
  2311. previousPage = (Page) _context.Items [CrossPagePostBack];
  2312. _context.Items.Remove (CrossPagePostBack);
  2313. }
  2314. return;
  2315. }
  2316. #else
  2317. previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
  2318. previousPage.isCrossPagePostBack = true;
  2319. previousPage.ProcessRequest (Context);
  2320. #endif
  2321. }
  2322. }
  2323. }
  2324. Hashtable contentTemplates;
  2325. [EditorBrowsable (EditorBrowsableState.Never)]
  2326. protected internal void AddContentTemplate (string templateName, ITemplate template)
  2327. {
  2328. if (contentTemplates == null)
  2329. contentTemplates = new Hashtable ();
  2330. contentTemplates [templateName] = template;
  2331. }
  2332. PageTheme _pageTheme;
  2333. internal PageTheme PageTheme {
  2334. get { return _pageTheme; }
  2335. }
  2336. PageTheme _styleSheetPageTheme;
  2337. internal PageTheme StyleSheetPageTheme {
  2338. get { return _styleSheetPageTheme; }
  2339. }
  2340. Stack dataItemCtx;
  2341. internal void PushDataItemContext (object o) {
  2342. if (dataItemCtx == null)
  2343. dataItemCtx = new Stack ();
  2344. dataItemCtx.Push (o);
  2345. }
  2346. internal void PopDataItemContext () {
  2347. if (dataItemCtx == null)
  2348. throw new InvalidOperationException ();
  2349. dataItemCtx.Pop ();
  2350. }
  2351. public object GetDataItem() {
  2352. if (dataItemCtx == null || dataItemCtx.Count == 0)
  2353. throw new InvalidOperationException ("No data item");
  2354. return dataItemCtx.Peek ();
  2355. }
  2356. protected internal override void OnInit (EventArgs e)
  2357. {
  2358. base.OnInit (e);
  2359. ArrayList themes = new ArrayList();
  2360. if (StyleSheetPageTheme != null && StyleSheetPageTheme.GetStyleSheets () != null)
  2361. themes.AddRange (StyleSheetPageTheme.GetStyleSheets ());
  2362. if (PageTheme != null && PageTheme.GetStyleSheets () != null)
  2363. themes.AddRange (PageTheme.GetStyleSheets ());
  2364. if (themes.Count > 0 && Header == null)
  2365. throw new InvalidOperationException ("Using themed css files requires a header control on the page.");
  2366. foreach (string lss in themes) {
  2367. HtmlLink hl = new HtmlLink ();
  2368. hl.Href = lss;
  2369. hl.Attributes["type"] = "text/css";
  2370. hl.Attributes["rel"] = "stylesheet";
  2371. Header.Controls.Add (hl);
  2372. }
  2373. }
  2374. #endif
  2375. #if NET_2_0
  2376. [MonoTODO ("Not implemented. Only used by .net aspx parser")]
  2377. protected object GetWrappedFileDependencies (string [] list)
  2378. {
  2379. return list;
  2380. }
  2381. [MonoTODO ("Does nothing. Used by .net aspx parser")]
  2382. protected virtual void InitializeCulture ()
  2383. {
  2384. }
  2385. [MonoTODO ("Does nothing. Used by .net aspx parser")]
  2386. protected internal void AddWrappedFileDependencies (object virtualFileDependencies)
  2387. {
  2388. }
  2389. #endif
  2390. }
  2391. }