Page.cs 42 KB

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