Page.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. //
  2. // System.Web.UI.Page
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.Collections;
  12. using System.Collections.Specialized;
  13. using System.IO;
  14. using System.Security.Principal;
  15. using System.Text;
  16. using System.Web;
  17. using System.Web.Caching;
  18. using System.Web.SessionState;
  19. using System.Web.Util;
  20. namespace System.Web.UI
  21. {
  22. public class Page : TemplateControl, IHttpHandler
  23. {
  24. private string _culture;
  25. private bool _viewState = true;
  26. private bool _viewStateMac = false;
  27. private string _errorPage;
  28. private string _ID;
  29. private bool _isValid;
  30. private HttpServerUtility _server;
  31. private bool _smartNavigation = false;
  32. private TraceContext _trace;
  33. private bool _traceEnabled;
  34. private TraceMode _traceModeValue;
  35. private int _transactionMode;
  36. private string _UICulture;
  37. private HttpContext _context;
  38. private ValidatorCollection _validators;
  39. private bool renderingForm;
  40. private object _savedViewState;
  41. private ArrayList _requiresPostBack;
  42. private ArrayList requiresPostDataChanged;
  43. private ArrayList requiresRaiseEvent;
  44. private NameValueCollection secondPostData;
  45. private bool requiresPostBackScript = false;
  46. private bool postBackScriptRendered = false;
  47. #region Fields
  48. protected const string postEventArgumentID = ""; //FIXME
  49. protected const string postEventSourceID = "";
  50. #endregion
  51. #region Constructor
  52. public Page ()
  53. {
  54. Page = this;
  55. }
  56. #endregion
  57. #region Properties
  58. public HttpApplicationState Application
  59. {
  60. get { return _context.Application; }
  61. }
  62. bool AspCompatMode
  63. {
  64. set { throw new NotImplementedException (); }
  65. }
  66. bool Buffer
  67. {
  68. set { Response.BufferOutput = value; }
  69. }
  70. public Cache Cache
  71. {
  72. get { return _context.Cache; }
  73. }
  74. [MonoTODO]
  75. public string ClientTarget
  76. {
  77. get { throw new NotImplementedException (); }
  78. set { throw new NotImplementedException (); }
  79. }
  80. [MonoTODO]
  81. int CodePage
  82. {
  83. set { throw new NotImplementedException (); }
  84. }
  85. [MonoTODO]
  86. string ContentType
  87. {
  88. set { throw new NotImplementedException (); }
  89. }
  90. protected override HttpContext Context
  91. {
  92. get { return _context; }
  93. }
  94. string Culture
  95. {
  96. set { _culture = value; }
  97. }
  98. public override bool EnableViewState
  99. {
  100. get { return _viewState; }
  101. set { _viewState = value; }
  102. }
  103. protected bool EnableViewStateMac
  104. {
  105. get { return _viewStateMac; }
  106. set { _viewStateMac = value; }
  107. }
  108. public string ErrorPage
  109. {
  110. get { return _errorPage; }
  111. set { _errorPage = value; }
  112. }
  113. protected ArrayList FileDependencies
  114. {
  115. set {
  116. if (Response != null)
  117. Response.AddFileDependencies (value);
  118. }
  119. }
  120. public override string ID
  121. {
  122. get { return _ID; }
  123. set { _ID = value; }
  124. }
  125. public bool IsPostBack
  126. {
  127. get {
  128. return (0 == String.Compare (Request.HttpMethod, "POST", true));
  129. }
  130. }
  131. [MonoTODO]
  132. public bool IsReusable
  133. {
  134. get { throw new NotImplementedException (); }
  135. }
  136. public bool IsValid
  137. {
  138. get { return _isValid; }
  139. }
  140. [MonoTODO]
  141. int LCID {
  142. set { throw new NotImplementedException (); }
  143. }
  144. public HttpRequest Request
  145. {
  146. get { return _context.Request; }
  147. }
  148. public HttpResponse Response
  149. {
  150. get { return _context.Response; }
  151. }
  152. string ResponseEncoding
  153. {
  154. set { Response.ContentEncoding = Encoding.GetEncoding (value); }
  155. }
  156. public HttpServerUtility Server
  157. {
  158. get { return _server; }
  159. }
  160. public virtual HttpSessionState Session
  161. {
  162. get { return _context.Session; }
  163. }
  164. public bool SmartNavigation
  165. {
  166. get { return _smartNavigation; }
  167. set { _smartNavigation = value; }
  168. }
  169. public TraceContext Trace
  170. {
  171. get { return _trace; }
  172. }
  173. bool TraceEnabled
  174. {
  175. set { _traceEnabled = value; }
  176. }
  177. TraceMode TraceModeValue
  178. {
  179. set { _traceModeValue = value; }
  180. }
  181. int TransactionMode
  182. {
  183. set { _transactionMode = value; }
  184. }
  185. string UICulture
  186. {
  187. set { _UICulture = value; }
  188. }
  189. public IPrincipal User
  190. {
  191. get { return _context.User; }
  192. }
  193. public ValidatorCollection Validators
  194. {
  195. get {
  196. if (_validators == null)
  197. _validators = new ValidatorCollection ();
  198. return _validators;
  199. }
  200. }
  201. public override bool Visible
  202. {
  203. get { return base.Visible; }
  204. set { base.Visible = value; }
  205. }
  206. #endregion
  207. #region Methods
  208. [MonoTODO]
  209. protected IAsyncResult AspCompatBeginProcessRequest (HttpContext context,
  210. AsyncCallback cb,
  211. object extraData)
  212. {
  213. throw new NotImplementedException ();
  214. }
  215. [MonoTODO]
  216. protected void AspcompatEndProcessRequest (IAsyncResult result)
  217. {
  218. throw new NotImplementedException ();
  219. }
  220. protected virtual HtmlTextWriter CreateHtmlTextWriter (TextWriter tw)
  221. {
  222. return new HtmlTextWriter (tw);
  223. }
  224. [MonoTODO]
  225. public void DesignerInitialize ()
  226. {
  227. throw new NotImplementedException ();
  228. }
  229. protected virtual NameValueCollection DeterminePostBackMode ()
  230. {
  231. if (_context == null)
  232. return null;
  233. HttpRequest req = _context.Request;
  234. if (req == null)
  235. return null;
  236. NameValueCollection coll = null;
  237. if (IsPostBack)
  238. coll = _context.Request.Form;
  239. else
  240. coll = _context.Request.QueryString;
  241. if (coll == null || coll ["__VIEWSTATE"] == null)
  242. return null;
  243. return coll;
  244. }
  245. public string GetPostBackClientEvent (Control control, string argument)
  246. {
  247. return GetPostBackEventReference (control, argument);
  248. }
  249. public string GetPostBackClientHyperlink (Control control, string argument)
  250. {
  251. return "javascript:" + GetPostBackEventReference (control, argument);
  252. }
  253. public string GetPostBackEventReference (Control control)
  254. {
  255. return GetPostBackEventReference (control, "");
  256. }
  257. public string GetPostBackEventReference (Control control, string argument)
  258. {
  259. RequiresPostBackScript ();
  260. return String.Format ("__doPostBack ('{0}', '{1}')", control.ID, argument);
  261. }
  262. internal void RequiresPostBackScript ()
  263. {
  264. requiresPostBackScript = true;
  265. }
  266. public virtual int GetTypeHashCode ()
  267. {
  268. return GetHashCode ();
  269. }
  270. [MonoTODO]
  271. protected virtual void InitOutputCache (int duration,
  272. string varyByHeader,
  273. string varyByCustom,
  274. OutputCacheLocation location,
  275. string varyByParam)
  276. {
  277. throw new NotImplementedException ();
  278. }
  279. [MonoTODO]
  280. public bool IsClientScriptBlockRegistered (string key)
  281. {
  282. throw new NotImplementedException ();
  283. }
  284. [MonoTODO]
  285. public bool IsStartupScriptRegistered (string key)
  286. {
  287. throw new NotImplementedException ();
  288. }
  289. [MonoTODO]
  290. public string MapPath (string virtualPath)
  291. {
  292. throw new NotImplementedException ();
  293. }
  294. private void RenderPostBackScript (HtmlTextWriter writer, string formUniqueID)
  295. {
  296. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTTARGET\" value=\"\" />");
  297. writer.WriteLine ("<input type=\"hidden\" name=\"__EVENTARGUMENT\" value=\"\" />");
  298. writer.WriteLine ();
  299. writer.WriteLine ("<script language=\"javascript\">");
  300. writer.WriteLine ("<!--");
  301. writer.WriteLine ("\tfunction __doPostBack(eventTarget, eventArgument) {");
  302. writer.WriteLine ("\t\tvar theform = document.{0};", formUniqueID);
  303. writer.WriteLine ("\t\ttheform.__EVENTTARGET.value = eventTarget;");
  304. writer.WriteLine ("\t\ttheform.__EVENTARGUMENT.value = eventArgument;");
  305. writer.WriteLine ("\t\ttheform.submit();");
  306. writer.WriteLine ("\t}");
  307. writer.WriteLine ("// -->");
  308. writer.WriteLine ("</script>");
  309. }
  310. internal void OnFormRender (HtmlTextWriter writer, string formUniqueID)
  311. {
  312. if (renderingForm)
  313. throw new HttpException ("Only 1 HtmlForm is allowed per page.");
  314. renderingForm = true;
  315. writer.WriteLine ();
  316. writer.Write ("<input type=\"hidden\" name=\"__VIEWSTATE\" ");
  317. writer.WriteLine ("value=\"{0}\" />", GetViewStateString ());
  318. if (requiresPostBackScript) {
  319. RenderPostBackScript (writer, formUniqueID);
  320. postBackScriptRendered = true;
  321. }
  322. }
  323. internal string GetViewStateString ()
  324. {
  325. StringWriter sr = new StringWriter ();
  326. LosFormatter fmt = new LosFormatter ();
  327. fmt.Serialize (sr, _savedViewState);
  328. return sr.GetStringBuilder ().ToString ();
  329. }
  330. internal void OnFormPostRender (HtmlTextWriter writer, string formUniqueID)
  331. {
  332. if (!postBackScriptRendered && requiresPostBackScript)
  333. RenderPostBackScript (writer, formUniqueID);
  334. renderingForm = false;
  335. postBackScriptRendered = false;
  336. }
  337. private void ProcessPostData (NameValueCollection data, bool second)
  338. {
  339. if (data == null)
  340. return;
  341. foreach (string id in data.AllKeys){
  342. if (id == "__VIEWSTATE")
  343. continue;
  344. Control ctrl = FindControl (id);
  345. if (ctrl != null){
  346. IPostBackDataHandler pbdh = ctrl as IPostBackDataHandler;
  347. IPostBackEventHandler pbeh = ctrl as IPostBackEventHandler;
  348. if (pbdh == null) {
  349. if (pbeh != null)
  350. RegisterRequiresRaiseEvent (pbeh);
  351. continue;
  352. }
  353. if (pbdh.LoadPostData (id, data) == true) {
  354. if (requiresPostDataChanged == null)
  355. requiresPostDataChanged = new ArrayList ();
  356. requiresPostDataChanged.Add (pbdh);
  357. }
  358. } else if (!second) {
  359. if (secondPostData == null)
  360. secondPostData = new NameValueCollection ();
  361. secondPostData.Add (id, null);
  362. }
  363. }
  364. }
  365. public void ProcessRequest (HttpContext context)
  366. {
  367. _context = context;
  368. WebTrace.PushContext ("Page.ProcessRequest ()");
  369. WebTrace.WriteLine ("Entering");
  370. WireupAutomaticEvents ();
  371. WebTrace.WriteLine ("Finished hookup");
  372. //-- Control execution lifecycle in the docs
  373. WebTrace.WriteLine ("Controls.Clear");
  374. Controls.Clear ();
  375. WebTrace.WriteLine ("FrameworkInitialize");
  376. FrameworkInitialize ();
  377. WebTrace.WriteLine ("InitRecursive");
  378. InitRecursive (null);
  379. renderingForm = false;
  380. _context = context;
  381. if (IsPostBack) {
  382. LoadPageViewState ();
  383. ProcessPostData (DeterminePostBackMode (), false);
  384. }
  385. WebTrace.WriteLine ("LoadRecursive");
  386. LoadRecursive ();
  387. if (IsPostBack) {
  388. ProcessPostData (secondPostData, true);
  389. RaiseChangedEvents ();
  390. RaisePostBackEvents ();
  391. }
  392. WebTrace.WriteLine ("PreRenderRecursiveInternal");
  393. PreRenderRecursiveInternal ();
  394. WebTrace.WriteLine ("SavePageViewState");
  395. SavePageViewState ();
  396. //--
  397. StringBuilder sb = new StringBuilder ();
  398. StringWriter sr = new StringWriter (sb);
  399. HtmlTextWriter output = new HtmlTextWriter (context.Response.Output);
  400. WebTrace.WriteLine ("RenderControl");
  401. RenderControl (output);
  402. _context = null;
  403. WebTrace.WriteLine ("UnloadRecursive");
  404. UnloadRecursive (true);
  405. WebTrace.WriteLine ("End");
  406. WebTrace.PopContext ();
  407. }
  408. internal void RaisePostBackEvents ()
  409. {
  410. NameValueCollection postdata = DeterminePostBackMode ();
  411. if (postdata == null)
  412. return;
  413. string eventTarget = postdata ["__EVENTTARGET"];
  414. if (eventTarget != null && eventTarget.Length > 0) {
  415. Control target = FindControl (eventTarget);
  416. if (!(target is IPostBackEventHandler))
  417. return;
  418. string eventArgument = postdata ["__EVENTARGUMENT"];
  419. RaisePostBackEvent ((IPostBackEventHandler) target, eventArgument);
  420. return;
  421. }
  422. if (requiresRaiseEvent == null)
  423. return;
  424. foreach (Control c in requiresRaiseEvent)
  425. RaisePostBackEvent ((IPostBackEventHandler) c, postdata [c.ID]);
  426. requiresRaiseEvent.Clear ();
  427. }
  428. internal void RaiseChangedEvents ()
  429. {
  430. if (requiresPostDataChanged == null)
  431. return;
  432. foreach (IPostBackDataHandler ipdh in requiresPostDataChanged)
  433. ipdh.RaisePostDataChangedEvent ();
  434. requiresPostDataChanged.Clear ();
  435. }
  436. protected virtual void RaisePostBackEvent (IPostBackEventHandler sourceControl, string eventArgument)
  437. {
  438. sourceControl.RaisePostBackEvent (eventArgument);
  439. }
  440. [MonoTODO]
  441. public void RegisterArrayDeclaration (string arrayName, string arrayValue)
  442. {
  443. throw new NotImplementedException ();
  444. }
  445. [MonoTODO]
  446. public virtual void RegisterClientScriptBlock (string key, string script)
  447. {
  448. throw new NotImplementedException ();
  449. }
  450. [MonoTODO]
  451. public virtual void RegisterHiddenField (string hiddenFieldName, string hiddenFieldInitialValue)
  452. {
  453. throw new NotImplementedException ();
  454. }
  455. [MonoTODO]
  456. public void RegisterClientScriptFile (string a, string b, string c)
  457. {
  458. throw new NotImplementedException ();
  459. }
  460. [MonoTODO]
  461. public void RegisterOnSubmitStatement (string key, string script)
  462. {
  463. throw new NotImplementedException ();
  464. }
  465. public void RegisterRequiresPostBack (Control control)
  466. {
  467. if (_requiresPostBack == null)
  468. _requiresPostBack = new ArrayList ();
  469. _requiresPostBack.Add (control);
  470. }
  471. public virtual void RegisterRequiresRaiseEvent (IPostBackEventHandler control)
  472. {
  473. if (requiresRaiseEvent == null)
  474. requiresRaiseEvent = new ArrayList ();
  475. requiresRaiseEvent.Add (control);
  476. }
  477. [MonoTODO]
  478. public void RegisterViewStateHandler ()
  479. {
  480. // Do nothing
  481. }
  482. protected virtual void SavePageStateToPersistenceMedium (object viewState)
  483. {
  484. _savedViewState = viewState;
  485. }
  486. protected virtual object LoadPageStateFromPersistenceMedium ()
  487. {
  488. NameValueCollection postdata = DeterminePostBackMode ();
  489. string view_state;
  490. if (postdata == null || (view_state = postdata ["__VIEWSTATE"]) == null)
  491. return null;
  492. _savedViewState = null;
  493. LosFormatter fmt = new LosFormatter ();
  494. try {
  495. _savedViewState = fmt.Deserialize (view_state);
  496. } catch {
  497. throw new HttpException ("Error restoring page viewstate.");
  498. }
  499. return _savedViewState;
  500. }
  501. internal void LoadPageViewState()
  502. {
  503. WebTrace.PushContext ("LoadPageViewState");
  504. object sState = LoadPageStateFromPersistenceMedium ();
  505. WebTrace.WriteLine ("sState = '{0}'", sState);
  506. if (sState != null) {
  507. Pair pair = (Pair) sState;
  508. LoadViewStateRecursive (pair.First);
  509. _requiresPostBack = pair.Second as ArrayList;
  510. }
  511. WebTrace.PopContext ();
  512. }
  513. internal void SavePageViewState ()
  514. {
  515. Pair pair = new Pair ();
  516. pair.First = SaveViewStateRecursive ();
  517. pair.Second = _requiresPostBack;
  518. SavePageStateToPersistenceMedium (pair);
  519. }
  520. public virtual void Validate ()
  521. {
  522. if (_validators == null || _validators.Count == 0){
  523. _isValid = true;
  524. return;
  525. }
  526. bool all_valid = true;
  527. foreach (IValidator v in _validators){
  528. v.Validate ();
  529. if (v.IsValid == false)
  530. all_valid = false;
  531. }
  532. if (all_valid)
  533. _isValid = true;
  534. }
  535. public virtual void VerifyRenderingInServerForm (Control control)
  536. {
  537. if (!renderingForm)
  538. throw new HttpException ("Control '" + control.ClientID + " " + control.GetType () +
  539. "' must be rendered within a HtmlForm");
  540. }
  541. #endregion
  542. }
  543. }