Page.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767
  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. #endif
  54. namespace System.Web.UI
  55. {
  56. // CAS
  57. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  58. [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  59. #if !NET_2_0
  60. [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
  61. #endif
  62. [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
  63. [ToolboxItem (false)]
  64. #if NET_2_0
  65. [Designer ("Microsoft.VisualStudio.Web.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VisualStudio_Web, typeof (IRootDesigner))]
  66. #else
  67. [Designer ("Microsoft.VSDesigner.WebForms.WebFormDesigner, " + Consts.AssemblyMicrosoft_VSDesigner, typeof (IRootDesigner))]
  68. #endif
  69. public class Page : TemplateControl, IHttpHandler
  70. {
  71. private bool _eventValidation = true;
  72. private bool _viewState = true;
  73. private bool _viewStateMac;
  74. private string _errorPage;
  75. private bool is_validated;
  76. private bool _smartNavigation;
  77. private int _transactionMode;
  78. private HttpContext _context;
  79. private ValidatorCollection _validators;
  80. private bool renderingForm;
  81. private object _savedViewState;
  82. private ArrayList _requiresPostBack;
  83. private ArrayList _requiresPostBackCopy;
  84. private ArrayList requiresPostDataChanged;
  85. private IPostBackEventHandler requiresRaiseEvent;
  86. private NameValueCollection secondPostData;
  87. private bool requiresPostBackScript;
  88. private bool postBackScriptRendered;
  89. bool handleViewState;
  90. string viewStateUserKey;
  91. NameValueCollection _requestValueCollection;
  92. string clientTarget;
  93. ClientScriptManager scriptManager;
  94. bool allow_load; // true when the Form collection belongs to this page (GetTypeHashCode)
  95. [EditorBrowsable (EditorBrowsableState.Never)]
  96. #if NET_2_0
  97. public
  98. #else
  99. protected
  100. #endif
  101. const string postEventArgumentID = "__EVENTARGUMENT";
  102. [EditorBrowsable (EditorBrowsableState.Never)]
  103. #if NET_2_0
  104. public
  105. #else
  106. protected
  107. #endif
  108. const string postEventSourceID = "__EVENTTARGET";
  109. #if NET_2_0
  110. internal const string CallbackArgumentID = "__CALLBACKARGUMENT";
  111. internal const string CallbackSourceID = "__CALLBACKTARGET";
  112. internal const string PreviousPageID = "__PREVIOUSPAGE";
  113. HtmlHead htmlHeader;
  114. MasterPage masterPage;
  115. string masterPageFile;
  116. Page previousPage;
  117. bool isCrossPagePostBack;
  118. ArrayList requireStateControls;
  119. Hashtable _validatorsByGroup;
  120. HtmlForm _form;
  121. string _title;
  122. string _theme;
  123. string _styleSheetTheme;
  124. #endif
  125. #region Constructor
  126. public Page ()
  127. {
  128. scriptManager = new ClientScriptManager (this);
  129. Page = this;
  130. ID = "__Page";
  131. }
  132. #endregion
  133. #region Properties
  134. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  135. [Browsable (false)]
  136. public HttpApplicationState Application
  137. {
  138. get {
  139. if (_context == null)
  140. return null;
  141. return _context.Application;
  142. }
  143. }
  144. [EditorBrowsable (EditorBrowsableState.Never)]
  145. protected bool AspCompatMode
  146. {
  147. #if NET_2_0
  148. get { return false; }
  149. #endif
  150. set { throw new NotImplementedException (); }
  151. }
  152. [EditorBrowsable (EditorBrowsableState.Never)]
  153. #if NET_2_0
  154. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  155. [BrowsableAttribute (false)]
  156. public bool Buffer
  157. {
  158. get { return Response.BufferOutput; }
  159. set { Response.BufferOutput = value; }
  160. }
  161. #else
  162. protected bool Buffer
  163. {
  164. set { Response.BufferOutput = value; }
  165. }
  166. #endif
  167. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  168. [Browsable (false)]
  169. public Cache Cache
  170. {
  171. get {
  172. if (_context == null)
  173. throw new HttpException ("No cache available without a context.");
  174. return _context.Cache;
  175. }
  176. }
  177. #if NET_2_0
  178. [EditorBrowsableAttribute (EditorBrowsableState.Advanced)]
  179. #endif
  180. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  181. [Browsable (false), DefaultValue ("")]
  182. [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
  183. public string ClientTarget
  184. {
  185. get { return (clientTarget == null) ? "" : clientTarget; }
  186. set {
  187. clientTarget = value;
  188. if (value == "")
  189. clientTarget = null;
  190. }
  191. }
  192. [EditorBrowsable (EditorBrowsableState.Never)]
  193. #if NET_2_0
  194. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  195. [BrowsableAttribute (false)]
  196. public int CodePage
  197. {
  198. get { return Response.ContentEncoding.CodePage; }
  199. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  200. }
  201. #else
  202. protected int CodePage
  203. {
  204. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  205. }
  206. #endif
  207. [EditorBrowsable (EditorBrowsableState.Never)]
  208. #if NET_2_0
  209. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  210. [BrowsableAttribute (false)]
  211. public string ContentType
  212. {
  213. get { return Response.ContentType; }
  214. set { Response.ContentType = value; }
  215. }
  216. #else
  217. protected string ContentType
  218. {
  219. set { Response.ContentType = value; }
  220. }
  221. #endif
  222. protected override HttpContext Context
  223. {
  224. get {
  225. if (_context == null)
  226. return HttpContext.Current;
  227. return _context;
  228. }
  229. }
  230. #if NET_2_0
  231. [EditorBrowsable (EditorBrowsableState.Advanced)]
  232. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  233. [BrowsableAttribute (false)]
  234. public string Culture
  235. {
  236. get { return Thread.CurrentThread.CurrentCulture.Name; }
  237. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  238. }
  239. #else
  240. [EditorBrowsable (EditorBrowsableState.Never)]
  241. protected string Culture
  242. {
  243. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  244. }
  245. #endif
  246. #if NET_2_0
  247. public virtual bool EnableEventValidation {
  248. get { return _eventValidation; }
  249. set { _eventValidation = value;}
  250. }
  251. #endif
  252. [Browsable (false)]
  253. public override bool EnableViewState
  254. {
  255. get { return _viewState; }
  256. set { _viewState = value; }
  257. }
  258. #if NET_2_0
  259. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  260. [BrowsableAttribute (false)]
  261. #endif
  262. [EditorBrowsable (EditorBrowsableState.Never)]
  263. #if NET_2_0
  264. public
  265. #else
  266. protected
  267. #endif
  268. bool EnableViewStateMac
  269. {
  270. get { return _viewStateMac; }
  271. set { _viewStateMac = value; }
  272. }
  273. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  274. [Browsable (false), DefaultValue ("")]
  275. [WebSysDescription ("The URL of a page used for error redirection.")]
  276. public string ErrorPage
  277. {
  278. get { return _errorPage; }
  279. set {
  280. _errorPage = value;
  281. if (_context != null)
  282. _context.ErrorPage = value;
  283. }
  284. }
  285. #if NET_2_0
  286. [Obsolete]
  287. #endif
  288. [EditorBrowsable (EditorBrowsableState.Never)]
  289. protected ArrayList FileDependencies
  290. {
  291. set {
  292. if (Response != null)
  293. Response.AddFileDependencies (value);
  294. }
  295. }
  296. [Browsable (false)]
  297. #if NET_2_0
  298. [EditorBrowsable (EditorBrowsableState.Never)]
  299. #endif
  300. public override string ID
  301. {
  302. get { return base.ID; }
  303. set { base.ID = value; }
  304. }
  305. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  306. [Browsable (false)]
  307. public bool IsPostBack
  308. {
  309. get {
  310. return _requestValueCollection != null;
  311. }
  312. }
  313. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  314. public bool IsReusable {
  315. get { return false; }
  316. }
  317. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  318. [Browsable (false)]
  319. public bool IsValid {
  320. get {
  321. if (!is_validated)
  322. throw new HttpException (Locale.GetText ("Page hasn't been validated."));
  323. return ValidateCollection (_validators);
  324. }
  325. }
  326. [EditorBrowsable (EditorBrowsableState.Never)]
  327. #if NET_2_0
  328. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  329. [BrowsableAttribute (false)]
  330. public int LCID {
  331. get { return Thread.CurrentThread.CurrentCulture.LCID; }
  332. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  333. }
  334. #else
  335. protected int LCID {
  336. set { Thread.CurrentThread.CurrentCulture = new CultureInfo (value); }
  337. }
  338. #endif
  339. #if NET_2_0
  340. public PageAdapter PageAdapter {
  341. get {
  342. return (PageAdapter)Adapter;
  343. }
  344. }
  345. #endif
  346. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  347. [Browsable (false)]
  348. public HttpRequest Request
  349. {
  350. get {
  351. if (_context != null)
  352. return _context.Request;
  353. throw new HttpException("Request is not available without context");
  354. }
  355. }
  356. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  357. [Browsable (false)]
  358. public HttpResponse Response
  359. {
  360. get {
  361. if (_context != null)
  362. return _context.Response;
  363. throw new HttpException ("Response is not available without context");
  364. }
  365. }
  366. [EditorBrowsable (EditorBrowsableState.Never)]
  367. #if NET_2_0
  368. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  369. [BrowsableAttribute (false)]
  370. public string ResponseEncoding
  371. {
  372. get { return Response.ContentEncoding.WebName; }
  373. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  374. }
  375. #else
  376. protected string ResponseEncoding
  377. {
  378. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  379. }
  380. #endif
  381. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  382. [Browsable (false)]
  383. public HttpServerUtility Server
  384. {
  385. get {
  386. return Context.Server;
  387. }
  388. }
  389. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  390. [Browsable (false)]
  391. public virtual HttpSessionState Session
  392. {
  393. get {
  394. if (_context == null)
  395. throw new HttpException ("Session is not available without context");
  396. if (_context.Session == null)
  397. throw new HttpException ("Session state can only be used " +
  398. "when enableSessionState is set to true, either " +
  399. "in a configuration file or in the Page directive.");
  400. return _context.Session;
  401. }
  402. }
  403. #if NET_2_0
  404. [FilterableAttribute (false)]
  405. [Obsolete]
  406. #endif
  407. [Browsable (false)]
  408. public bool SmartNavigation
  409. {
  410. get { return _smartNavigation; }
  411. set { _smartNavigation = value; }
  412. }
  413. #if NET_2_0
  414. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  415. [Filterable (false)]
  416. [Browsable (false)]
  417. public virtual string StyleSheetTheme {
  418. get { return _styleSheetTheme; }
  419. set { _styleSheetTheme = value; }
  420. }
  421. [Browsable (false)]
  422. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  423. public virtual string Theme {
  424. get { return _theme; }
  425. set { _theme = value; }
  426. }
  427. void InitializeStyleSheet ()
  428. {
  429. if (_styleSheetTheme == null) {
  430. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  431. if (ps != null)
  432. _styleSheetTheme = ps.StyleSheetTheme;
  433. }
  434. if (_styleSheetTheme != null && _styleSheetTheme != "")
  435. _styleSheetPageTheme = ThemeDirectoryCompiler.GetCompiledInstance ("./App_Themes/" + _styleSheetTheme + "/", _context);
  436. }
  437. void InitializeTheme ()
  438. {
  439. if (_theme == null) {
  440. PagesSection ps = WebConfigurationManager.GetSection ("system.web/pages") as PagesSection;
  441. if (ps != null)
  442. _theme = ps.Theme;
  443. }
  444. if (_theme != null && _theme != "") {
  445. _pageTheme = ThemeDirectoryCompiler.GetCompiledInstance ("./App_Themes/" + _theme + "/", _context);
  446. ApplyThemeRecursive ();
  447. }
  448. }
  449. #endif
  450. #if NET_2_0
  451. [Localizable (true)]
  452. [Bindable (true)]
  453. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  454. public string Title {
  455. get { return _title; }
  456. set { _title = value; }
  457. }
  458. #endif
  459. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  460. [Browsable (false)]
  461. public TraceContext Trace
  462. {
  463. get { return Context.Trace; }
  464. }
  465. [EditorBrowsable (EditorBrowsableState.Never)]
  466. #if NET_2_0
  467. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  468. [BrowsableAttribute (false)]
  469. public bool TraceEnabled
  470. {
  471. get { return Trace.IsEnabled; }
  472. set { Trace.IsEnabled = value; }
  473. }
  474. #else
  475. protected bool TraceEnabled
  476. {
  477. set { Trace.IsEnabled = value; }
  478. }
  479. #endif
  480. [EditorBrowsable (EditorBrowsableState.Never)]
  481. #if NET_2_0
  482. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  483. [BrowsableAttribute (false)]
  484. public TraceMode TraceModeValue
  485. {
  486. get { return Trace.TraceMode; }
  487. set { Trace.TraceMode = value; }
  488. }
  489. #else
  490. protected TraceMode TraceModeValue
  491. {
  492. set { Trace.TraceMode = value; }
  493. }
  494. #endif
  495. [EditorBrowsable (EditorBrowsableState.Never)]
  496. protected int TransactionMode
  497. {
  498. #if NET_2_0
  499. get { return _transactionMode; }
  500. #endif
  501. set { _transactionMode = value; }
  502. }
  503. #if NET_2_0
  504. [EditorBrowsable (EditorBrowsableState.Advanced)]
  505. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  506. [BrowsableAttribute (false)]
  507. public string UICulture
  508. {
  509. get { return Thread.CurrentThread.CurrentUICulture.Name; }
  510. set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
  511. }
  512. #else
  513. [EditorBrowsable (EditorBrowsableState.Never)]
  514. protected string UICulture
  515. {
  516. set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
  517. }
  518. #endif
  519. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  520. [Browsable (false)]
  521. public IPrincipal User
  522. {
  523. get { return Context.User; }
  524. }
  525. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  526. [Browsable (false)]
  527. public ValidatorCollection Validators
  528. {
  529. get {
  530. if (_validators == null)
  531. _validators = new ValidatorCollection ();
  532. return _validators;
  533. }
  534. }
  535. [MonoTODO ("Use this when encrypting/decrypting ViewState")]
  536. [Browsable (false)]
  537. public string ViewStateUserKey {
  538. get { return viewStateUserKey; }
  539. set { viewStateUserKey = value; }
  540. }
  541. [Browsable (false)]
  542. public override bool Visible
  543. {
  544. get { return base.Visible; }
  545. set { base.Visible = value; }
  546. }
  547. #endregion
  548. #region Methods
  549. [EditorBrowsable (EditorBrowsableState.Never)]
  550. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  551. AsyncCallback cb,
  552. object extraData)
  553. {
  554. throw new NotImplementedException ();
  555. }
  556. [EditorBrowsable (EditorBrowsableState.Never)]
  557. protected void AspCompatEndProcessRequest (IAsyncResult result)
  558. {
  559. throw new NotImplementedException ();
  560. }
  561. [EditorBrowsable (EditorBrowsableState.Advanced)]
  562. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  563. {
  564. return new HtmlTextWriter (tw);
  565. }
  566. [EditorBrowsable (EditorBrowsableState.Never)]
  567. public void DesignerInitialize ()
  568. {
  569. InitRecursive (null);
  570. }
  571. [EditorBrowsable (EditorBrowsableState.Advanced)]
  572. protected virtual NameValueCollection DeterminePostBackMode ()
  573. {
  574. if (_context == null)
  575. return null;
  576. HttpRequest req = _context.Request;
  577. if (req == null)
  578. return null;
  579. NameValueCollection coll = null;
  580. if (0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture)) {
  581. coll = req.Form;
  582. WebROCollection c = (WebROCollection) coll;
  583. allow_load = !c.GotID;
  584. if (allow_load) {
  585. c.ID = GetTypeHashCode ();
  586. } else {
  587. allow_load = (c.ID == GetTypeHashCode ());
  588. }
  589. } else {
  590. coll = req.QueryString;
  591. }
  592. if (coll == null || coll ["__VIEWSTATE"] == null)
  593. return null;
  594. return coll;
  595. }
  596. #if NET_2_0
  597. public override Control FindControl (string id) {
  598. if (id == ID)
  599. return this;
  600. else
  601. return base.FindControl (id);
  602. }
  603. #endif
  604. #if NET_2_0
  605. [Obsolete]
  606. #endif
  607. [EditorBrowsable (EditorBrowsableState.Advanced)]
  608. public string GetPostBackClientEvent (Control control, string argument)
  609. {
  610. return scriptManager.GetPostBackEventReference (control, argument);
  611. }
  612. #if NET_2_0
  613. [Obsolete]
  614. #endif
  615. [EditorBrowsable (EditorBrowsableState.Advanced)]
  616. public string GetPostBackClientHyperlink (Control control, string argument)
  617. {
  618. return scriptManager.GetPostBackClientHyperlink (control, argument);
  619. }
  620. #if NET_2_0
  621. [Obsolete]
  622. #endif
  623. [EditorBrowsable (EditorBrowsableState.Advanced)]
  624. public string GetPostBackEventReference (Control control)
  625. {
  626. return scriptManager.GetPostBackEventReference (control, "");
  627. }
  628. #if NET_2_0
  629. [Obsolete]
  630. #endif
  631. [EditorBrowsable (EditorBrowsableState.Advanced)]
  632. public string GetPostBackEventReference (Control control, string argument)
  633. {
  634. return scriptManager.GetPostBackEventReference (control, argument);
  635. }
  636. internal void RequiresPostBackScript ()
  637. {
  638. requiresPostBackScript = true;
  639. }
  640. [EditorBrowsable (EditorBrowsableState.Never)]
  641. public virtual int GetTypeHashCode ()
  642. {
  643. return 0;
  644. }
  645. [EditorBrowsable (EditorBrowsableState.Never)]
  646. protected virtual void InitOutputCache (int duration,
  647. string varyByHeader,
  648. string varyByCustom,
  649. OutputCacheLocation location,
  650. string varyByParam)
  651. {
  652. HttpCachePolicy cache = _context.Response.Cache;
  653. bool set_vary = false;
  654. switch (location) {
  655. case OutputCacheLocation.Any:
  656. cache.SetCacheability (HttpCacheability.Public);
  657. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  658. cache.SetLastModified (_context.Timestamp);
  659. set_vary = true;
  660. break;
  661. case OutputCacheLocation.Client:
  662. cache.SetCacheability (HttpCacheability.Private);
  663. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  664. cache.SetLastModified (_context.Timestamp);
  665. break;
  666. case OutputCacheLocation.Downstream:
  667. cache.SetCacheability (HttpCacheability.Public);
  668. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  669. cache.SetLastModified (_context.Timestamp);
  670. break;
  671. case OutputCacheLocation.Server:
  672. cache.SetCacheability (HttpCacheability.Server);
  673. set_vary = true;
  674. break;
  675. case OutputCacheLocation.None:
  676. break;
  677. }
  678. if (set_vary) {
  679. if (varyByCustom != null)
  680. cache.SetVaryByCustom (varyByCustom);
  681. if (varyByParam != null && varyByParam.Length > 0) {
  682. string[] prms = varyByParam.Split (';');
  683. foreach (string p in prms)
  684. cache.VaryByParams [p.Trim ()] = true;
  685. cache.VaryByParams.IgnoreParams = false;
  686. } else {
  687. cache.VaryByParams.IgnoreParams = true;
  688. }
  689. if (varyByHeader != null && varyByHeader.Length > 0) {
  690. string[] hdrs = varyByHeader.Split (';');
  691. foreach (string h in hdrs)
  692. cache.VaryByHeaders [h.Trim ()] = true;
  693. }
  694. }
  695. cache.Duration = duration;
  696. cache.SetExpires (_context.Timestamp.AddSeconds (duration));
  697. }
  698. #if NET_2_0
  699. [Obsolete]
  700. #else
  701. [EditorBrowsable (EditorBrowsableState.Advanced)]
  702. #endif
  703. public bool IsClientScriptBlockRegistered (string key)
  704. {
  705. return scriptManager.IsClientScriptBlockRegistered (key);
  706. }
  707. #if NET_2_0
  708. [Obsolete]
  709. #else
  710. [EditorBrowsable (EditorBrowsableState.Advanced)]
  711. #endif
  712. public bool IsStartupScriptRegistered (string key)
  713. {
  714. return scriptManager.IsStartupScriptRegistered (key);
  715. }
  716. public string MapPath (string virtualPath)
  717. {
  718. return Request.MapPath (virtualPath);
  719. }
  720. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  721. {
  722. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
  723. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
  724. writer.WriteLine ();
  725. writer.WriteLine ("<script language=\"javascript\">");
  726. writer.WriteLine ("<!--");
  727. if (Request.Browser.Browser == ("Netscape") && Request.Browser.MajorVersion == 4)
  728. writer.WriteLine ("\tvar theForm = document.{0};", formUniqueID);
  729. else
  730. writer.WriteLine ("\tvar theForm = document.getElementById ('{0}');", formUniqueID);
  731. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  732. writer.WriteLine ("\t\ttheForm.{0}.value = eventTarget;", postEventSourceID);
  733. writer.WriteLine ("\t\ttheForm.{0}.value = eventArgument;", postEventArgumentID);
  734. writer.WriteLine ("\t\ttheForm.submit();");
  735. writer.WriteLine ("\t}");
  736. writer.WriteLine ("// -->");
  737. writer.WriteLine ("</script>");
  738. }
  739. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  740. {
  741. if (renderingForm)
  742. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  743. renderingForm = true;
  744. writer.WriteLine ();
  745. scriptManager.WriteHiddenFields (writer);
  746. if (requiresPostBackScript) {
  747. RenderPostBackScript (writer, formUniqueID);
  748. postBackScriptRendered = true;
  749. }
  750. if (handleViewState) {
  751. string vs = GetViewStateString ();
  752. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  753. writer.WriteLine ("value=\"{0}\" />", vs);
  754. }
  755. scriptManager.WriteClientScriptBlocks (writer);
  756. }
  757. LosFormatter GetFormatter ()
  758. {
  759. #if NET_2_0
  760. PagesSection config = (PagesSection) WebConfigurationManager.GetSection ("system.web/pages");
  761. #else
  762. PagesConfiguration config = PagesConfiguration.GetInstance (_context);
  763. #endif
  764. byte [] vkey = null;
  765. if (config.EnableViewStateMac) {
  766. #if NET_2_0
  767. MachineKeySection mconfig = (MachineKeySection) WebConfigurationManager.GetSection ("system.web/machineKey");
  768. vkey = mconfig.ValidationKeyBytes;
  769. #else
  770. MachineKeyConfig mconfig = HttpContext.GetAppConfig ("system.web/machineKey") as MachineKeyConfig;
  771. vkey = mconfig.ValidationKey;
  772. #endif
  773. }
  774. return new LosFormatter (config.EnableViewStateMac, vkey);
  775. }
  776. string GetViewStateString ()
  777. {
  778. if (_savedViewState == null)
  779. return null;
  780. LosFormatter fmt = GetFormatter ();
  781. MemoryStream ms = new MemoryStream ();
  782. fmt.Serialize (ms, _savedViewState);
  783. return Convert.ToBase64String (ms.GetBuffer (), 0, (int) ms.Length);
  784. }
  785. internal object GetSavedViewState ()
  786. {
  787. return _savedViewState;
  788. }
  789. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  790. {
  791. scriptManager.WriteArrayDeclares (writer);
  792. if (!postBackScriptRendered && requiresPostBackScript)
  793. RenderPostBackScript (writer, formUniqueID);
  794. scriptManager.WriteHiddenFields (writer);
  795. scriptManager.WriteClientScriptIncludes (writer);
  796. scriptManager.WriteStartupScriptBlocks (writer);
  797. renderingForm = false;
  798. postBackScriptRendered = false;
  799. }
  800. private void ProcessPostData (NameValueCollection data, bool second)
  801. {
  802. if (data == null)
  803. return;
  804. if (_requiresPostBackCopy == null && _requiresPostBack != null)
  805. _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
  806. Hashtable used = new Hashtable ();
  807. foreach (string id in data.AllKeys){
  808. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
  809. continue;
  810. string real_id = id;
  811. int dot = real_id.IndexOf ('.');
  812. if (dot >= 1)
  813. real_id = real_id.Substring (0, dot);
  814. if (real_id == null || used.ContainsKey (real_id))
  815. continue;
  816. used.Add (real_id, real_id);
  817. Control ctrl = FindControl (real_id);
  818. if (ctrl != null){
  819. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  820. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  821. if (pbdh == null) {
  822. if (pbeh != null)
  823. RegisterRequiresRaiseEvent (pbeh);
  824. continue;
  825. }
  826. if (pbdh.LoadPostData (real_id, data) == true) {
  827. if (requiresPostDataChanged == null)
  828. requiresPostDataChanged = new ArrayList ();
  829. requiresPostDataChanged.Add (pbdh);
  830. if (_requiresPostBackCopy != null)
  831. _requiresPostBackCopy.Remove (ctrl.UniqueID);
  832. }
  833. } else if (!second) {
  834. if (secondPostData == null)
  835. secondPostData = new NameValueCollection ();
  836. secondPostData.Add (real_id, data [id]);
  837. }
  838. }
  839. ArrayList list1 = null;
  840. if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
  841. string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
  842. foreach (string id in handlers) {
  843. IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
  844. if (pbdh != null) {
  845. _requiresPostBackCopy.Remove (id);
  846. if (pbdh.LoadPostData (id, data)) {
  847. if (requiresPostDataChanged == null)
  848. requiresPostDataChanged = new ArrayList ();
  849. requiresPostDataChanged.Add (pbdh);
  850. }
  851. } else if (second) {
  852. if (list1 == null)
  853. list1 = new ArrayList ();
  854. list1.Add (id);
  855. }
  856. }
  857. }
  858. _requiresPostBack = list1;
  859. }
  860. [EditorBrowsable (EditorBrowsableState.Never)]
  861. #if NET_2_0 || TARGET_JVM
  862. public virtual void ProcessRequest (HttpContext context)
  863. #else
  864. public void ProcessRequest (HttpContext context)
  865. #endif
  866. {
  867. _context = context;
  868. if (clientTarget != null)
  869. Request.ClientTarget = clientTarget;
  870. WireupAutomaticEvents ();
  871. //-- Control execution lifecycle in the docs
  872. // Save culture information because it can be modified in FrameworkInitialize()
  873. CultureInfo culture = Thread.CurrentThread.CurrentCulture;
  874. CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
  875. FrameworkInitialize ();
  876. context.ErrorPage = _errorPage;
  877. try {
  878. InternalProcessRequest ();
  879. } catch (ThreadAbortException) {
  880. // Do nothing, just ignore it by now.
  881. } catch (Exception e) {
  882. context.AddError (e); // OnError might access LastError
  883. OnError (EventArgs.Empty);
  884. context.ClearError (e);
  885. // We want to remove that error, as we're rethrowing to stop
  886. // further processing.
  887. throw;
  888. } finally {
  889. try {
  890. UnloadRecursive (true);
  891. } catch {}
  892. if (Thread.CurrentThread.CurrentCulture.Equals (culture) == false)
  893. Thread.CurrentThread.CurrentCulture = culture;
  894. if (Thread.CurrentThread.CurrentUICulture.Equals (uiculture) == false)
  895. Thread.CurrentThread.CurrentUICulture = uiculture;
  896. }
  897. }
  898. #if NET_2_0
  899. internal void ProcessCrossPagePostBack (HttpContext context)
  900. {
  901. isCrossPagePostBack = true;
  902. ProcessRequest (context);
  903. }
  904. #endif
  905. void InternalProcessRequest ()
  906. {
  907. _requestValueCollection = this.DeterminePostBackMode();
  908. #if NET_2_0
  909. if (!IsCrossPagePostBack)
  910. LoadPreviousPageReference ();
  911. OnPreInit (EventArgs.Empty);
  912. InitializeTheme ();
  913. #endif
  914. Trace.Write ("aspx.page", "Begin Init");
  915. InitRecursive (null);
  916. Trace.Write ("aspx.page", "End Init");
  917. #if NET_2_0
  918. OnInitComplete (EventArgs.Empty);
  919. ApplyMasterPage ();
  920. if (_title != null && htmlHeader != null)
  921. htmlHeader.Title = _title;
  922. #endif
  923. renderingForm = false;
  924. if (IsPostBack) {
  925. Trace.Write ("aspx.page", "Begin LoadViewState");
  926. LoadPageViewState ();
  927. Trace.Write ("aspx.page", "End LoadViewState");
  928. Trace.Write ("aspx.page", "Begin ProcessPostData");
  929. ProcessPostData (_requestValueCollection, false);
  930. Trace.Write ("aspx.page", "End ProcessPostData");
  931. }
  932. #if NET_2_0
  933. if (IsCrossPagePostBack)
  934. return;
  935. OnPreLoad (EventArgs.Empty);
  936. #endif
  937. LoadRecursive ();
  938. if (IsPostBack) {
  939. Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
  940. ProcessPostData (secondPostData, true);
  941. Trace.Write ("aspx.page", "End ProcessPostData Second Try");
  942. Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
  943. RaiseChangedEvents ();
  944. Trace.Write ("aspx.page", "End Raise ChangedEvents");
  945. Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
  946. RaisePostBackEvents ();
  947. Trace.Write ("aspx.page", "End Raise PostBackEvent");
  948. }
  949. #if NET_2_0
  950. OnLoadComplete (EventArgs.Empty);
  951. if (IsCallback) {
  952. string result = ProcessCallbackData ();
  953. HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
  954. callbackOutput.Write (result);
  955. callbackOutput.Flush ();
  956. return;
  957. }
  958. #endif
  959. Trace.Write ("aspx.page", "Begin PreRender");
  960. PreRenderRecursiveInternal ();
  961. Trace.Write ("aspx.page", "End PreRender");
  962. #if NET_2_0
  963. OnPreRenderComplete (EventArgs.Empty);
  964. #endif
  965. Trace.Write ("aspx.page", "Begin SaveViewState");
  966. SavePageViewState ();
  967. Trace.Write ("aspx.page", "End SaveViewState");
  968. #if NET_2_0
  969. OnSaveStateComplete (EventArgs.Empty);
  970. #endif
  971. //--
  972. Trace.Write ("aspx.page", "Begin Render");
  973. HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
  974. RenderControl (output);
  975. Trace.Write ("aspx.page", "End Render");
  976. RenderTrace (output);
  977. }
  978. private void RenderTrace (HtmlTextWriter output)
  979. {
  980. TraceManager traceManager = HttpRuntime.TraceManager;
  981. if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
  982. return;
  983. Trace.SaveData ();
  984. if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
  985. return;
  986. if (!traceManager.LocalOnly || Context.Request.IsLocal)
  987. Trace.Render (output);
  988. }
  989. void RaisePostBackEvents ()
  990. {
  991. if (requiresRaiseEvent != null) {
  992. RaisePostBackEvent (requiresRaiseEvent, null);
  993. return;
  994. }
  995. NameValueCollection postdata = _requestValueCollection;
  996. if (postdata == null)
  997. return;
  998. string eventTarget = postdata [postEventSourceID];
  999. if (eventTarget == null || eventTarget.Length == 0) {
  1000. Validate ();
  1001. return;
  1002. }
  1003. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  1004. if (target == null)
  1005. return;
  1006. string eventArgument = postdata [postEventArgumentID];
  1007. RaisePostBackEvent (target, eventArgument);
  1008. }
  1009. internal void RaiseChangedEvents ()
  1010. {
  1011. if (requiresPostDataChanged == null)
  1012. return;
  1013. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  1014. ipdh.RaisePostDataChangedEvent ();
  1015. requiresPostDataChanged = null;
  1016. }
  1017. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1018. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  1019. {
  1020. sourceControl.RaisePostBackEvent (eventArgument);
  1021. }
  1022. #if NET_2_0
  1023. [Obsolete]
  1024. #endif
  1025. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1026. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  1027. {
  1028. scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
  1029. }
  1030. #if NET_2_0
  1031. [Obsolete]
  1032. #endif
  1033. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1034. public virtual void RegisterClientScriptBlock (string key, string script)
  1035. {
  1036. scriptManager.RegisterClientScriptBlock (key, script);
  1037. }
  1038. #if NET_2_0
  1039. [Obsolete]
  1040. #endif
  1041. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1042. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  1043. {
  1044. scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  1045. }
  1046. [MonoTODO("Used in HtmlForm")]
  1047. internal void RegisterClientScriptFile (string a, string b, string c)
  1048. {
  1049. throw new NotImplementedException ();
  1050. }
  1051. #if NET_2_0
  1052. [Obsolete]
  1053. #endif
  1054. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1055. public void RegisterOnSubmitStatement (string key, string script)
  1056. {
  1057. scriptManager.RegisterOnSubmitStatement (key, script);
  1058. }
  1059. internal string GetSubmitStatements ()
  1060. {
  1061. return scriptManager.WriteSubmitStatements ();
  1062. }
  1063. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1064. public void RegisterRequiresPostBack (Control control)
  1065. {
  1066. if (_requiresPostBack == null)
  1067. _requiresPostBack = new ArrayList ();
  1068. _requiresPostBack.Add (control.UniqueID);
  1069. }
  1070. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1071. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  1072. {
  1073. requiresRaiseEvent = control;
  1074. }
  1075. #if NET_2_0
  1076. [Obsolete]
  1077. #endif
  1078. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1079. public virtual void RegisterStartupScript (string key, string script)
  1080. {
  1081. scriptManager.RegisterStartupScript (key, script);
  1082. }
  1083. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1084. public void RegisterViewStateHandler ()
  1085. {
  1086. handleViewState = true;
  1087. }
  1088. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1089. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  1090. {
  1091. _savedViewState = viewState;
  1092. }
  1093. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1094. protected virtual object LoadPageStateFromPersistenceMedium ()
  1095. {
  1096. NameValueCollection postdata = _requestValueCollection;
  1097. string view_state;
  1098. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  1099. return null;
  1100. _savedViewState = null;
  1101. if (view_state == "")
  1102. return null;
  1103. LosFormatter fmt = GetFormatter ();
  1104. try {
  1105. _savedViewState = fmt.Deserialize (view_state);
  1106. } catch (Exception e) {
  1107. throw new HttpException ("Error restoring page viewstate.", e);
  1108. }
  1109. return _savedViewState;
  1110. }
  1111. internal void LoadPageViewState()
  1112. {
  1113. object sState = LoadPageStateFromPersistenceMedium ();
  1114. if (sState != null) {
  1115. #if NET_2_0
  1116. Triplet data = (Triplet) sState;
  1117. if (allow_load) {
  1118. LoadPageControlState (data.Third);
  1119. LoadViewStateRecursive (data.First);
  1120. _requiresPostBack = data.Second as ArrayList;
  1121. }
  1122. #else
  1123. Pair pair = (Pair) sState;
  1124. if (allow_load) {
  1125. LoadViewStateRecursive (pair.First);
  1126. _requiresPostBack = pair.Second as ArrayList;
  1127. }
  1128. #endif
  1129. }
  1130. }
  1131. internal void SavePageViewState ()
  1132. {
  1133. if (!handleViewState)
  1134. return;
  1135. #if NET_2_0
  1136. object controlState = SavePageControlState ();
  1137. #endif
  1138. object viewState = SaveViewStateRecursive ();
  1139. object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
  1140. #if NET_2_0
  1141. Triplet triplet = new Triplet ();
  1142. triplet.First = viewState;
  1143. triplet.Second = reqPostback;
  1144. triplet.Third = controlState;
  1145. if (triplet.First == null && triplet.Second == null && triplet.Third == null)
  1146. triplet = null;
  1147. SavePageStateToPersistenceMedium (triplet);
  1148. #else
  1149. Pair pair = new Pair ();
  1150. pair.First = viewState;
  1151. pair.Second = reqPostback;
  1152. if (pair.First == null && pair.Second == null)
  1153. pair = null;
  1154. SavePageStateToPersistenceMedium (pair);
  1155. #endif
  1156. }
  1157. public virtual void Validate ()
  1158. {
  1159. is_validated = true;
  1160. ValidateCollection (_validators);
  1161. }
  1162. internal virtual bool AreValidatorsUplevel ()
  1163. {
  1164. bool uplevel = false;
  1165. foreach (IValidator v in Validators) {
  1166. BaseValidator bv = v as BaseValidator;
  1167. if (bv == null) continue;
  1168. if (bv.GetRenderUplevel()) {
  1169. uplevel = true;
  1170. break;
  1171. }
  1172. }
  1173. return uplevel;
  1174. }
  1175. bool ValidateCollection (ValidatorCollection validators)
  1176. {
  1177. #if NET_2_0
  1178. if (!_eventValidation)
  1179. return true;
  1180. #endif
  1181. if (validators == null || validators.Count == 0)
  1182. return true;
  1183. bool all_valid = true;
  1184. foreach (IValidator v in validators){
  1185. v.Validate ();
  1186. if (v.IsValid == false)
  1187. all_valid = false;
  1188. }
  1189. return all_valid;
  1190. }
  1191. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1192. public virtual void VerifyRenderingInServerForm (Control control)
  1193. {
  1194. if (_context == null)
  1195. return;
  1196. if (!renderingForm)
  1197. throw new HttpException ("Control '" +
  1198. control.ClientID +
  1199. "' of type '" +
  1200. control.GetType ().Name +
  1201. "' must be placed inside a form tag with runat=server.");
  1202. }
  1203. protected override void FrameworkInitialize ()
  1204. {
  1205. base.FrameworkInitialize ();
  1206. #if NET_2_0
  1207. InitializeStyleSheet ();
  1208. #endif
  1209. }
  1210. #endregion
  1211. #if NET_2_0
  1212. public
  1213. #else
  1214. internal
  1215. #endif
  1216. ClientScriptManager ClientScript {
  1217. get { return scriptManager; }
  1218. }
  1219. #if NET_2_0
  1220. static readonly object InitCompleteEvent = new object ();
  1221. static readonly object LoadCompleteEvent = new object ();
  1222. static readonly object PreInitEvent = new object ();
  1223. static readonly object PreLoadEvent = new object ();
  1224. static readonly object PreRenderCompleteEvent = new object ();
  1225. static readonly object SaveStateCompleteEvent = new object ();
  1226. int event_mask;
  1227. const int initcomplete_mask = 1;
  1228. const int loadcomplete_mask = 1 << 1;
  1229. const int preinit_mask = 1 << 2;
  1230. const int preload_mask = 1 << 3;
  1231. const int prerendercomplete_mask = 1 << 4;
  1232. const int savestatecomplete_mask = 1 << 5;
  1233. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1234. public event EventHandler InitComplete {
  1235. add {
  1236. event_mask |= initcomplete_mask;
  1237. Events.AddHandler (InitCompleteEvent, value);
  1238. }
  1239. remove { Events.RemoveHandler (InitCompleteEvent, value); }
  1240. }
  1241. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1242. public event EventHandler LoadComplete {
  1243. add {
  1244. event_mask |= loadcomplete_mask;
  1245. Events.AddHandler (LoadCompleteEvent, value);
  1246. }
  1247. remove { Events.RemoveHandler (LoadCompleteEvent, value); }
  1248. }
  1249. public event EventHandler PreInit {
  1250. add {
  1251. event_mask |= preinit_mask;
  1252. Events.AddHandler (PreInitEvent, value);
  1253. }
  1254. remove { Events.RemoveHandler (PreInitEvent, value); }
  1255. }
  1256. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1257. public event EventHandler PreLoad {
  1258. add {
  1259. event_mask |= preload_mask;
  1260. Events.AddHandler (PreLoadEvent, value);
  1261. }
  1262. remove { Events.RemoveHandler (PreLoadEvent, value); }
  1263. }
  1264. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1265. public event EventHandler PreRenderComplete {
  1266. add {
  1267. event_mask |= prerendercomplete_mask;
  1268. Events.AddHandler (PreRenderCompleteEvent, value);
  1269. }
  1270. remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
  1271. }
  1272. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1273. public event EventHandler SaveStateComplete {
  1274. add {
  1275. event_mask |= savestatecomplete_mask;
  1276. Events.AddHandler (SaveStateCompleteEvent, value);
  1277. }
  1278. remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
  1279. }
  1280. protected virtual void OnInitComplete (EventArgs e)
  1281. {
  1282. if ((event_mask & initcomplete_mask) != 0) {
  1283. EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
  1284. if (eh != null) eh (this, e);
  1285. }
  1286. }
  1287. protected virtual void OnLoadComplete (EventArgs e)
  1288. {
  1289. if ((event_mask & loadcomplete_mask) != 0) {
  1290. EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
  1291. if (eh != null) eh (this, e);
  1292. }
  1293. }
  1294. protected virtual void OnPreInit (EventArgs e)
  1295. {
  1296. if ((event_mask & preinit_mask) != 0) {
  1297. EventHandler eh = (EventHandler) (Events [PreInitEvent]);
  1298. if (eh != null) eh (this, e);
  1299. }
  1300. }
  1301. protected virtual void OnPreLoad (EventArgs e)
  1302. {
  1303. if ((event_mask & preload_mask) != 0) {
  1304. EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
  1305. if (eh != null) eh (this, e);
  1306. }
  1307. }
  1308. protected virtual void OnPreRenderComplete (EventArgs e)
  1309. {
  1310. if ((event_mask & prerendercomplete_mask) != 0) {
  1311. EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
  1312. if (eh != null) eh (this, e);
  1313. }
  1314. }
  1315. protected virtual void OnSaveStateComplete (EventArgs e)
  1316. {
  1317. if ((event_mask & savestatecomplete_mask) != 0) {
  1318. EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
  1319. if (eh != null) eh (this, e);
  1320. }
  1321. }
  1322. public HtmlForm Form {
  1323. get { return _form; }
  1324. }
  1325. internal void RegisterForm (HtmlForm form)
  1326. {
  1327. _form = form;
  1328. }
  1329. [BrowsableAttribute (false)]
  1330. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1331. public Page PreviousPage {
  1332. get { return previousPage; }
  1333. }
  1334. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1335. [BrowsableAttribute (false)]
  1336. public bool IsCallback {
  1337. get { return _requestValueCollection != null && _requestValueCollection [CallbackArgumentID] != null; }
  1338. }
  1339. [BrowsableAttribute (false)]
  1340. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1341. public bool IsCrossPagePostBack {
  1342. get { return _requestValueCollection != null && isCrossPagePostBack; }
  1343. }
  1344. string ProcessCallbackData ()
  1345. {
  1346. string callbackTarget = _requestValueCollection [CallbackSourceID];
  1347. if (callbackTarget == null || callbackTarget.Length == 0)
  1348. throw new HttpException ("Callback target not provided.");
  1349. ICallbackEventHandler target = FindControl (callbackTarget) as ICallbackEventHandler;
  1350. if (target == null)
  1351. throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
  1352. string callbackArgument = _requestValueCollection [CallbackArgumentID];
  1353. target.RaiseCallbackEvent (callbackArgument);
  1354. return target.GetCallbackResult ();
  1355. }
  1356. [BrowsableAttribute (false)]
  1357. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1358. public HtmlHead Header {
  1359. get { return htmlHeader; }
  1360. }
  1361. internal void SetHeader (HtmlHead header)
  1362. {
  1363. htmlHeader = header;
  1364. }
  1365. void ApplyMasterPage ()
  1366. {
  1367. if (masterPageFile != null) {
  1368. ArrayList appliedMasterPageFiles = new ArrayList ();
  1369. if (Master != null) {
  1370. MasterPage.ApplyMasterPageRecursive (Master, appliedMasterPageFiles);
  1371. Master.Page = this;
  1372. Controls.Clear ();
  1373. Controls.Add (Master);
  1374. }
  1375. }
  1376. }
  1377. [DefaultValueAttribute ("")]
  1378. public virtual string MasterPageFile {
  1379. get { return masterPageFile; }
  1380. set {
  1381. masterPageFile = value;
  1382. masterPage = null;
  1383. }
  1384. }
  1385. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1386. [BrowsableAttribute (false)]
  1387. public MasterPage Master {
  1388. get {
  1389. if (masterPage == null)
  1390. masterPage = MasterPage.CreateMasterPage (this, Context, masterPageFile, contentTemplates);
  1391. return masterPage;
  1392. }
  1393. }
  1394. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1395. public void RegisterRequiresControlState (Control control)
  1396. {
  1397. if (requireStateControls == null) requireStateControls = new ArrayList ();
  1398. requireStateControls.Add (control);
  1399. }
  1400. public bool RequiresControlState (Control control)
  1401. {
  1402. if (requireStateControls == null) return false;
  1403. return requireStateControls.Contains (control);
  1404. }
  1405. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1406. public void UnregisterRequiresControlState (Control control)
  1407. {
  1408. if (requireStateControls != null)
  1409. requireStateControls.Remove (control);
  1410. }
  1411. public ValidatorCollection GetValidators (string validationGroup)
  1412. {
  1413. if (validationGroup == null || validationGroup == "")
  1414. return Validators;
  1415. if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
  1416. ValidatorCollection col = _validatorsByGroup [validationGroup] as ValidatorCollection;
  1417. if (col == null) {
  1418. col = new ValidatorCollection ();
  1419. _validatorsByGroup [validationGroup] = col;
  1420. }
  1421. return col;
  1422. }
  1423. public virtual void Validate (string validationGroup)
  1424. {
  1425. is_validated = true;
  1426. if (validationGroup == null || validationGroup == "")
  1427. ValidateCollection (_validators);
  1428. else if (_validatorsByGroup != null) {
  1429. ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
  1430. }
  1431. }
  1432. object SavePageControlState ()
  1433. {
  1434. if (requireStateControls == null) return null;
  1435. object[] state = new object [requireStateControls.Count];
  1436. bool allNull = true;
  1437. for (int n=0; n<state.Length; n++) {
  1438. state [n] = ((Control) requireStateControls [n]).SaveControlState ();
  1439. if (state [n] != null) allNull = false;
  1440. }
  1441. if (allNull) return null;
  1442. else return state;
  1443. }
  1444. void LoadPageControlState (object data)
  1445. {
  1446. if (requireStateControls == null) return;
  1447. object[] state = (object[]) data;
  1448. int max = Math.Min (requireStateControls.Count, state != null ? state.Length : requireStateControls.Count);
  1449. for (int n=0; n < max; n++) {
  1450. Control ctl = (Control) requireStateControls [n];
  1451. ctl.LoadControlState (state != null ? state [n] : null);
  1452. }
  1453. }
  1454. void LoadPreviousPageReference ()
  1455. {
  1456. if (_requestValueCollection != null) {
  1457. string prevPage = _requestValueCollection [PreviousPageID];
  1458. if (prevPage != null) {
  1459. previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
  1460. previousPage.ProcessCrossPagePostBack (_context);
  1461. } else {
  1462. previousPage = _context.LastPage;
  1463. }
  1464. }
  1465. _context.LastPage = this;
  1466. }
  1467. Hashtable contentTemplates;
  1468. [EditorBrowsable (EditorBrowsableState.Never)]
  1469. protected internal void AddContentTemplate (string templateName, ITemplate template)
  1470. {
  1471. if (contentTemplates == null)
  1472. contentTemplates = new Hashtable ();
  1473. contentTemplates [templateName] = template;
  1474. }
  1475. PageTheme _pageTheme;
  1476. internal PageTheme PageTheme {
  1477. get { return _pageTheme; }
  1478. }
  1479. PageTheme _styleSheetPageTheme;
  1480. internal PageTheme StyleSheetPageTheme {
  1481. get { return _styleSheetPageTheme; }
  1482. }
  1483. Stack dataItemCtx;
  1484. internal void PushDataItemContext (object o) {
  1485. if (dataItemCtx == null)
  1486. dataItemCtx = new Stack ();
  1487. dataItemCtx.Push (o);
  1488. }
  1489. internal void PopDataItemContext () {
  1490. if (dataItemCtx == null)
  1491. throw new InvalidOperationException ();
  1492. dataItemCtx.Pop ();
  1493. }
  1494. public object GetDataItem() {
  1495. if (dataItemCtx == null || dataItemCtx.Count == 0)
  1496. throw new InvalidOperationException ("No data item");
  1497. return dataItemCtx.Peek ();
  1498. }
  1499. protected internal override void OnInit (EventArgs e)
  1500. {
  1501. base.OnInit (e);
  1502. if (Header == null)
  1503. return;
  1504. ArrayList themes = new ArrayList();
  1505. if (StyleSheetPageTheme != null)
  1506. themes.AddRange (StyleSheetPageTheme.GetStyleSheets ());
  1507. if (PageTheme != null)
  1508. themes.AddRange (PageTheme.GetStyleSheets ());
  1509. foreach (string lss in themes) {
  1510. HtmlLink hl = new HtmlLink ();
  1511. hl.Href = lss;
  1512. hl.Attributes["type"] = "text/css";
  1513. hl.Attributes["rel"] = "stylesheet";
  1514. Header.Controls.Add (hl);
  1515. }
  1516. }
  1517. #endif
  1518. }
  1519. }