Page.cs 20 KB

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