Page.cs 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764
  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. }
  447. }
  448. #endif
  449. #if NET_2_0
  450. [Localizable (true)]
  451. [Bindable (true)]
  452. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  453. public string Title {
  454. get { return _title; }
  455. set { _title = value; }
  456. }
  457. #endif
  458. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  459. [Browsable (false)]
  460. public TraceContext Trace
  461. {
  462. get { return Context.Trace; }
  463. }
  464. [EditorBrowsable (EditorBrowsableState.Never)]
  465. #if NET_2_0
  466. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  467. [BrowsableAttribute (false)]
  468. public bool TraceEnabled
  469. {
  470. get { return Trace.IsEnabled; }
  471. set { Trace.IsEnabled = value; }
  472. }
  473. #else
  474. protected bool TraceEnabled
  475. {
  476. set { Trace.IsEnabled = value; }
  477. }
  478. #endif
  479. [EditorBrowsable (EditorBrowsableState.Never)]
  480. #if NET_2_0
  481. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  482. [BrowsableAttribute (false)]
  483. public TraceMode TraceModeValue
  484. {
  485. get { return Trace.TraceMode; }
  486. set { Trace.TraceMode = value; }
  487. }
  488. #else
  489. protected TraceMode TraceModeValue
  490. {
  491. set { Trace.TraceMode = value; }
  492. }
  493. #endif
  494. [EditorBrowsable (EditorBrowsableState.Never)]
  495. protected int TransactionMode
  496. {
  497. #if NET_2_0
  498. get { return _transactionMode; }
  499. #endif
  500. set { _transactionMode = value; }
  501. }
  502. #if NET_2_0
  503. [EditorBrowsable (EditorBrowsableState.Advanced)]
  504. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  505. [BrowsableAttribute (false)]
  506. public string UICulture
  507. {
  508. get { return Thread.CurrentThread.CurrentUICulture.Name; }
  509. set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
  510. }
  511. #else
  512. [EditorBrowsable (EditorBrowsableState.Never)]
  513. protected string UICulture
  514. {
  515. set { Thread.CurrentThread.CurrentUICulture = new CultureInfo (value); }
  516. }
  517. #endif
  518. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  519. [Browsable (false)]
  520. public IPrincipal User
  521. {
  522. get { return Context.User; }
  523. }
  524. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  525. [Browsable (false)]
  526. public ValidatorCollection Validators
  527. {
  528. get {
  529. if (_validators == null)
  530. _validators = new ValidatorCollection ();
  531. return _validators;
  532. }
  533. }
  534. [MonoTODO ("Use this when encrypting/decrypting ViewState")]
  535. [Browsable (false)]
  536. public string ViewStateUserKey {
  537. get { return viewStateUserKey; }
  538. set { viewStateUserKey = value; }
  539. }
  540. [Browsable (false)]
  541. public override bool Visible
  542. {
  543. get { return base.Visible; }
  544. set { base.Visible = value; }
  545. }
  546. #endregion
  547. #region Methods
  548. [EditorBrowsable (EditorBrowsableState.Never)]
  549. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  550. AsyncCallback cb,
  551. object extraData)
  552. {
  553. throw new NotImplementedException ();
  554. }
  555. [EditorBrowsable (EditorBrowsableState.Never)]
  556. protected void AspCompatEndProcessRequest (IAsyncResult result)
  557. {
  558. throw new NotImplementedException ();
  559. }
  560. [EditorBrowsable (EditorBrowsableState.Advanced)]
  561. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  562. {
  563. return new HtmlTextWriter (tw);
  564. }
  565. [EditorBrowsable (EditorBrowsableState.Never)]
  566. public void DesignerInitialize ()
  567. {
  568. InitRecursive (null);
  569. }
  570. [EditorBrowsable (EditorBrowsableState.Advanced)]
  571. protected virtual NameValueCollection DeterminePostBackMode ()
  572. {
  573. if (_context == null)
  574. return null;
  575. HttpRequest req = _context.Request;
  576. if (req == null)
  577. return null;
  578. NameValueCollection coll = null;
  579. if (0 == String.Compare (Request.HttpMethod, "POST", true, CultureInfo.InvariantCulture)) {
  580. coll = req.Form;
  581. WebROCollection c = (WebROCollection) coll;
  582. allow_load = !c.GotID;
  583. if (allow_load) {
  584. c.ID = GetTypeHashCode ();
  585. } else {
  586. allow_load = (c.ID == GetTypeHashCode ());
  587. }
  588. } else {
  589. coll = req.QueryString;
  590. }
  591. if (coll == null || coll ["__VIEWSTATE"] == null)
  592. return null;
  593. return coll;
  594. }
  595. #if NET_2_0
  596. public override Control FindControl (string id) {
  597. if (id == ID)
  598. return this;
  599. else
  600. return base.FindControl (id);
  601. }
  602. #endif
  603. #if NET_2_0
  604. [Obsolete]
  605. #endif
  606. [EditorBrowsable (EditorBrowsableState.Advanced)]
  607. public string GetPostBackClientEvent (Control control, string argument)
  608. {
  609. return scriptManager.GetPostBackEventReference (control, argument);
  610. }
  611. #if NET_2_0
  612. [Obsolete]
  613. #endif
  614. [EditorBrowsable (EditorBrowsableState.Advanced)]
  615. public string GetPostBackClientHyperlink (Control control, string argument)
  616. {
  617. return scriptManager.GetPostBackClientHyperlink (control, argument);
  618. }
  619. #if NET_2_0
  620. [Obsolete]
  621. #endif
  622. [EditorBrowsable (EditorBrowsableState.Advanced)]
  623. public string GetPostBackEventReference (Control control)
  624. {
  625. return scriptManager.GetPostBackEventReference (control, "");
  626. }
  627. #if NET_2_0
  628. [Obsolete]
  629. #endif
  630. [EditorBrowsable (EditorBrowsableState.Advanced)]
  631. public string GetPostBackEventReference (Control control, string argument)
  632. {
  633. return scriptManager.GetPostBackEventReference (control, argument);
  634. }
  635. internal void RequiresPostBackScript ()
  636. {
  637. requiresPostBackScript = true;
  638. }
  639. [EditorBrowsable (EditorBrowsableState.Never)]
  640. public virtual int GetTypeHashCode ()
  641. {
  642. return 0;
  643. }
  644. [EditorBrowsable (EditorBrowsableState.Never)]
  645. protected virtual void InitOutputCache (int duration,
  646. string varyByHeader,
  647. string varyByCustom,
  648. OutputCacheLocation location,
  649. string varyByParam)
  650. {
  651. HttpCachePolicy cache = _context.Response.Cache;
  652. bool set_vary = false;
  653. switch (location) {
  654. case OutputCacheLocation.Any:
  655. cache.SetCacheability (HttpCacheability.Public);
  656. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  657. cache.SetLastModified (_context.Timestamp);
  658. set_vary = true;
  659. break;
  660. case OutputCacheLocation.Client:
  661. cache.SetCacheability (HttpCacheability.Private);
  662. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  663. cache.SetLastModified (_context.Timestamp);
  664. break;
  665. case OutputCacheLocation.Downstream:
  666. cache.SetCacheability (HttpCacheability.Public);
  667. cache.SetMaxAge (new TimeSpan (0, 0, duration));
  668. cache.SetLastModified (_context.Timestamp);
  669. break;
  670. case OutputCacheLocation.Server:
  671. cache.SetCacheability (HttpCacheability.Server);
  672. set_vary = true;
  673. break;
  674. case OutputCacheLocation.None:
  675. break;
  676. }
  677. if (set_vary) {
  678. if (varyByCustom != null)
  679. cache.SetVaryByCustom (varyByCustom);
  680. if (varyByParam != null && varyByParam.Length > 0) {
  681. string[] prms = varyByParam.Split (';');
  682. foreach (string p in prms)
  683. cache.VaryByParams [p.Trim ()] = true;
  684. cache.VaryByParams.IgnoreParams = false;
  685. } else {
  686. cache.VaryByParams.IgnoreParams = true;
  687. }
  688. if (varyByHeader != null && varyByHeader.Length > 0) {
  689. string[] hdrs = varyByHeader.Split (';');
  690. foreach (string h in hdrs)
  691. cache.VaryByHeaders [h.Trim ()] = true;
  692. }
  693. }
  694. cache.Duration = duration;
  695. cache.SetExpires (_context.Timestamp.AddSeconds (duration));
  696. }
  697. #if NET_2_0
  698. [Obsolete]
  699. #else
  700. [EditorBrowsable (EditorBrowsableState.Advanced)]
  701. #endif
  702. public bool IsClientScriptBlockRegistered (string key)
  703. {
  704. return scriptManager.IsClientScriptBlockRegistered (key);
  705. }
  706. #if NET_2_0
  707. [Obsolete]
  708. #else
  709. [EditorBrowsable (EditorBrowsableState.Advanced)]
  710. #endif
  711. public bool IsStartupScriptRegistered (string key)
  712. {
  713. return scriptManager.IsStartupScriptRegistered (key);
  714. }
  715. public string MapPath (string virtualPath)
  716. {
  717. return Request.MapPath (virtualPath);
  718. }
  719. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  720. {
  721. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
  722. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
  723. writer.WriteLine ();
  724. writer.WriteLine ("<script language=\"javascript\">");
  725. writer.WriteLine ("<!--");
  726. if (Request.Browser.Browser == ("Netscape") && Request.Browser.MajorVersion == 4)
  727. writer.WriteLine ("\tvar theForm = document.{0};", formUniqueID);
  728. else
  729. writer.WriteLine ("\tvar theForm = document.getElementById ('{0}');", formUniqueID);
  730. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  731. writer.WriteLine ("\t\ttheForm.{0}.value = eventTarget;", postEventSourceID);
  732. writer.WriteLine ("\t\ttheForm.{0}.value = eventArgument;", postEventArgumentID);
  733. writer.WriteLine ("\t\ttheForm.submit();");
  734. writer.WriteLine ("\t}");
  735. writer.WriteLine ("// -->");
  736. writer.WriteLine ("</script>");
  737. }
  738. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  739. {
  740. if (renderingForm)
  741. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  742. renderingForm = true;
  743. writer.WriteLine ();
  744. scriptManager.WriteHiddenFields (writer);
  745. if (requiresPostBackScript) {
  746. RenderPostBackScript (writer, formUniqueID);
  747. postBackScriptRendered = true;
  748. }
  749. if (handleViewState) {
  750. string vs = GetViewStateString ();
  751. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  752. writer.WriteLine ("value=\"{0}\" />", vs);
  753. }
  754. scriptManager.WriteClientScriptBlocks (writer);
  755. }
  756. LosFormatter GetFormatter ()
  757. {
  758. #if NET_2_0
  759. PagesSection config = (PagesSection) WebConfigurationManager.GetSection ("system.web/pages");
  760. #else
  761. PagesConfiguration config = PagesConfiguration.GetInstance (_context);
  762. #endif
  763. byte [] vkey = null;
  764. if (config.EnableViewStateMac) {
  765. #if NET_2_0
  766. MachineKeySection mconfig = (MachineKeySection) WebConfigurationManager.GetSection ("system.web/machineKey");
  767. vkey = mconfig.ValidationKeyBytes;
  768. #else
  769. MachineKeyConfig mconfig = HttpContext.GetAppConfig ("system.web/machineKey") as MachineKeyConfig;
  770. vkey = mconfig.ValidationKey;
  771. #endif
  772. }
  773. return new LosFormatter (config.EnableViewStateMac, vkey);
  774. }
  775. string GetViewStateString ()
  776. {
  777. if (_savedViewState == null)
  778. return null;
  779. LosFormatter fmt = GetFormatter ();
  780. return fmt.SerializeToBase64 (_savedViewState);
  781. }
  782. internal object GetSavedViewState ()
  783. {
  784. return _savedViewState;
  785. }
  786. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  787. {
  788. scriptManager.WriteArrayDeclares (writer);
  789. if (!postBackScriptRendered && requiresPostBackScript)
  790. RenderPostBackScript (writer, formUniqueID);
  791. scriptManager.WriteHiddenFields (writer);
  792. scriptManager.WriteClientScriptIncludes (writer);
  793. scriptManager.WriteStartupScriptBlocks (writer);
  794. renderingForm = false;
  795. postBackScriptRendered = false;
  796. }
  797. private void ProcessPostData (NameValueCollection data, bool second)
  798. {
  799. if (data == null)
  800. return;
  801. if (_requiresPostBackCopy == null && _requiresPostBack != null)
  802. _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
  803. Hashtable used = new Hashtable ();
  804. foreach (string id in data.AllKeys){
  805. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
  806. continue;
  807. string real_id = id;
  808. int dot = real_id.IndexOf ('.');
  809. if (dot >= 1)
  810. real_id = real_id.Substring (0, dot);
  811. if (real_id == null || used.ContainsKey (real_id))
  812. continue;
  813. used.Add (real_id, real_id);
  814. Control ctrl = FindControl (real_id);
  815. if (ctrl != null){
  816. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  817. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  818. if (pbdh == null) {
  819. if (pbeh != null)
  820. RegisterRequiresRaiseEvent (pbeh);
  821. continue;
  822. }
  823. if (pbdh.LoadPostData (real_id, data) == true) {
  824. if (requiresPostDataChanged == null)
  825. requiresPostDataChanged = new ArrayList ();
  826. requiresPostDataChanged.Add (pbdh);
  827. if (_requiresPostBackCopy != null)
  828. _requiresPostBackCopy.Remove (ctrl.UniqueID);
  829. }
  830. } else if (!second) {
  831. if (secondPostData == null)
  832. secondPostData = new NameValueCollection ();
  833. secondPostData.Add (real_id, data [id]);
  834. }
  835. }
  836. ArrayList list1 = null;
  837. if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
  838. string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
  839. foreach (string id in handlers) {
  840. IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
  841. if (pbdh != null) {
  842. _requiresPostBackCopy.Remove (id);
  843. if (pbdh.LoadPostData (id, data)) {
  844. if (requiresPostDataChanged == null)
  845. requiresPostDataChanged = new ArrayList ();
  846. requiresPostDataChanged.Add (pbdh);
  847. }
  848. } else if (second) {
  849. if (list1 == null)
  850. list1 = new ArrayList ();
  851. list1.Add (id);
  852. }
  853. }
  854. }
  855. _requiresPostBack = list1;
  856. }
  857. [EditorBrowsable (EditorBrowsableState.Never)]
  858. #if NET_2_0 || TARGET_JVM
  859. public virtual void ProcessRequest (HttpContext context)
  860. #else
  861. public void ProcessRequest (HttpContext context)
  862. #endif
  863. {
  864. _context = context;
  865. if (clientTarget != null)
  866. Request.ClientTarget = clientTarget;
  867. WireupAutomaticEvents ();
  868. //-- Control execution lifecycle in the docs
  869. // Save culture information because it can be modified in FrameworkInitialize()
  870. CultureInfo culture = Thread.CurrentThread.CurrentCulture;
  871. CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
  872. FrameworkInitialize ();
  873. context.ErrorPage = _errorPage;
  874. try {
  875. InternalProcessRequest ();
  876. } catch (ThreadAbortException) {
  877. // Do nothing, just ignore it by now.
  878. } catch (Exception e) {
  879. context.AddError (e); // OnError might access LastError
  880. OnError (EventArgs.Empty);
  881. context.ClearError (e);
  882. // We want to remove that error, as we're rethrowing to stop
  883. // further processing.
  884. throw;
  885. } finally {
  886. try {
  887. UnloadRecursive (true);
  888. } catch {}
  889. if (Thread.CurrentThread.CurrentCulture.Equals (culture) == false)
  890. Thread.CurrentThread.CurrentCulture = culture;
  891. if (Thread.CurrentThread.CurrentUICulture.Equals (uiculture) == false)
  892. Thread.CurrentThread.CurrentUICulture = uiculture;
  893. }
  894. }
  895. #if NET_2_0
  896. internal void ProcessCrossPagePostBack (HttpContext context)
  897. {
  898. isCrossPagePostBack = true;
  899. ProcessRequest (context);
  900. }
  901. #endif
  902. void InternalProcessRequest ()
  903. {
  904. _requestValueCollection = this.DeterminePostBackMode();
  905. #if NET_2_0
  906. if (!IsCrossPagePostBack)
  907. LoadPreviousPageReference ();
  908. OnPreInit (EventArgs.Empty);
  909. InitializeTheme ();
  910. ApplyMasterPage ();
  911. #endif
  912. Trace.Write ("aspx.page", "Begin Init");
  913. InitRecursive (null);
  914. Trace.Write ("aspx.page", "End Init");
  915. #if NET_2_0
  916. OnInitComplete (EventArgs.Empty);
  917. if (_title != null && htmlHeader != null)
  918. htmlHeader.Title = _title;
  919. #endif
  920. renderingForm = false;
  921. if (IsPostBack) {
  922. Trace.Write ("aspx.page", "Begin LoadViewState");
  923. LoadPageViewState ();
  924. Trace.Write ("aspx.page", "End LoadViewState");
  925. Trace.Write ("aspx.page", "Begin ProcessPostData");
  926. ProcessPostData (_requestValueCollection, false);
  927. Trace.Write ("aspx.page", "End ProcessPostData");
  928. }
  929. #if NET_2_0
  930. if (IsCrossPagePostBack)
  931. return;
  932. OnPreLoad (EventArgs.Empty);
  933. #endif
  934. LoadRecursive ();
  935. if (IsPostBack) {
  936. Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
  937. ProcessPostData (secondPostData, true);
  938. Trace.Write ("aspx.page", "End ProcessPostData Second Try");
  939. Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
  940. RaiseChangedEvents ();
  941. Trace.Write ("aspx.page", "End Raise ChangedEvents");
  942. Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
  943. RaisePostBackEvents ();
  944. Trace.Write ("aspx.page", "End Raise PostBackEvent");
  945. }
  946. #if NET_2_0
  947. OnLoadComplete (EventArgs.Empty);
  948. if (IsCallback) {
  949. string result = ProcessCallbackData ();
  950. HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
  951. callbackOutput.Write (result);
  952. callbackOutput.Flush ();
  953. return;
  954. }
  955. #endif
  956. Trace.Write ("aspx.page", "Begin PreRender");
  957. PreRenderRecursiveInternal ();
  958. Trace.Write ("aspx.page", "End PreRender");
  959. #if NET_2_0
  960. OnPreRenderComplete (EventArgs.Empty);
  961. #endif
  962. Trace.Write ("aspx.page", "Begin SaveViewState");
  963. SavePageViewState ();
  964. Trace.Write ("aspx.page", "End SaveViewState");
  965. #if NET_2_0
  966. OnSaveStateComplete (EventArgs.Empty);
  967. #endif
  968. //--
  969. Trace.Write ("aspx.page", "Begin Render");
  970. HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
  971. RenderControl (output);
  972. Trace.Write ("aspx.page", "End Render");
  973. RenderTrace (output);
  974. }
  975. private void RenderTrace (HtmlTextWriter output)
  976. {
  977. TraceManager traceManager = HttpRuntime.TraceManager;
  978. if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
  979. return;
  980. Trace.SaveData ();
  981. if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
  982. return;
  983. if (!traceManager.LocalOnly || Context.Request.IsLocal)
  984. Trace.Render (output);
  985. }
  986. void RaisePostBackEvents ()
  987. {
  988. if (requiresRaiseEvent != null) {
  989. RaisePostBackEvent (requiresRaiseEvent, null);
  990. return;
  991. }
  992. NameValueCollection postdata = _requestValueCollection;
  993. if (postdata == null)
  994. return;
  995. string eventTarget = postdata [postEventSourceID];
  996. if (eventTarget == null || eventTarget.Length == 0) {
  997. Validate ();
  998. return;
  999. }
  1000. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  1001. if (target == null)
  1002. return;
  1003. string eventArgument = postdata [postEventArgumentID];
  1004. RaisePostBackEvent (target, eventArgument);
  1005. }
  1006. internal void RaiseChangedEvents ()
  1007. {
  1008. if (requiresPostDataChanged == null)
  1009. return;
  1010. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  1011. ipdh.RaisePostDataChangedEvent ();
  1012. requiresPostDataChanged = null;
  1013. }
  1014. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1015. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  1016. {
  1017. sourceControl.RaisePostBackEvent (eventArgument);
  1018. }
  1019. #if NET_2_0
  1020. [Obsolete]
  1021. #endif
  1022. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1023. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  1024. {
  1025. scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
  1026. }
  1027. #if NET_2_0
  1028. [Obsolete]
  1029. #endif
  1030. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1031. public virtual void RegisterClientScriptBlock (string key, string script)
  1032. {
  1033. scriptManager.RegisterClientScriptBlock (key, script);
  1034. }
  1035. #if NET_2_0
  1036. [Obsolete]
  1037. #endif
  1038. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1039. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  1040. {
  1041. scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  1042. }
  1043. [MonoTODO("Used in HtmlForm")]
  1044. internal void RegisterClientScriptFile (string a, string b, string c)
  1045. {
  1046. throw new NotImplementedException ();
  1047. }
  1048. #if NET_2_0
  1049. [Obsolete]
  1050. #endif
  1051. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1052. public void RegisterOnSubmitStatement (string key, string script)
  1053. {
  1054. scriptManager.RegisterOnSubmitStatement (key, script);
  1055. }
  1056. internal string GetSubmitStatements ()
  1057. {
  1058. return scriptManager.WriteSubmitStatements ();
  1059. }
  1060. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1061. public void RegisterRequiresPostBack (Control control)
  1062. {
  1063. if (_requiresPostBack == null)
  1064. _requiresPostBack = new ArrayList ();
  1065. _requiresPostBack.Add (control.UniqueID);
  1066. }
  1067. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1068. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  1069. {
  1070. requiresRaiseEvent = control;
  1071. }
  1072. #if NET_2_0
  1073. [Obsolete]
  1074. #endif
  1075. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1076. public virtual void RegisterStartupScript (string key, string script)
  1077. {
  1078. scriptManager.RegisterStartupScript (key, script);
  1079. }
  1080. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1081. public void RegisterViewStateHandler ()
  1082. {
  1083. handleViewState = true;
  1084. }
  1085. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1086. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  1087. {
  1088. _savedViewState = viewState;
  1089. }
  1090. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1091. protected virtual object LoadPageStateFromPersistenceMedium ()
  1092. {
  1093. NameValueCollection postdata = _requestValueCollection;
  1094. string view_state;
  1095. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  1096. return null;
  1097. _savedViewState = null;
  1098. if (view_state == "")
  1099. return null;
  1100. LosFormatter fmt = GetFormatter ();
  1101. try {
  1102. _savedViewState = fmt.Deserialize (view_state);
  1103. } catch (Exception e) {
  1104. throw new HttpException ("Error restoring page viewstate.", e);
  1105. }
  1106. return _savedViewState;
  1107. }
  1108. internal void LoadPageViewState()
  1109. {
  1110. object sState = LoadPageStateFromPersistenceMedium ();
  1111. if (sState != null) {
  1112. #if NET_2_0
  1113. Triplet data = (Triplet) sState;
  1114. if (allow_load) {
  1115. LoadPageControlState (data.Third);
  1116. LoadViewStateRecursive (data.First);
  1117. _requiresPostBack = data.Second as ArrayList;
  1118. }
  1119. #else
  1120. Pair pair = (Pair) sState;
  1121. if (allow_load) {
  1122. LoadViewStateRecursive (pair.First);
  1123. _requiresPostBack = pair.Second as ArrayList;
  1124. }
  1125. #endif
  1126. }
  1127. }
  1128. internal void SavePageViewState ()
  1129. {
  1130. if (!handleViewState)
  1131. return;
  1132. #if NET_2_0
  1133. object controlState = SavePageControlState ();
  1134. #endif
  1135. object viewState = SaveViewStateRecursive ();
  1136. object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
  1137. #if NET_2_0
  1138. Triplet triplet = new Triplet ();
  1139. triplet.First = viewState;
  1140. triplet.Second = reqPostback;
  1141. triplet.Third = controlState;
  1142. if (triplet.First == null && triplet.Second == null && triplet.Third == null)
  1143. triplet = null;
  1144. SavePageStateToPersistenceMedium (triplet);
  1145. #else
  1146. Pair pair = new Pair ();
  1147. pair.First = viewState;
  1148. pair.Second = reqPostback;
  1149. if (pair.First == null && pair.Second == null)
  1150. pair = null;
  1151. SavePageStateToPersistenceMedium (pair);
  1152. #endif
  1153. }
  1154. public virtual void Validate ()
  1155. {
  1156. is_validated = true;
  1157. ValidateCollection (_validators);
  1158. }
  1159. internal virtual bool AreValidatorsUplevel ()
  1160. {
  1161. bool uplevel = false;
  1162. foreach (IValidator v in Validators) {
  1163. BaseValidator bv = v as BaseValidator;
  1164. if (bv == null) continue;
  1165. if (bv.GetRenderUplevel()) {
  1166. uplevel = true;
  1167. break;
  1168. }
  1169. }
  1170. return uplevel;
  1171. }
  1172. bool ValidateCollection (ValidatorCollection validators)
  1173. {
  1174. #if NET_2_0
  1175. if (!_eventValidation)
  1176. return true;
  1177. #endif
  1178. if (validators == null || validators.Count == 0)
  1179. return true;
  1180. bool all_valid = true;
  1181. foreach (IValidator v in validators){
  1182. v.Validate ();
  1183. if (v.IsValid == false)
  1184. all_valid = false;
  1185. }
  1186. return all_valid;
  1187. }
  1188. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1189. public virtual void VerifyRenderingInServerForm (Control control)
  1190. {
  1191. if (_context == null)
  1192. return;
  1193. if (!renderingForm)
  1194. throw new HttpException ("Control '" +
  1195. control.ClientID +
  1196. "' of type '" +
  1197. control.GetType ().Name +
  1198. "' must be placed inside a form tag with runat=server.");
  1199. }
  1200. protected override void FrameworkInitialize ()
  1201. {
  1202. base.FrameworkInitialize ();
  1203. #if NET_2_0
  1204. InitializeStyleSheet ();
  1205. #endif
  1206. }
  1207. #endregion
  1208. #if NET_2_0
  1209. public
  1210. #else
  1211. internal
  1212. #endif
  1213. ClientScriptManager ClientScript {
  1214. get { return scriptManager; }
  1215. }
  1216. #if NET_2_0
  1217. static readonly object InitCompleteEvent = new object ();
  1218. static readonly object LoadCompleteEvent = new object ();
  1219. static readonly object PreInitEvent = new object ();
  1220. static readonly object PreLoadEvent = new object ();
  1221. static readonly object PreRenderCompleteEvent = new object ();
  1222. static readonly object SaveStateCompleteEvent = new object ();
  1223. int event_mask;
  1224. const int initcomplete_mask = 1;
  1225. const int loadcomplete_mask = 1 << 1;
  1226. const int preinit_mask = 1 << 2;
  1227. const int preload_mask = 1 << 3;
  1228. const int prerendercomplete_mask = 1 << 4;
  1229. const int savestatecomplete_mask = 1 << 5;
  1230. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1231. public event EventHandler InitComplete {
  1232. add {
  1233. event_mask |= initcomplete_mask;
  1234. Events.AddHandler (InitCompleteEvent, value);
  1235. }
  1236. remove { Events.RemoveHandler (InitCompleteEvent, value); }
  1237. }
  1238. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1239. public event EventHandler LoadComplete {
  1240. add {
  1241. event_mask |= loadcomplete_mask;
  1242. Events.AddHandler (LoadCompleteEvent, value);
  1243. }
  1244. remove { Events.RemoveHandler (LoadCompleteEvent, value); }
  1245. }
  1246. public event EventHandler PreInit {
  1247. add {
  1248. event_mask |= preinit_mask;
  1249. Events.AddHandler (PreInitEvent, value);
  1250. }
  1251. remove { Events.RemoveHandler (PreInitEvent, value); }
  1252. }
  1253. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1254. public event EventHandler PreLoad {
  1255. add {
  1256. event_mask |= preload_mask;
  1257. Events.AddHandler (PreLoadEvent, value);
  1258. }
  1259. remove { Events.RemoveHandler (PreLoadEvent, value); }
  1260. }
  1261. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1262. public event EventHandler PreRenderComplete {
  1263. add {
  1264. event_mask |= prerendercomplete_mask;
  1265. Events.AddHandler (PreRenderCompleteEvent, value);
  1266. }
  1267. remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
  1268. }
  1269. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1270. public event EventHandler SaveStateComplete {
  1271. add {
  1272. event_mask |= savestatecomplete_mask;
  1273. Events.AddHandler (SaveStateCompleteEvent, value);
  1274. }
  1275. remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
  1276. }
  1277. protected virtual void OnInitComplete (EventArgs e)
  1278. {
  1279. if ((event_mask & initcomplete_mask) != 0) {
  1280. EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
  1281. if (eh != null) eh (this, e);
  1282. }
  1283. }
  1284. protected virtual void OnLoadComplete (EventArgs e)
  1285. {
  1286. if ((event_mask & loadcomplete_mask) != 0) {
  1287. EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
  1288. if (eh != null) eh (this, e);
  1289. }
  1290. }
  1291. protected virtual void OnPreInit (EventArgs e)
  1292. {
  1293. if ((event_mask & preinit_mask) != 0) {
  1294. EventHandler eh = (EventHandler) (Events [PreInitEvent]);
  1295. if (eh != null) eh (this, e);
  1296. }
  1297. }
  1298. protected virtual void OnPreLoad (EventArgs e)
  1299. {
  1300. if ((event_mask & preload_mask) != 0) {
  1301. EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
  1302. if (eh != null) eh (this, e);
  1303. }
  1304. }
  1305. protected virtual void OnPreRenderComplete (EventArgs e)
  1306. {
  1307. if ((event_mask & prerendercomplete_mask) != 0) {
  1308. EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
  1309. if (eh != null) eh (this, e);
  1310. }
  1311. }
  1312. protected virtual void OnSaveStateComplete (EventArgs e)
  1313. {
  1314. if ((event_mask & savestatecomplete_mask) != 0) {
  1315. EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
  1316. if (eh != null) eh (this, e);
  1317. }
  1318. }
  1319. public HtmlForm Form {
  1320. get { return _form; }
  1321. }
  1322. internal void RegisterForm (HtmlForm form)
  1323. {
  1324. _form = form;
  1325. }
  1326. [BrowsableAttribute (false)]
  1327. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1328. public Page PreviousPage {
  1329. get { return previousPage; }
  1330. }
  1331. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1332. [BrowsableAttribute (false)]
  1333. public bool IsCallback {
  1334. get { return _requestValueCollection != null && _requestValueCollection [CallbackArgumentID] != null; }
  1335. }
  1336. [BrowsableAttribute (false)]
  1337. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1338. public bool IsCrossPagePostBack {
  1339. get { return _requestValueCollection != null && isCrossPagePostBack; }
  1340. }
  1341. string ProcessCallbackData ()
  1342. {
  1343. string callbackTarget = _requestValueCollection [CallbackSourceID];
  1344. if (callbackTarget == null || callbackTarget.Length == 0)
  1345. throw new HttpException ("Callback target not provided.");
  1346. ICallbackEventHandler target = FindControl (callbackTarget) as ICallbackEventHandler;
  1347. if (target == null)
  1348. throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
  1349. string callbackArgument = _requestValueCollection [CallbackArgumentID];
  1350. target.RaiseCallbackEvent (callbackArgument);
  1351. return target.GetCallbackResult ();
  1352. }
  1353. [BrowsableAttribute (false)]
  1354. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1355. public HtmlHead Header {
  1356. get { return htmlHeader; }
  1357. }
  1358. internal void SetHeader (HtmlHead header)
  1359. {
  1360. htmlHeader = header;
  1361. }
  1362. void ApplyMasterPage ()
  1363. {
  1364. if (masterPageFile != null) {
  1365. ArrayList appliedMasterPageFiles = new ArrayList ();
  1366. if (Master != null) {
  1367. MasterPage.ApplyMasterPageRecursive (Master, appliedMasterPageFiles);
  1368. Master.Page = this;
  1369. Controls.Clear ();
  1370. Controls.Add (Master);
  1371. }
  1372. }
  1373. }
  1374. [DefaultValueAttribute ("")]
  1375. public virtual string MasterPageFile {
  1376. get { return masterPageFile; }
  1377. set {
  1378. masterPageFile = value;
  1379. masterPage = null;
  1380. }
  1381. }
  1382. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1383. [BrowsableAttribute (false)]
  1384. public MasterPage Master {
  1385. get {
  1386. if (masterPage == null)
  1387. masterPage = MasterPage.CreateMasterPage (this, Context, masterPageFile, contentTemplates);
  1388. return masterPage;
  1389. }
  1390. }
  1391. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1392. public void RegisterRequiresControlState (Control control)
  1393. {
  1394. if (requireStateControls == null) requireStateControls = new ArrayList ();
  1395. requireStateControls.Add (control);
  1396. }
  1397. public bool RequiresControlState (Control control)
  1398. {
  1399. if (requireStateControls == null) return false;
  1400. return requireStateControls.Contains (control);
  1401. }
  1402. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1403. public void UnregisterRequiresControlState (Control control)
  1404. {
  1405. if (requireStateControls != null)
  1406. requireStateControls.Remove (control);
  1407. }
  1408. public ValidatorCollection GetValidators (string validationGroup)
  1409. {
  1410. if (validationGroup == null || validationGroup == "")
  1411. return Validators;
  1412. if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
  1413. ValidatorCollection col = _validatorsByGroup [validationGroup] as ValidatorCollection;
  1414. if (col == null) {
  1415. col = new ValidatorCollection ();
  1416. _validatorsByGroup [validationGroup] = col;
  1417. }
  1418. return col;
  1419. }
  1420. public virtual void Validate (string validationGroup)
  1421. {
  1422. is_validated = true;
  1423. if (validationGroup == null || validationGroup == "")
  1424. ValidateCollection (_validators);
  1425. else if (_validatorsByGroup != null) {
  1426. ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
  1427. }
  1428. }
  1429. object SavePageControlState ()
  1430. {
  1431. if (requireStateControls == null) return null;
  1432. object[] state = new object [requireStateControls.Count];
  1433. bool allNull = true;
  1434. for (int n=0; n<state.Length; n++) {
  1435. state [n] = ((Control) requireStateControls [n]).SaveControlState ();
  1436. if (state [n] != null) allNull = false;
  1437. }
  1438. if (allNull) return null;
  1439. else return state;
  1440. }
  1441. void LoadPageControlState (object data)
  1442. {
  1443. if (requireStateControls == null) return;
  1444. object[] state = (object[]) data;
  1445. int max = Math.Min (requireStateControls.Count, state != null ? state.Length : requireStateControls.Count);
  1446. for (int n=0; n < max; n++) {
  1447. Control ctl = (Control) requireStateControls [n];
  1448. ctl.LoadControlState (state != null ? state [n] : null);
  1449. }
  1450. }
  1451. void LoadPreviousPageReference ()
  1452. {
  1453. if (_requestValueCollection != null) {
  1454. string prevPage = _requestValueCollection [PreviousPageID];
  1455. if (prevPage != null) {
  1456. previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
  1457. previousPage.ProcessCrossPagePostBack (_context);
  1458. } else {
  1459. previousPage = _context.LastPage;
  1460. }
  1461. }
  1462. _context.LastPage = this;
  1463. }
  1464. Hashtable contentTemplates;
  1465. [EditorBrowsable (EditorBrowsableState.Never)]
  1466. protected internal void AddContentTemplate (string templateName, ITemplate template)
  1467. {
  1468. if (contentTemplates == null)
  1469. contentTemplates = new Hashtable ();
  1470. contentTemplates [templateName] = template;
  1471. }
  1472. PageTheme _pageTheme;
  1473. internal PageTheme PageTheme {
  1474. get { return _pageTheme; }
  1475. }
  1476. PageTheme _styleSheetPageTheme;
  1477. internal PageTheme StyleSheetPageTheme {
  1478. get { return _styleSheetPageTheme; }
  1479. }
  1480. Stack dataItemCtx;
  1481. internal void PushDataItemContext (object o) {
  1482. if (dataItemCtx == null)
  1483. dataItemCtx = new Stack ();
  1484. dataItemCtx.Push (o);
  1485. }
  1486. internal void PopDataItemContext () {
  1487. if (dataItemCtx == null)
  1488. throw new InvalidOperationException ();
  1489. dataItemCtx.Pop ();
  1490. }
  1491. public object GetDataItem() {
  1492. if (dataItemCtx == null || dataItemCtx.Count == 0)
  1493. throw new InvalidOperationException ("No data item");
  1494. return dataItemCtx.Peek ();
  1495. }
  1496. protected internal override void OnInit (EventArgs e)
  1497. {
  1498. base.OnInit (e);
  1499. if (Header == null)
  1500. return;
  1501. ArrayList themes = new ArrayList();
  1502. if (StyleSheetPageTheme != null)
  1503. themes.AddRange (StyleSheetPageTheme.GetStyleSheets ());
  1504. if (PageTheme != null)
  1505. themes.AddRange (PageTheme.GetStyleSheets ());
  1506. foreach (string lss in themes) {
  1507. HtmlLink hl = new HtmlLink ();
  1508. hl.Href = lss;
  1509. hl.Attributes["type"] = "text/css";
  1510. hl.Attributes["rel"] = "stylesheet";
  1511. Header.Controls.Add (hl);
  1512. }
  1513. }
  1514. #endif
  1515. }
  1516. }