Page.cs 48 KB

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