Page.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  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. //
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Specialized;
  14. using System.ComponentModel;
  15. using System.ComponentModel.Design;
  16. using System.ComponentModel.Design.Serialization;
  17. using System.IO;
  18. using System.Security.Principal;
  19. using System.Text;
  20. using System.Web;
  21. using System.Web.Caching;
  22. using System.Web.SessionState;
  23. using System.Web.Util;
  24. namespace System.Web.UI
  25. {
  26. // TODO FIXME missing the IRootDesigner Attribute
  27. [DefaultEvent ("Load"), DesignerCategory ("ASPXCodeBehind")]
  28. [ToolboxItem (false)]
  29. [Designer ("System.Web.UI.Design.ControlDesigner, " + Consts.AssemblySystem_Design, typeof (IDesigner))]
  30. [RootDesignerSerializer ("Microsoft.VSDesigner.WebForms.RootCodeDomSerializer, " + Consts.AssemblyMicrosoft_VSDesigner, "System.ComponentModel.Design.Serialization.CodeDomSerializer, " + Consts.AssemblySystem_Design, true)]
  31. public class Page : TemplateControl, IHttpHandler
  32. {
  33. private string _culture;
  34. private bool _viewState = true;
  35. private bool _viewStateMac = false;
  36. private string _errorPage;
  37. private string _ID;
  38. private bool _isValid;
  39. private bool _smartNavigation = false;
  40. private TraceContext _trace;
  41. private bool _traceEnabled;
  42. private TraceMode _traceModeValue;
  43. private int _transactionMode;
  44. private string _UICulture;
  45. private HttpContext _context;
  46. private ValidatorCollection _validators;
  47. private bool renderingForm;
  48. private object _savedViewState;
  49. private ArrayList _requiresPostBack;
  50. private ArrayList requiresPostDataChanged;
  51. private IPostBackEventHandler requiresRaiseEvent;
  52. private NameValueCollection secondPostData;
  53. private bool requiresPostBackScript = false;
  54. private bool postBackScriptRendered = false;
  55. Hashtable clientScriptBlocks;
  56. Hashtable startupScriptBlocks;
  57. Hashtable hiddenFields;
  58. bool handleViewState;
  59. [EditorBrowsable (EditorBrowsableState.Never)]
  60. protected const string postEventArgumentID = "__EVENTARGUMENT";
  61. [EditorBrowsable (EditorBrowsableState.Never)]
  62. protected const string postEventSourceID = "__EVENTTARGET";
  63. #region Constructor
  64. public Page ()
  65. {
  66. Page = this;
  67. }
  68. #endregion
  69. #region Properties
  70. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  71. [Browsable (false)]
  72. public HttpApplicationState Application
  73. {
  74. get { return _context.Application; }
  75. }
  76. [EditorBrowsable (EditorBrowsableState.Never)]
  77. protected bool AspCompatMode
  78. {
  79. set { throw new NotImplementedException (); }
  80. }
  81. [EditorBrowsable (EditorBrowsableState.Never)]
  82. protected bool Buffer
  83. {
  84. set { Response.BufferOutput = value; }
  85. }
  86. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  87. [Browsable (false)]
  88. public Cache Cache
  89. {
  90. get { return _context.Cache; }
  91. }
  92. [MonoTODO]
  93. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  94. [Browsable (false), DefaultValue ("")]
  95. [WebSysDescription ("Value do override the automatic browser detection and force the page to use the specified browser.")]
  96. public string ClientTarget
  97. {
  98. get { throw new NotImplementedException (); }
  99. set { throw new NotImplementedException (); }
  100. }
  101. [MonoTODO]
  102. [EditorBrowsable (EditorBrowsableState.Never)]
  103. protected int CodePage
  104. {
  105. set { throw new NotImplementedException (); }
  106. }
  107. [MonoTODO]
  108. [EditorBrowsable (EditorBrowsableState.Never)]
  109. protected string ContentType
  110. {
  111. set { throw new NotImplementedException (); }
  112. }
  113. protected override HttpContext Context
  114. {
  115. get { return _context; }
  116. }
  117. [EditorBrowsable (EditorBrowsableState.Never)]
  118. protected string Culture
  119. {
  120. set { _culture = value; }
  121. }
  122. [Browsable (false)]
  123. public override bool EnableViewState
  124. {
  125. get { return _viewState; }
  126. set { _viewState = value; }
  127. }
  128. [EditorBrowsable (EditorBrowsableState.Never)]
  129. protected bool EnableViewStateMac
  130. {
  131. get { return _viewStateMac; }
  132. set { _viewStateMac = value; }
  133. }
  134. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  135. [Browsable (false), DefaultValue ("")]
  136. [WebSysDescription ("The URL of a page used for error redirection.")]
  137. public string ErrorPage
  138. {
  139. get { return _errorPage; }
  140. set { _errorPage = value; }
  141. }
  142. [EditorBrowsable (EditorBrowsableState.Never)]
  143. protected ArrayList FileDependencies
  144. {
  145. set {
  146. if (Response != null)
  147. Response.AddFileDependencies (value);
  148. }
  149. }
  150. [Browsable (false)]
  151. public override string ID
  152. {
  153. get { return _ID; }
  154. set { _ID = value; }
  155. }
  156. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  157. [Browsable (false)]
  158. public bool IsPostBack
  159. {
  160. get {
  161. return (0 == String.Compare (Request.HttpMethod, "POST", true));
  162. }
  163. }
  164. [EditorBrowsable (EditorBrowsableState.Never), Browsable (false)]
  165. public bool IsReusable {
  166. get { return false; }
  167. }
  168. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  169. [Browsable (false)]
  170. public bool IsValid
  171. {
  172. get { return _isValid; }
  173. }
  174. [MonoTODO]
  175. [EditorBrowsable (EditorBrowsableState.Never)]
  176. protected int LCID {
  177. set { throw new NotImplementedException (); }
  178. }
  179. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  180. [Browsable (false)]
  181. public HttpRequest Request
  182. {
  183. get { return _context.Request; }
  184. }
  185. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  186. [Browsable (false)]
  187. public HttpResponse Response
  188. {
  189. get { return _context.Response; }
  190. }
  191. [EditorBrowsable (EditorBrowsableState.Never)]
  192. protected string ResponseEncoding
  193. {
  194. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  195. }
  196. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  197. [Browsable (false)]
  198. public HttpServerUtility Server
  199. {
  200. get {
  201. return Context.Server;
  202. }
  203. }
  204. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  205. [Browsable (false)]
  206. public virtual HttpSessionState Session
  207. {
  208. get { return _context.Session; }
  209. }
  210. [Browsable (false)]
  211. public bool SmartNavigation
  212. {
  213. get { return _smartNavigation; }
  214. set { _smartNavigation = value; }
  215. }
  216. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  217. [Browsable (false)]
  218. public TraceContext Trace
  219. {
  220. get { return _trace; }
  221. }
  222. [EditorBrowsable (EditorBrowsableState.Never)]
  223. protected bool TraceEnabled
  224. {
  225. set { _traceEnabled = value; }
  226. }
  227. [EditorBrowsable (EditorBrowsableState.Never)]
  228. protected TraceMode TraceModeValue
  229. {
  230. set { _traceModeValue = value; }
  231. }
  232. [EditorBrowsable (EditorBrowsableState.Never)]
  233. protected int TransactionMode
  234. {
  235. set { _transactionMode = value; }
  236. }
  237. [EditorBrowsable (EditorBrowsableState.Never)]
  238. protected string UICulture
  239. {
  240. set { _UICulture = value; }
  241. }
  242. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  243. [Browsable (false)]
  244. public IPrincipal User
  245. {
  246. get { return _context.User; }
  247. }
  248. [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
  249. [Browsable (false)]
  250. public ValidatorCollection Validators
  251. {
  252. get {
  253. if (_validators == null)
  254. _validators = new ValidatorCollection ();
  255. return _validators;
  256. }
  257. }
  258. [Browsable (false)]
  259. public override bool Visible
  260. {
  261. get { return base.Visible; }
  262. set { base.Visible = value; }
  263. }
  264. #endregion
  265. #region Methods
  266. [MonoTODO]
  267. [EditorBrowsable (EditorBrowsableState.Never)]
  268. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  269. AsyncCallback cb,
  270. object extraData)
  271. {
  272. throw new NotImplementedException ();
  273. }
  274. [MonoTODO]
  275. [EditorBrowsable (EditorBrowsableState.Never)]
  276. protected void AspCompatEndProcessRequest (IAsyncResult result)
  277. {
  278. throw new NotImplementedException ();
  279. }
  280. [EditorBrowsable (EditorBrowsableState.Advanced)]
  281. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  282. {
  283. return new HtmlTextWriter (tw);
  284. }
  285. [MonoTODO]
  286. [EditorBrowsable (EditorBrowsableState.Never)]
  287. public void DesignerInitialize ()
  288. {
  289. throw new NotImplementedException ();
  290. }
  291. [EditorBrowsable (EditorBrowsableState.Advanced)]
  292. protected virtual NameValueCollection DeterminePostBackMode ()
  293. {
  294. if (_context == null)
  295. return null;
  296. HttpRequest req = _context.Request;
  297. if (req == null)
  298. return null;
  299. NameValueCollection coll = null;
  300. if (IsPostBack)
  301. coll = req.Form;
  302. else
  303. coll = req.QueryString;
  304. if (coll == null || coll ["__VIEWSTATE"] == null)
  305. return null;
  306. return coll;
  307. }
  308. [EditorBrowsable (EditorBrowsableState.Advanced)]
  309. public string GetPostBackClientEvent (Control control, string argument)
  310. {
  311. return GetPostBackEventReference (control, argument);
  312. }
  313. [EditorBrowsable (EditorBrowsableState.Advanced)]
  314. public string GetPostBackClientHyperlink (Control control, string argument)
  315. {
  316. return "javascript:" + GetPostBackEventReference (control, argument);
  317. }
  318. [EditorBrowsable (EditorBrowsableState.Advanced)]
  319. public string GetPostBackEventReference (Control control)
  320. {
  321. return GetPostBackEventReference (control, "");
  322. }
  323. [EditorBrowsable (EditorBrowsableState.Advanced)]
  324. public string GetPostBackEventReference (Control control, string argument)
  325. {
  326. RequiresPostBackScript ();
  327. return String.Format ("__doPostBack ('{0}', '{1}')", control.UniqueID, argument);
  328. }
  329. internal void RequiresPostBackScript ()
  330. {
  331. requiresPostBackScript = true;
  332. }
  333. [EditorBrowsable (EditorBrowsableState.Never)]
  334. public virtual int GetTypeHashCode ()
  335. {
  336. return 0;
  337. }
  338. [MonoTODO]
  339. [EditorBrowsable (EditorBrowsableState.Never)]
  340. protected virtual void InitOutputCache (int duration,
  341. string varyByHeader,
  342. string varyByCustom,
  343. OutputCacheLocation location,
  344. string varyByParam)
  345. {
  346. throw new NotImplementedException ();
  347. }
  348. [EditorBrowsable (EditorBrowsableState.Advanced)]
  349. public bool IsClientScriptBlockRegistered (string key)
  350. {
  351. if (clientScriptBlocks == null)
  352. return false;
  353. return clientScriptBlocks.ContainsKey (key);
  354. }
  355. [EditorBrowsable (EditorBrowsableState.Advanced)]
  356. public bool IsStartupScriptRegistered (string key)
  357. {
  358. if (startupScriptBlocks == null)
  359. return false;
  360. return startupScriptBlocks.ContainsKey (key);
  361. }
  362. public string MapPath (string virtualPath)
  363. {
  364. return Request.MapPath (virtualPath);
  365. }
  366. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  367. {
  368. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventSourceID);
  369. writer.WriteLine ("<input type=\"hidden\" name=\"{0}\" value=\"\" />", postEventArgumentID);
  370. writer.WriteLine ();
  371. writer.WriteLine ("<script language=\"javascript\">");
  372. writer.WriteLine ("<!--");
  373. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  374. writer.WriteLine ("\t\tvar theform = document.{0};", formUniqueID);
  375. writer.WriteLine ("\t\ttheform.{0}.value = eventTarget;", postEventSourceID);
  376. writer.WriteLine ("\t\ttheform.{0}.value = eventArgument;", postEventArgumentID);
  377. writer.WriteLine ("\t\ttheform.submit();");
  378. writer.WriteLine ("\t}");
  379. writer.WriteLine ("// -->");
  380. writer.WriteLine ("</script>");
  381. }
  382. static void WriteScripts (HtmlTextWriter writer, Hashtable scripts)
  383. {
  384. if (scripts == null)
  385. return;
  386. foreach (string key in scripts.Values)
  387. writer.WriteLine (key);
  388. }
  389. void WriteHiddenFields (HtmlTextWriter writer)
  390. {
  391. if (hiddenFields == null)
  392. return;
  393. foreach (string key in hiddenFields.Keys) {
  394. string value = hiddenFields [key] as string;
  395. writer.WriteLine ("\n<input type=\"hidden\" name=\"{0}\" value=\"{1}\" />", key, value);
  396. }
  397. hiddenFields = null;
  398. }
  399. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  400. {
  401. if (renderingForm)
  402. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  403. renderingForm = true;
  404. writer.WriteLine ();
  405. WriteHiddenFields (writer);
  406. if (requiresPostBackScript) {
  407. RenderPostBackScript (writer, formUniqueID);
  408. postBackScriptRendered = true;
  409. }
  410. if (handleViewState) {
  411. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  412. writer.WriteLine ("value=\"{0}\" />", GetViewStateString ());
  413. }
  414. WriteScripts (writer, clientScriptBlocks);
  415. }
  416. internal string GetViewStateString ()
  417. {
  418. StringWriter sr = new StringWriter ();
  419. LosFormatter fmt = new LosFormatter ();
  420. fmt.Serialize (sr, _savedViewState);
  421. return sr.GetStringBuilder ().ToString ();
  422. }
  423. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  424. {
  425. if (!postBackScriptRendered && requiresPostBackScript)
  426. RenderPostBackScript (writer, formUniqueID);
  427. WriteHiddenFields (writer);
  428. WriteScripts (writer, startupScriptBlocks);
  429. renderingForm = false;
  430. postBackScriptRendered = false;
  431. }
  432. private void ProcessPostData (NameValueCollection data, bool second)
  433. {
  434. if (data == null)
  435. return;
  436. Hashtable used = new Hashtable ();
  437. foreach (string id in data.AllKeys){
  438. if (id == "__VIEWSTATE" || id == postEventSourceID || id == postEventArgumentID)
  439. continue;
  440. string real_id = id;
  441. int dot = real_id.IndexOf ('.');
  442. if (dot >= 1)
  443. real_id = real_id.Substring (0, dot);
  444. if (real_id == null || used.ContainsKey (real_id))
  445. continue;
  446. used.Add (real_id, real_id);
  447. Control ctrl = FindControl (real_id);
  448. if (ctrl != null){
  449. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  450. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  451. if (pbdh == null) {
  452. if (pbeh != null)
  453. RegisterRequiresRaiseEvent (pbeh);
  454. continue;
  455. }
  456. if (pbdh.LoadPostData (real_id, data) == true) {
  457. if (requiresPostDataChanged == null)
  458. requiresPostDataChanged = new ArrayList ();
  459. requiresPostDataChanged.Add (pbdh);
  460. }
  461. } else if (!second) {
  462. if (secondPostData == null)
  463. secondPostData = new NameValueCollection ();
  464. secondPostData.Add (real_id, data [id]);
  465. }
  466. }
  467. }
  468. [EditorBrowsable (EditorBrowsableState.Never)]
  469. public void ProcessRequest (HttpContext context)
  470. {
  471. _context = context;
  472. WebTrace.PushContext ("Page.ProcessRequest ()");
  473. WebTrace.WriteLine ("Entering");
  474. WireupAutomaticEvents ();
  475. WebTrace.WriteLine ("Finished hookup");
  476. //-- Control execution lifecycle in the docs
  477. WebTrace.WriteLine ("FrameworkInitialize");
  478. FrameworkInitialize ();
  479. WebTrace.WriteLine ("InitRecursive");
  480. InitRecursive (null);
  481. renderingForm = false;
  482. if (IsPostBack) {
  483. LoadPageViewState ();
  484. ProcessPostData (DeterminePostBackMode (), false);
  485. }
  486. WebTrace.WriteLine ("LoadRecursive");
  487. LoadRecursive ();
  488. if (IsPostBack) {
  489. ProcessPostData (secondPostData, true);
  490. RaiseChangedEvents ();
  491. RaisePostBackEvents ();
  492. }
  493. WebTrace.WriteLine ("PreRenderRecursiveInternal");
  494. PreRenderRecursiveInternal ();
  495. WebTrace.WriteLine ("SavePageViewState");
  496. SavePageViewState ();
  497. //--
  498. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  499. WebTrace.WriteLine ("RenderControl");
  500. RenderControl (output);
  501. _context = null;
  502. WebTrace.WriteLine ("UnloadRecursive");
  503. UnloadRecursive (true);
  504. WebTrace.WriteLine ("End");
  505. WebTrace.PopContext ();
  506. }
  507. internal void RaisePostBackEvents ()
  508. {
  509. if (requiresRaiseEvent != null) {
  510. RaisePostBackEvent (requiresRaiseEvent, null);
  511. return;
  512. }
  513. NameValueCollection postdata = DeterminePostBackMode ();
  514. if (postdata == null)
  515. return;
  516. string eventTarget = postdata [postEventSourceID];
  517. if (eventTarget == null || eventTarget.Length == 0)
  518. return;
  519. IPostBackEventHandler target = FindControl (eventTarget) as IPostBackEventHandler;
  520. if (target == null)
  521. return;
  522. string eventArgument = postdata [postEventArgumentID];
  523. RaisePostBackEvent (target, eventArgument);
  524. }
  525. internal void RaiseChangedEvents ()
  526. {
  527. if (requiresPostDataChanged == null)
  528. return;
  529. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  530. ipdh.RaisePostDataChangedEvent ();
  531. requiresPostDataChanged.Clear ();
  532. }
  533. [EditorBrowsable (EditorBrowsableState.Advanced)]
  534. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  535. {
  536. sourceControl.RaisePostBackEvent (eventArgument);
  537. }
  538. [MonoTODO]
  539. [EditorBrowsable (EditorBrowsableState.Advanced)]
  540. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  541. {
  542. throw new NotImplementedException ();
  543. }
  544. [EditorBrowsable (EditorBrowsableState.Advanced)]
  545. public virtual void RegisterClientScriptBlock (string key, string script)
  546. {
  547. if (IsClientScriptBlockRegistered (key))
  548. return;
  549. if (clientScriptBlocks == null)
  550. clientScriptBlocks = new Hashtable ();
  551. clientScriptBlocks.Add (key, script);
  552. }
  553. [EditorBrowsable (EditorBrowsableState.Advanced)]
  554. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  555. {
  556. if (hiddenFields == null)
  557. hiddenFields = new Hashtable ();
  558. if (!hiddenFields.ContainsKey (hiddenFieldName))
  559. hiddenFields.Add (hiddenFieldName, hiddenFieldInitialValue);
  560. }
  561. [MonoTODO]
  562. public void RegisterClientScriptFile (string a, string b, string c)
  563. {
  564. throw new NotImplementedException ();
  565. }
  566. [MonoTODO]
  567. [EditorBrowsable (EditorBrowsableState.Advanced)]
  568. public void RegisterOnSubmitStatement (string key, string script)
  569. {
  570. throw new NotImplementedException ();
  571. }
  572. [EditorBrowsable (EditorBrowsableState.Advanced)]
  573. public void RegisterRequiresPostBack (Control control)
  574. {
  575. if (_requiresPostBack == null)
  576. _requiresPostBack = new ArrayList ();
  577. _requiresPostBack.Add (control.ID);
  578. }
  579. [EditorBrowsable (EditorBrowsableState.Advanced)]
  580. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  581. {
  582. requiresRaiseEvent = control;
  583. }
  584. [EditorBrowsable (EditorBrowsableState.Advanced)]
  585. public virtual void RegisterStartupScript (string key, string script)
  586. {
  587. if (IsStartupScriptRegistered (key))
  588. return;
  589. if (startupScriptBlocks == null)
  590. startupScriptBlocks = new Hashtable ();
  591. startupScriptBlocks.Add (key, script);
  592. }
  593. [EditorBrowsable (EditorBrowsableState.Advanced)]
  594. public void RegisterViewStateHandler ()
  595. {
  596. handleViewState = true;
  597. }
  598. [EditorBrowsable (EditorBrowsableState.Advanced)]
  599. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  600. {
  601. _savedViewState = viewState;
  602. }
  603. [EditorBrowsable (EditorBrowsableState.Advanced)]
  604. protected virtual object LoadPageStateFromPersistenceMedium ()
  605. {
  606. NameValueCollection postdata = DeterminePostBackMode ();
  607. string view_state;
  608. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  609. return null;
  610. _savedViewState = null;
  611. LosFormatter fmt = new LosFormatter ();
  612. try {
  613. _savedViewState = fmt.Deserialize (view_state);
  614. } catch (Exception e) {
  615. throw new HttpException ("Error restoring page viewstate.\n{0}", e);
  616. }
  617. return _savedViewState;
  618. }
  619. internal void LoadPageViewState()
  620. {
  621. WebTrace.PushContext ("LoadPageViewState");
  622. object sState = LoadPageStateFromPersistenceMedium ();
  623. WebTrace.WriteLine ("sState = '{0}'", sState);
  624. if (sState != null) {
  625. Pair pair = (Pair) sState;
  626. LoadViewStateRecursive (pair.First);
  627. _requiresPostBack = pair.Second as ArrayList;
  628. }
  629. WebTrace.PopContext ();
  630. }
  631. internal void SavePageViewState ()
  632. {
  633. if (!handleViewState)
  634. return;
  635. Pair pair = new Pair ();
  636. pair.First = SaveViewStateRecursive ();
  637. if (_requiresPostBack != null && _requiresPostBack.Count > 0)
  638. pair.Second = _requiresPostBack;
  639. SavePageStateToPersistenceMedium (pair);
  640. }
  641. public virtual void Validate ()
  642. {
  643. if (_validators == null || _validators.Count == 0){
  644. _isValid = true;
  645. return;
  646. }
  647. bool all_valid = true;
  648. foreach (IValidator v in _validators){
  649. v.Validate ();
  650. if (v.IsValid == false)
  651. all_valid = false;
  652. }
  653. if (all_valid)
  654. _isValid = true;
  655. }
  656. [EditorBrowsable (EditorBrowsableState.Advanced)]
  657. public virtual void VerifyRenderingInServerForm (Control control)
  658. {
  659. if (!renderingForm)
  660. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  661. "' must be rendered within a HtmlForm");
  662. }
  663. #endregion
  664. }
  665. }