Page.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  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. MemoryStream ms = new MemoryStream ();
  781. fmt.Serialize (ms, _savedViewState);
  782. return Convert.ToBase64String (ms.GetBuffer (), 0, (int) ms.Length);
  783. }
  784. internal object GetSavedViewState ()
  785. {
  786. return _savedViewState;
  787. }
  788. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  789. {
  790. scriptManager.WriteArrayDeclares (writer);
  791. if (!postBackScriptRendered && requiresPostBackScript)
  792. RenderPostBackScript (writer, formUniqueID);
  793. scriptManager.WriteHiddenFields (writer);
  794. scriptManager.WriteClientScriptIncludes (writer);
  795. scriptManager.WriteStartupScriptBlocks (writer);
  796. renderingForm = false;
  797. postBackScriptRendered = false;
  798. }
  799. private void ProcessPostData (NameValueCollection data, bool second)
  800. {
  801. if (data == null)
  802. return;
  803. if (_requiresPostBackCopy == null && _requiresPostBack != null)
  804. _requiresPostBackCopy = (ArrayList) _requiresPostBack.Clone ();
  805. Hashtable used = new Hashtable ();
  806. foreach (string id in data.AllKeys){
  807. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
  808. continue;
  809. string real_id = id;
  810. int dot = real_id.IndexOf ('.');
  811. if (dot >= 1)
  812. real_id = real_id.Substring (0, dot);
  813. if (real_id == null || used.ContainsKey (real_id))
  814. continue;
  815. used.Add (real_id, real_id);
  816. Control ctrl = FindControl (real_id);
  817. if (ctrl != null){
  818. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  819. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  820. if (pbdh == null) {
  821. if (pbeh != null)
  822. RegisterRequiresRaiseEvent (pbeh);
  823. continue;
  824. }
  825. if (pbdh.LoadPostData (real_id, data) == true) {
  826. if (requiresPostDataChanged == null)
  827. requiresPostDataChanged = new ArrayList ();
  828. requiresPostDataChanged.Add (pbdh);
  829. if (_requiresPostBackCopy != null)
  830. _requiresPostBackCopy.Remove (ctrl.UniqueID);
  831. }
  832. } else if (!second) {
  833. if (secondPostData == null)
  834. secondPostData = new NameValueCollection ();
  835. secondPostData.Add (real_id, data [id]);
  836. }
  837. }
  838. ArrayList list1 = null;
  839. if (_requiresPostBackCopy != null && _requiresPostBackCopy.Count > 0) {
  840. string [] handlers = (string []) _requiresPostBackCopy.ToArray (typeof (string));
  841. foreach (string id in handlers) {
  842. IPostBackDataHandler pbdh = FindControl (id) as IPostBackDataHandler;
  843. if (pbdh != null) {
  844. _requiresPostBackCopy.Remove (id);
  845. if (pbdh.LoadPostData (id, data)) {
  846. if (requiresPostDataChanged == null)
  847. requiresPostDataChanged = new ArrayList ();
  848. requiresPostDataChanged.Add (pbdh);
  849. }
  850. } else if (second) {
  851. if (list1 == null)
  852. list1 = new ArrayList ();
  853. list1.Add (id);
  854. }
  855. }
  856. }
  857. _requiresPostBack = list1;
  858. }
  859. [EditorBrowsable (EditorBrowsableState.Never)]
  860. #if NET_2_0 || TARGET_JVM
  861. public virtual void ProcessRequest (HttpContext context)
  862. #else
  863. public void ProcessRequest (HttpContext context)
  864. #endif
  865. {
  866. _context = context;
  867. if (clientTarget != null)
  868. Request.ClientTarget = clientTarget;
  869. WireupAutomaticEvents ();
  870. //-- Control execution lifecycle in the docs
  871. // Save culture information because it can be modified in FrameworkInitialize()
  872. CultureInfo culture = Thread.CurrentThread.CurrentCulture;
  873. CultureInfo uiculture = Thread.CurrentThread.CurrentUICulture;
  874. FrameworkInitialize ();
  875. context.ErrorPage = _errorPage;
  876. try {
  877. InternalProcessRequest ();
  878. } catch (ThreadAbortException) {
  879. // Do nothing, just ignore it by now.
  880. } catch (Exception e) {
  881. context.AddError (e); // OnError might access LastError
  882. OnError (EventArgs.Empty);
  883. context.ClearError (e);
  884. // We want to remove that error, as we're rethrowing to stop
  885. // further processing.
  886. throw;
  887. } finally {
  888. try {
  889. UnloadRecursive (true);
  890. } catch {}
  891. if (Thread.CurrentThread.CurrentCulture.Equals (culture) == false)
  892. Thread.CurrentThread.CurrentCulture = culture;
  893. if (Thread.CurrentThread.CurrentUICulture.Equals (uiculture) == false)
  894. Thread.CurrentThread.CurrentUICulture = uiculture;
  895. }
  896. }
  897. #if NET_2_0
  898. internal void ProcessCrossPagePostBack (HttpContext context)
  899. {
  900. isCrossPagePostBack = true;
  901. ProcessRequest (context);
  902. }
  903. #endif
  904. void InternalProcessRequest ()
  905. {
  906. _requestValueCollection = this.DeterminePostBackMode();
  907. #if NET_2_0
  908. if (!IsCrossPagePostBack)
  909. LoadPreviousPageReference ();
  910. OnPreInit (EventArgs.Empty);
  911. InitializeTheme ();
  912. ApplyMasterPage ();
  913. #endif
  914. Trace.Write ("aspx.page", "Begin Init");
  915. InitRecursive (null);
  916. Trace.Write ("aspx.page", "End Init");
  917. #if NET_2_0
  918. OnInitComplete (EventArgs.Empty);
  919. if (_title != null && htmlHeader != null)
  920. htmlHeader.Title = _title;
  921. #endif
  922. renderingForm = false;
  923. if (IsPostBack) {
  924. Trace.Write ("aspx.page", "Begin LoadViewState");
  925. LoadPageViewState ();
  926. Trace.Write ("aspx.page", "End LoadViewState");
  927. Trace.Write ("aspx.page", "Begin ProcessPostData");
  928. ProcessPostData (_requestValueCollection, false);
  929. Trace.Write ("aspx.page", "End ProcessPostData");
  930. }
  931. #if NET_2_0
  932. if (IsCrossPagePostBack)
  933. return;
  934. OnPreLoad (EventArgs.Empty);
  935. #endif
  936. LoadRecursive ();
  937. if (IsPostBack) {
  938. Trace.Write ("aspx.page", "Begin ProcessPostData Second Try");
  939. ProcessPostData (secondPostData, true);
  940. Trace.Write ("aspx.page", "End ProcessPostData Second Try");
  941. Trace.Write ("aspx.page", "Begin Raise ChangedEvents");
  942. RaiseChangedEvents ();
  943. Trace.Write ("aspx.page", "End Raise ChangedEvents");
  944. Trace.Write ("aspx.page", "Begin Raise PostBackEvent");
  945. RaisePostBackEvents ();
  946. Trace.Write ("aspx.page", "End Raise PostBackEvent");
  947. }
  948. #if NET_2_0
  949. OnLoadComplete (EventArgs.Empty);
  950. if (IsCallback) {
  951. string result = ProcessCallbackData ();
  952. HtmlTextWriter callbackOutput = new HtmlTextWriter (_context.Response.Output);
  953. callbackOutput.Write (result);
  954. callbackOutput.Flush ();
  955. return;
  956. }
  957. #endif
  958. Trace.Write ("aspx.page", "Begin PreRender");
  959. PreRenderRecursiveInternal ();
  960. Trace.Write ("aspx.page", "End PreRender");
  961. #if NET_2_0
  962. OnPreRenderComplete (EventArgs.Empty);
  963. #endif
  964. Trace.Write ("aspx.page", "Begin SaveViewState");
  965. SavePageViewState ();
  966. Trace.Write ("aspx.page", "End SaveViewState");
  967. #if NET_2_0
  968. OnSaveStateComplete (EventArgs.Empty);
  969. #endif
  970. //--
  971. Trace.Write ("aspx.page", "Begin Render");
  972. HtmlTextWriter output = new HtmlTextWriter (_context.Response.Output);
  973. RenderControl (output);
  974. Trace.Write ("aspx.page", "End Render");
  975. RenderTrace (output);
  976. }
  977. private void RenderTrace (HtmlTextWriter output)
  978. {
  979. TraceManager traceManager = HttpRuntime.TraceManager;
  980. if (Trace.HaveTrace && !Trace.IsEnabled || !Trace.HaveTrace && !traceManager.Enabled)
  981. return;
  982. Trace.SaveData ();
  983. if (!Trace.HaveTrace && traceManager.Enabled && !traceManager.PageOutput)
  984. return;
  985. if (!traceManager.LocalOnly || Context.Request.IsLocal)
  986. Trace.Render (output);
  987. }
  988. void RaisePostBackEvents ()
  989. {
  990. if (requiresRaiseEvent != null) {
  991. RaisePostBackEvent (requiresRaiseEvent, null);
  992. return;
  993. }
  994. NameValueCollection postdata = _requestValueCollection;
  995. if (postdata == null)
  996. return;
  997. string eventTarget = postdata [postEventSourceID];
  998. if (eventTarget == null || eventTarget.Length == 0) {
  999. Validate ();
  1000. return;
  1001. }
  1002. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  1003. if (target == null)
  1004. return;
  1005. string eventArgument = postdata [postEventArgumentID];
  1006. RaisePostBackEvent (target, eventArgument);
  1007. }
  1008. internal void RaiseChangedEvents ()
  1009. {
  1010. if (requiresPostDataChanged == null)
  1011. return;
  1012. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  1013. ipdh.RaisePostDataChangedEvent ();
  1014. requiresPostDataChanged = null;
  1015. }
  1016. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1017. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  1018. {
  1019. sourceControl.RaisePostBackEvent (eventArgument);
  1020. }
  1021. #if NET_2_0
  1022. [Obsolete]
  1023. #endif
  1024. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1025. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  1026. {
  1027. scriptManager.RegisterArrayDeclaration (arrayName, arrayValue);
  1028. }
  1029. #if NET_2_0
  1030. [Obsolete]
  1031. #endif
  1032. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1033. public virtual void RegisterClientScriptBlock (string key, string script)
  1034. {
  1035. scriptManager.RegisterClientScriptBlock (key, script);
  1036. }
  1037. #if NET_2_0
  1038. [Obsolete]
  1039. #endif
  1040. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1041. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  1042. {
  1043. scriptManager.RegisterHiddenField (hiddenFieldName, hiddenFieldInitialValue);
  1044. }
  1045. [MonoTODO("Used in HtmlForm")]
  1046. internal void RegisterClientScriptFile (string a, string b, string c)
  1047. {
  1048. throw new NotImplementedException ();
  1049. }
  1050. #if NET_2_0
  1051. [Obsolete]
  1052. #endif
  1053. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1054. public void RegisterOnSubmitStatement (string key, string script)
  1055. {
  1056. scriptManager.RegisterOnSubmitStatement (key, script);
  1057. }
  1058. internal string GetSubmitStatements ()
  1059. {
  1060. return scriptManager.WriteSubmitStatements ();
  1061. }
  1062. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1063. public void RegisterRequiresPostBack (Control control)
  1064. {
  1065. if (_requiresPostBack == null)
  1066. _requiresPostBack = new ArrayList ();
  1067. _requiresPostBack.Add (control.UniqueID);
  1068. }
  1069. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1070. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  1071. {
  1072. requiresRaiseEvent = control;
  1073. }
  1074. #if NET_2_0
  1075. [Obsolete]
  1076. #endif
  1077. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1078. public virtual void RegisterStartupScript (string key, string script)
  1079. {
  1080. scriptManager.RegisterStartupScript (key, script);
  1081. }
  1082. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1083. public void RegisterViewStateHandler ()
  1084. {
  1085. handleViewState = true;
  1086. }
  1087. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1088. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  1089. {
  1090. _savedViewState = viewState;
  1091. }
  1092. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1093. protected virtual object LoadPageStateFromPersistenceMedium ()
  1094. {
  1095. NameValueCollection postdata = _requestValueCollection;
  1096. string view_state;
  1097. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  1098. return null;
  1099. _savedViewState = null;
  1100. if (view_state == "")
  1101. return null;
  1102. LosFormatter fmt = GetFormatter ();
  1103. try {
  1104. _savedViewState = fmt.Deserialize (view_state);
  1105. } catch (Exception e) {
  1106. throw new HttpException ("Error restoring page viewstate.", e);
  1107. }
  1108. return _savedViewState;
  1109. }
  1110. internal void LoadPageViewState()
  1111. {
  1112. object sState = LoadPageStateFromPersistenceMedium ();
  1113. if (sState != null) {
  1114. #if NET_2_0
  1115. Triplet data = (Triplet) sState;
  1116. if (allow_load) {
  1117. LoadPageControlState (data.Third);
  1118. LoadViewStateRecursive (data.First);
  1119. _requiresPostBack = data.Second as ArrayList;
  1120. }
  1121. #else
  1122. Pair pair = (Pair) sState;
  1123. if (allow_load) {
  1124. LoadViewStateRecursive (pair.First);
  1125. _requiresPostBack = pair.Second as ArrayList;
  1126. }
  1127. #endif
  1128. }
  1129. }
  1130. internal void SavePageViewState ()
  1131. {
  1132. if (!handleViewState)
  1133. return;
  1134. #if NET_2_0
  1135. object controlState = SavePageControlState ();
  1136. #endif
  1137. object viewState = SaveViewStateRecursive ();
  1138. object reqPostback = (_requiresPostBack != null && _requiresPostBack.Count > 0) ? _requiresPostBack : null;
  1139. #if NET_2_0
  1140. Triplet triplet = new Triplet ();
  1141. triplet.First = viewState;
  1142. triplet.Second = reqPostback;
  1143. triplet.Third = controlState;
  1144. if (triplet.First == null && triplet.Second == null && triplet.Third == null)
  1145. triplet = null;
  1146. SavePageStateToPersistenceMedium (triplet);
  1147. #else
  1148. Pair pair = new Pair ();
  1149. pair.First = viewState;
  1150. pair.Second = reqPostback;
  1151. if (pair.First == null && pair.Second == null)
  1152. pair = null;
  1153. SavePageStateToPersistenceMedium (pair);
  1154. #endif
  1155. }
  1156. public virtual void Validate ()
  1157. {
  1158. is_validated = true;
  1159. ValidateCollection (_validators);
  1160. }
  1161. internal virtual bool AreValidatorsUplevel ()
  1162. {
  1163. bool uplevel = false;
  1164. foreach (IValidator v in Validators) {
  1165. BaseValidator bv = v as BaseValidator;
  1166. if (bv == null) continue;
  1167. if (bv.GetRenderUplevel()) {
  1168. uplevel = true;
  1169. break;
  1170. }
  1171. }
  1172. return uplevel;
  1173. }
  1174. bool ValidateCollection (ValidatorCollection validators)
  1175. {
  1176. #if NET_2_0
  1177. if (!_eventValidation)
  1178. return true;
  1179. #endif
  1180. if (validators == null || validators.Count == 0)
  1181. return true;
  1182. bool all_valid = true;
  1183. foreach (IValidator v in validators){
  1184. v.Validate ();
  1185. if (v.IsValid == false)
  1186. all_valid = false;
  1187. }
  1188. return all_valid;
  1189. }
  1190. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1191. public virtual void VerifyRenderingInServerForm (Control control)
  1192. {
  1193. if (_context == null)
  1194. return;
  1195. if (!renderingForm)
  1196. throw new HttpException ("Control '" +
  1197. control.ClientID +
  1198. "' of type '" +
  1199. control.GetType ().Name +
  1200. "' must be placed inside a form tag with runat=server.");
  1201. }
  1202. protected override void FrameworkInitialize ()
  1203. {
  1204. base.FrameworkInitialize ();
  1205. #if NET_2_0
  1206. InitializeStyleSheet ();
  1207. #endif
  1208. }
  1209. #endregion
  1210. #if NET_2_0
  1211. public
  1212. #else
  1213. internal
  1214. #endif
  1215. ClientScriptManager ClientScript {
  1216. get { return scriptManager; }
  1217. }
  1218. #if NET_2_0
  1219. static readonly object InitCompleteEvent = new object ();
  1220. static readonly object LoadCompleteEvent = new object ();
  1221. static readonly object PreInitEvent = new object ();
  1222. static readonly object PreLoadEvent = new object ();
  1223. static readonly object PreRenderCompleteEvent = new object ();
  1224. static readonly object SaveStateCompleteEvent = new object ();
  1225. int event_mask;
  1226. const int initcomplete_mask = 1;
  1227. const int loadcomplete_mask = 1 << 1;
  1228. const int preinit_mask = 1 << 2;
  1229. const int preload_mask = 1 << 3;
  1230. const int prerendercomplete_mask = 1 << 4;
  1231. const int savestatecomplete_mask = 1 << 5;
  1232. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1233. public event EventHandler InitComplete {
  1234. add {
  1235. event_mask |= initcomplete_mask;
  1236. Events.AddHandler (InitCompleteEvent, value);
  1237. }
  1238. remove { Events.RemoveHandler (InitCompleteEvent, value); }
  1239. }
  1240. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1241. public event EventHandler LoadComplete {
  1242. add {
  1243. event_mask |= loadcomplete_mask;
  1244. Events.AddHandler (LoadCompleteEvent, value);
  1245. }
  1246. remove { Events.RemoveHandler (LoadCompleteEvent, value); }
  1247. }
  1248. public event EventHandler PreInit {
  1249. add {
  1250. event_mask |= preinit_mask;
  1251. Events.AddHandler (PreInitEvent, value);
  1252. }
  1253. remove { Events.RemoveHandler (PreInitEvent, value); }
  1254. }
  1255. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1256. public event EventHandler PreLoad {
  1257. add {
  1258. event_mask |= preload_mask;
  1259. Events.AddHandler (PreLoadEvent, value);
  1260. }
  1261. remove { Events.RemoveHandler (PreLoadEvent, value); }
  1262. }
  1263. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1264. public event EventHandler PreRenderComplete {
  1265. add {
  1266. event_mask |= prerendercomplete_mask;
  1267. Events.AddHandler (PreRenderCompleteEvent, value);
  1268. }
  1269. remove { Events.RemoveHandler (PreRenderCompleteEvent, value); }
  1270. }
  1271. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1272. public event EventHandler SaveStateComplete {
  1273. add {
  1274. event_mask |= savestatecomplete_mask;
  1275. Events.AddHandler (SaveStateCompleteEvent, value);
  1276. }
  1277. remove { Events.RemoveHandler (SaveStateCompleteEvent, value); }
  1278. }
  1279. protected virtual void OnInitComplete (EventArgs e)
  1280. {
  1281. if ((event_mask & initcomplete_mask) != 0) {
  1282. EventHandler eh = (EventHandler) (Events [InitCompleteEvent]);
  1283. if (eh != null) eh (this, e);
  1284. }
  1285. }
  1286. protected virtual void OnLoadComplete (EventArgs e)
  1287. {
  1288. if ((event_mask & loadcomplete_mask) != 0) {
  1289. EventHandler eh = (EventHandler) (Events [LoadCompleteEvent]);
  1290. if (eh != null) eh (this, e);
  1291. }
  1292. }
  1293. protected virtual void OnPreInit (EventArgs e)
  1294. {
  1295. if ((event_mask & preinit_mask) != 0) {
  1296. EventHandler eh = (EventHandler) (Events [PreInitEvent]);
  1297. if (eh != null) eh (this, e);
  1298. }
  1299. }
  1300. protected virtual void OnPreLoad (EventArgs e)
  1301. {
  1302. if ((event_mask & preload_mask) != 0) {
  1303. EventHandler eh = (EventHandler) (Events [PreLoadEvent]);
  1304. if (eh != null) eh (this, e);
  1305. }
  1306. }
  1307. protected virtual void OnPreRenderComplete (EventArgs e)
  1308. {
  1309. if ((event_mask & prerendercomplete_mask) != 0) {
  1310. EventHandler eh = (EventHandler) (Events [PreRenderCompleteEvent]);
  1311. if (eh != null) eh (this, e);
  1312. }
  1313. }
  1314. protected virtual void OnSaveStateComplete (EventArgs e)
  1315. {
  1316. if ((event_mask & savestatecomplete_mask) != 0) {
  1317. EventHandler eh = (EventHandler) (Events [SaveStateCompleteEvent]);
  1318. if (eh != null) eh (this, e);
  1319. }
  1320. }
  1321. public HtmlForm Form {
  1322. get { return _form; }
  1323. }
  1324. internal void RegisterForm (HtmlForm form)
  1325. {
  1326. _form = form;
  1327. }
  1328. [BrowsableAttribute (false)]
  1329. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1330. public Page PreviousPage {
  1331. get { return previousPage; }
  1332. }
  1333. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1334. [BrowsableAttribute (false)]
  1335. public bool IsCallback {
  1336. get { return _requestValueCollection != null && _requestValueCollection [CallbackArgumentID] != null; }
  1337. }
  1338. [BrowsableAttribute (false)]
  1339. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1340. public bool IsCrossPagePostBack {
  1341. get { return _requestValueCollection != null && isCrossPagePostBack; }
  1342. }
  1343. string ProcessCallbackData ()
  1344. {
  1345. string callbackTarget = _requestValueCollection [CallbackSourceID];
  1346. if (callbackTarget == null || callbackTarget.Length == 0)
  1347. throw new HttpException ("Callback target not provided.");
  1348. ICallbackEventHandler target = FindControl (callbackTarget) as ICallbackEventHandler;
  1349. if (target == null)
  1350. throw new HttpException (string.Format ("Invalid callback target '{0}'.", callbackTarget));
  1351. string callbackArgument = _requestValueCollection [CallbackArgumentID];
  1352. target.RaiseCallbackEvent (callbackArgument);
  1353. return target.GetCallbackResult ();
  1354. }
  1355. [BrowsableAttribute (false)]
  1356. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1357. public HtmlHead Header {
  1358. get { return htmlHeader; }
  1359. }
  1360. internal void SetHeader (HtmlHead header)
  1361. {
  1362. htmlHeader = header;
  1363. }
  1364. void ApplyMasterPage ()
  1365. {
  1366. if (masterPageFile != null) {
  1367. ArrayList appliedMasterPageFiles = new ArrayList ();
  1368. if (Master != null) {
  1369. MasterPage.ApplyMasterPageRecursive (Master, appliedMasterPageFiles);
  1370. Master.Page = this;
  1371. Controls.Clear ();
  1372. Controls.Add (Master);
  1373. }
  1374. }
  1375. }
  1376. [DefaultValueAttribute ("")]
  1377. public virtual string MasterPageFile {
  1378. get { return masterPageFile; }
  1379. set {
  1380. masterPageFile = value;
  1381. masterPage = null;
  1382. }
  1383. }
  1384. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  1385. [BrowsableAttribute (false)]
  1386. public MasterPage Master {
  1387. get {
  1388. if (masterPage == null)
  1389. masterPage = MasterPage.CreateMasterPage (this, Context, masterPageFile, contentTemplates);
  1390. return masterPage;
  1391. }
  1392. }
  1393. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1394. public void RegisterRequiresControlState (Control control)
  1395. {
  1396. if (requireStateControls == null) requireStateControls = new ArrayList ();
  1397. requireStateControls.Add (control);
  1398. }
  1399. public bool RequiresControlState (Control control)
  1400. {
  1401. if (requireStateControls == null) return false;
  1402. return requireStateControls.Contains (control);
  1403. }
  1404. [EditorBrowsable (EditorBrowsableState.Advanced)]
  1405. public void UnregisterRequiresControlState (Control control)
  1406. {
  1407. if (requireStateControls != null)
  1408. requireStateControls.Remove (control);
  1409. }
  1410. public ValidatorCollection GetValidators (string validationGroup)
  1411. {
  1412. if (validationGroup == null || validationGroup == "")
  1413. return Validators;
  1414. if (_validatorsByGroup == null) _validatorsByGroup = new Hashtable ();
  1415. ValidatorCollection col = _validatorsByGroup [validationGroup] as ValidatorCollection;
  1416. if (col == null) {
  1417. col = new ValidatorCollection ();
  1418. _validatorsByGroup [validationGroup] = col;
  1419. }
  1420. return col;
  1421. }
  1422. public virtual void Validate (string validationGroup)
  1423. {
  1424. is_validated = true;
  1425. if (validationGroup == null || validationGroup == "")
  1426. ValidateCollection (_validators);
  1427. else if (_validatorsByGroup != null) {
  1428. ValidateCollection (_validatorsByGroup [validationGroup] as ValidatorCollection);
  1429. }
  1430. }
  1431. object SavePageControlState ()
  1432. {
  1433. if (requireStateControls == null) return null;
  1434. object[] state = new object [requireStateControls.Count];
  1435. bool allNull = true;
  1436. for (int n=0; n<state.Length; n++) {
  1437. state [n] = ((Control) requireStateControls [n]).SaveControlState ();
  1438. if (state [n] != null) allNull = false;
  1439. }
  1440. if (allNull) return null;
  1441. else return state;
  1442. }
  1443. void LoadPageControlState (object data)
  1444. {
  1445. if (requireStateControls == null) return;
  1446. object[] state = (object[]) data;
  1447. int max = Math.Min (requireStateControls.Count, state != null ? state.Length : requireStateControls.Count);
  1448. for (int n=0; n < max; n++) {
  1449. Control ctl = (Control) requireStateControls [n];
  1450. ctl.LoadControlState (state != null ? state [n] : null);
  1451. }
  1452. }
  1453. void LoadPreviousPageReference ()
  1454. {
  1455. if (_requestValueCollection != null) {
  1456. string prevPage = _requestValueCollection [PreviousPageID];
  1457. if (prevPage != null) {
  1458. previousPage = (Page) PageParser.GetCompiledPageInstance (prevPage, Server.MapPath (prevPage), Context);
  1459. previousPage.ProcessCrossPagePostBack (_context);
  1460. } else {
  1461. previousPage = _context.LastPage;
  1462. }
  1463. }
  1464. _context.LastPage = this;
  1465. }
  1466. Hashtable contentTemplates;
  1467. [EditorBrowsable (EditorBrowsableState.Never)]
  1468. protected internal void AddContentTemplate (string templateName, ITemplate template)
  1469. {
  1470. if (contentTemplates == null)
  1471. contentTemplates = new Hashtable ();
  1472. contentTemplates [templateName] = template;
  1473. }
  1474. PageTheme _pageTheme;
  1475. internal PageTheme PageTheme {
  1476. get { return _pageTheme; }
  1477. }
  1478. PageTheme _styleSheetPageTheme;
  1479. internal PageTheme StyleSheetPageTheme {
  1480. get { return _styleSheetPageTheme; }
  1481. }
  1482. Stack dataItemCtx;
  1483. internal void PushDataItemContext (object o) {
  1484. if (dataItemCtx == null)
  1485. dataItemCtx = new Stack ();
  1486. dataItemCtx.Push (o);
  1487. }
  1488. internal void PopDataItemContext () {
  1489. if (dataItemCtx == null)
  1490. throw new InvalidOperationException ();
  1491. dataItemCtx.Pop ();
  1492. }
  1493. public object GetDataItem() {
  1494. if (dataItemCtx == null || dataItemCtx.Count == 0)
  1495. throw new InvalidOperationException ("No data item");
  1496. return dataItemCtx.Peek ();
  1497. }
  1498. protected internal override void OnInit (EventArgs e)
  1499. {
  1500. base.OnInit (e);
  1501. if (Header == null)
  1502. return;
  1503. ArrayList themes = new ArrayList();
  1504. if (StyleSheetPageTheme != null)
  1505. themes.AddRange (StyleSheetPageTheme.GetStyleSheets ());
  1506. if (PageTheme != null)
  1507. themes.AddRange (PageTheme.GetStyleSheets ());
  1508. foreach (string lss in themes) {
  1509. HtmlLink hl = new HtmlLink ();
  1510. hl.Href = lss;
  1511. hl.Attributes["type"] = "text/css";
  1512. hl.Attributes["rel"] = "stylesheet";
  1513. Header.Controls.Add (hl);
  1514. }
  1515. }
  1516. #endif
  1517. }
  1518. }